Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 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. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5 | #include "src/v8.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 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" |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 21 | #include "src/runtime/runtime.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 22 | #include "src/string-search.h" |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 23 | #include "src/unicode-decoder.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 24 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 25 | #ifndef V8_INTERPRETED_REGEXP |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 26 | #if V8_TARGET_ARCH_IA32 |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 27 | #include "src/ia32/regexp-macro-assembler-ia32.h" // NOLINT |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 28 | #elif V8_TARGET_ARCH_X64 |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 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 |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 32 | #elif V8_TARGET_ARCH_ARM |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 33 | #include "src/arm/regexp-macro-assembler-arm.h" // NOLINT |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 34 | #elif V8_TARGET_ARCH_MIPS |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 35 | #include "src/mips/regexp-macro-assembler-mips.h" // NOLINT |
| 36 | #elif V8_TARGET_ARCH_MIPS64 |
| 37 | #include "src/mips64/regexp-macro-assembler-mips64.h" // NOLINT |
| 38 | #elif V8_TARGET_ARCH_X87 |
| 39 | #include "src/x87/regexp-macro-assembler-x87.h" // NOLINT |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 40 | #else |
| 41 | #error Unsupported target architecture. |
| 42 | #endif |
| 43 | #endif |
| 44 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 45 | #include "src/interpreter-irregexp.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 46 | |
| 47 | |
| 48 | namespace v8 { |
| 49 | namespace internal { |
| 50 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 51 | MaybeHandle<Object> RegExpImpl::CreateRegExpLiteral( |
| 52 | Handle<JSFunction> constructor, |
| 53 | Handle<String> pattern, |
| 54 | Handle<String> flags) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 55 | // Call the construct code with 2 arguments. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 56 | Handle<Object> argv[] = { pattern, flags }; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 57 | return Execution::New(constructor, arraysize(argv), argv); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 61 | MUST_USE_RESULT |
| 62 | static inline MaybeHandle<Object> ThrowRegExpException( |
| 63 | Handle<JSRegExp> re, |
| 64 | Handle<String> pattern, |
| 65 | Handle<String> error_text, |
| 66 | const char* message) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 67 | Isolate* isolate = re->GetIsolate(); |
| 68 | Factory* factory = isolate->factory(); |
| 69 | Handle<FixedArray> elements = factory->NewFixedArray(2); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 70 | elements->set(0, *pattern); |
| 71 | elements->set(1, *error_text); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 72 | Handle<JSArray> array = factory->NewJSArrayWithElements(elements); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 73 | Handle<Object> regexp_err; |
| 74 | THROW_NEW_ERROR(isolate, NewSyntaxError(message, array), Object); |
| 75 | } |
| 76 | |
| 77 | |
| 78 | ContainedInLattice AddRange(ContainedInLattice containment, |
| 79 | const int* ranges, |
| 80 | int ranges_length, |
| 81 | Interval new_range) { |
| 82 | DCHECK((ranges_length & 1) == 1); |
| 83 | DCHECK(ranges[ranges_length - 1] == String::kMaxUtf16CodeUnit + 1); |
| 84 | if (containment == kLatticeUnknown) return containment; |
| 85 | bool inside = false; |
| 86 | int last = 0; |
| 87 | for (int i = 0; i < ranges_length; inside = !inside, last = ranges[i], i++) { |
| 88 | // Consider the range from last to ranges[i]. |
| 89 | // We haven't got to the new range yet. |
| 90 | if (ranges[i] <= new_range.from()) continue; |
| 91 | // New range is wholly inside last-ranges[i]. Note that new_range.to() is |
| 92 | // inclusive, but the values in ranges are not. |
| 93 | if (last <= new_range.from() && new_range.to() < ranges[i]) { |
| 94 | return Combine(containment, inside ? kLatticeIn : kLatticeOut); |
| 95 | } |
| 96 | return kLatticeUnknown; |
| 97 | } |
| 98 | return containment; |
| 99 | } |
| 100 | |
| 101 | |
| 102 | // More makes code generation slower, less makes V8 benchmark score lower. |
| 103 | const int kMaxLookaheadForBoyerMoore = 8; |
| 104 | // In a 3-character pattern you can maximally step forwards 3 characters |
| 105 | // at a time, which is not always enough to pay for the extra logic. |
| 106 | const int kPatternTooShortForBoyerMoore = 2; |
| 107 | |
| 108 | |
| 109 | // Identifies the sort of regexps where the regexp engine is faster |
| 110 | // than the code used for atom matches. |
| 111 | static bool HasFewDifferentCharacters(Handle<String> pattern) { |
| 112 | int length = Min(kMaxLookaheadForBoyerMoore, pattern->length()); |
| 113 | if (length <= kPatternTooShortForBoyerMoore) return false; |
| 114 | const int kMod = 128; |
| 115 | bool character_found[kMod]; |
| 116 | int different = 0; |
| 117 | memset(&character_found[0], 0, sizeof(character_found)); |
| 118 | for (int i = 0; i < length; i++) { |
| 119 | int ch = (pattern->Get(i) & (kMod - 1)); |
| 120 | if (!character_found[ch]) { |
| 121 | character_found[ch] = true; |
| 122 | different++; |
| 123 | // We declare a regexp low-alphabet if it has at least 3 times as many |
| 124 | // characters as it has different characters. |
| 125 | if (different * 3 > length) return false; |
| 126 | } |
| 127 | } |
| 128 | return true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | |
| 132 | // Generic RegExp methods. Dispatches to implementation specific methods. |
| 133 | |
| 134 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 135 | MaybeHandle<Object> RegExpImpl::Compile(Handle<JSRegExp> re, |
| 136 | Handle<String> pattern, |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 137 | JSRegExp::Flags flags) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 138 | Isolate* isolate = re->GetIsolate(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 139 | Zone zone(isolate); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 140 | CompilationCache* compilation_cache = isolate->compilation_cache(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 141 | MaybeHandle<FixedArray> maybe_cached = |
| 142 | compilation_cache->LookupRegExp(pattern, flags); |
| 143 | Handle<FixedArray> cached; |
| 144 | bool in_cache = maybe_cached.ToHandle(&cached); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 145 | LOG(isolate, RegExpCompileEvent(re, in_cache)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 146 | |
| 147 | Handle<Object> result; |
| 148 | if (in_cache) { |
| 149 | re->set_data(*cached); |
| 150 | return re; |
| 151 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 152 | pattern = String::Flatten(pattern); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 153 | PostponeInterruptsScope postpone(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 154 | RegExpCompileData parse_result; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 155 | FlatStringReader reader(isolate, pattern); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 156 | if (!RegExpParser::ParseRegExp(&reader, flags.is_multiline(), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 157 | &parse_result, &zone)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 158 | // Throw an exception if we fail to parse the pattern. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 159 | return ThrowRegExpException(re, |
| 160 | pattern, |
| 161 | parse_result.error, |
| 162 | "malformed_regexp"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 165 | bool has_been_compiled = false; |
| 166 | |
| 167 | if (parse_result.simple && |
| 168 | !flags.is_ignore_case() && |
| 169 | !flags.is_sticky() && |
| 170 | !HasFewDifferentCharacters(pattern)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 171 | // Parse-tree is a single atom that is equal to the pattern. |
| 172 | AtomCompile(re, pattern, flags, pattern); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 173 | has_been_compiled = true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 174 | } else if (parse_result.tree->IsAtom() && |
| 175 | !flags.is_ignore_case() && |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 176 | !flags.is_sticky() && |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 177 | parse_result.capture_count == 0) { |
| 178 | RegExpAtom* atom = parse_result.tree->AsAtom(); |
| 179 | Vector<const uc16> atom_pattern = atom->data(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 180 | Handle<String> atom_string; |
| 181 | ASSIGN_RETURN_ON_EXCEPTION( |
| 182 | isolate, atom_string, |
| 183 | isolate->factory()->NewStringFromTwoByte(atom_pattern), |
| 184 | Object); |
| 185 | if (!HasFewDifferentCharacters(atom_string)) { |
| 186 | AtomCompile(re, pattern, flags, atom_string); |
| 187 | has_been_compiled = true; |
| 188 | } |
| 189 | } |
| 190 | if (!has_been_compiled) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 191 | IrregexpInitialize(re, pattern, flags, parse_result.capture_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 192 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 193 | DCHECK(re->data()->IsFixedArray()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 194 | // Compilation succeeded so the data is set on the regexp |
| 195 | // and we can store it in the cache. |
| 196 | Handle<FixedArray> data(FixedArray::cast(re->data())); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 197 | compilation_cache->PutRegExp(pattern, flags, data); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 198 | |
| 199 | return re; |
| 200 | } |
| 201 | |
| 202 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 203 | MaybeHandle<Object> RegExpImpl::Exec(Handle<JSRegExp> regexp, |
| 204 | Handle<String> subject, |
| 205 | int index, |
| 206 | Handle<JSArray> last_match_info) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 207 | switch (regexp->TypeTag()) { |
| 208 | case JSRegExp::ATOM: |
| 209 | return AtomExec(regexp, subject, index, last_match_info); |
| 210 | case JSRegExp::IRREGEXP: { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 211 | return IrregexpExec(regexp, subject, index, last_match_info); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 212 | } |
| 213 | default: |
| 214 | UNREACHABLE(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 215 | return MaybeHandle<Object>(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | |
| 219 | |
| 220 | // RegExp Atom implementation: Simple string search using indexOf. |
| 221 | |
| 222 | |
| 223 | void RegExpImpl::AtomCompile(Handle<JSRegExp> re, |
| 224 | Handle<String> pattern, |
| 225 | JSRegExp::Flags flags, |
| 226 | Handle<String> match_pattern) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 227 | re->GetIsolate()->factory()->SetRegExpAtomData(re, |
| 228 | JSRegExp::ATOM, |
| 229 | pattern, |
| 230 | flags, |
| 231 | match_pattern); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | |
| 235 | static void SetAtomLastCapture(FixedArray* array, |
| 236 | String* subject, |
| 237 | int from, |
| 238 | int to) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 239 | SealHandleScope shs(array->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 240 | RegExpImpl::SetLastCaptureCount(array, 2); |
| 241 | RegExpImpl::SetLastSubject(array, subject); |
| 242 | RegExpImpl::SetLastInput(array, subject); |
| 243 | RegExpImpl::SetCapture(array, 0, from); |
| 244 | RegExpImpl::SetCapture(array, 1, to); |
| 245 | } |
| 246 | |
| 247 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 248 | int RegExpImpl::AtomExecRaw(Handle<JSRegExp> regexp, |
| 249 | Handle<String> subject, |
| 250 | int index, |
| 251 | int32_t* output, |
| 252 | int output_size) { |
| 253 | Isolate* isolate = regexp->GetIsolate(); |
| 254 | |
| 255 | DCHECK(0 <= index); |
| 256 | DCHECK(index <= subject->length()); |
| 257 | |
| 258 | subject = String::Flatten(subject); |
| 259 | DisallowHeapAllocation no_gc; // ensure vectors stay valid |
| 260 | |
| 261 | String* needle = String::cast(regexp->DataAt(JSRegExp::kAtomPatternIndex)); |
| 262 | int needle_len = needle->length(); |
| 263 | DCHECK(needle->IsFlat()); |
| 264 | DCHECK_LT(0, needle_len); |
| 265 | |
| 266 | if (index + needle_len > subject->length()) { |
| 267 | return RegExpImpl::RE_FAILURE; |
| 268 | } |
| 269 | |
| 270 | for (int i = 0; i < output_size; i += 2) { |
| 271 | String::FlatContent needle_content = needle->GetFlatContent(); |
| 272 | String::FlatContent subject_content = subject->GetFlatContent(); |
| 273 | DCHECK(needle_content.IsFlat()); |
| 274 | DCHECK(subject_content.IsFlat()); |
| 275 | // dispatch on type of strings |
| 276 | index = |
| 277 | (needle_content.IsOneByte() |
| 278 | ? (subject_content.IsOneByte() |
| 279 | ? SearchString(isolate, subject_content.ToOneByteVector(), |
| 280 | needle_content.ToOneByteVector(), index) |
| 281 | : SearchString(isolate, subject_content.ToUC16Vector(), |
| 282 | needle_content.ToOneByteVector(), index)) |
| 283 | : (subject_content.IsOneByte() |
| 284 | ? SearchString(isolate, subject_content.ToOneByteVector(), |
| 285 | needle_content.ToUC16Vector(), index) |
| 286 | : SearchString(isolate, subject_content.ToUC16Vector(), |
| 287 | needle_content.ToUC16Vector(), index))); |
| 288 | if (index == -1) { |
| 289 | return i / 2; // Return number of matches. |
| 290 | } else { |
| 291 | output[i] = index; |
| 292 | output[i+1] = index + needle_len; |
| 293 | index += needle_len; |
| 294 | } |
| 295 | } |
| 296 | return output_size / 2; |
| 297 | } |
| 298 | |
| 299 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 300 | Handle<Object> RegExpImpl::AtomExec(Handle<JSRegExp> re, |
| 301 | Handle<String> subject, |
| 302 | int index, |
| 303 | Handle<JSArray> last_match_info) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 304 | Isolate* isolate = re->GetIsolate(); |
| 305 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 306 | static const int kNumRegisters = 2; |
| 307 | STATIC_ASSERT(kNumRegisters <= Isolate::kJSRegexpStaticOffsetsVectorSize); |
| 308 | int32_t* output_registers = isolate->jsregexp_static_offsets_vector(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 309 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 310 | int res = AtomExecRaw(re, subject, index, output_registers, kNumRegisters); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 311 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 312 | if (res == RegExpImpl::RE_FAILURE) return isolate->factory()->null_value(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 313 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 314 | DCHECK_EQ(res, RegExpImpl::RE_SUCCESS); |
| 315 | SealHandleScope shs(isolate); |
| 316 | FixedArray* array = FixedArray::cast(last_match_info->elements()); |
| 317 | SetAtomLastCapture(array, *subject, output_registers[0], output_registers[1]); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 318 | return last_match_info; |
| 319 | } |
| 320 | |
| 321 | |
| 322 | // Irregexp implementation. |
| 323 | |
| 324 | // Ensures that the regexp object contains a compiled version of the |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 325 | // source for either one-byte or two-byte subject strings. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 326 | // If the compiled version doesn't already exist, it is compiled |
| 327 | // from the source pattern. |
| 328 | // If compilation fails, an exception is thrown and this function |
| 329 | // returns false. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 330 | bool RegExpImpl::EnsureCompiledIrregexp(Handle<JSRegExp> re, |
| 331 | Handle<String> sample_subject, |
| 332 | bool is_one_byte) { |
| 333 | Object* compiled_code = re->DataAt(JSRegExp::code_index(is_one_byte)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 334 | #ifdef V8_INTERPRETED_REGEXP |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 335 | if (compiled_code->IsByteArray()) return true; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 336 | #else // V8_INTERPRETED_REGEXP (RegExp native code) |
| 337 | if (compiled_code->IsCode()) return true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 338 | #endif |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 339 | // We could potentially have marked this as flushable, but have kept |
| 340 | // a saved version if we did not flush it yet. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 341 | Object* saved_code = re->DataAt(JSRegExp::saved_code_index(is_one_byte)); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 342 | if (saved_code->IsCode()) { |
| 343 | // Reinstate the code in the original place. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 344 | re->SetDataAt(JSRegExp::code_index(is_one_byte), saved_code); |
| 345 | DCHECK(compiled_code->IsSmi()); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 346 | return true; |
| 347 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 348 | return CompileIrregexp(re, sample_subject, is_one_byte); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 352 | static void CreateRegExpErrorObjectAndThrow(Handle<JSRegExp> re, |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 353 | Handle<String> error_message, |
| 354 | Isolate* isolate) { |
| 355 | Factory* factory = isolate->factory(); |
| 356 | Handle<FixedArray> elements = factory->NewFixedArray(2); |
| 357 | elements->set(0, re->Pattern()); |
| 358 | elements->set(1, *error_message); |
| 359 | Handle<JSArray> array = factory->NewJSArrayWithElements(elements); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 360 | Handle<Object> error; |
| 361 | MaybeHandle<Object> maybe_error = |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 362 | factory->NewSyntaxError("malformed_regexp", array); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 363 | if (maybe_error.ToHandle(&error)) isolate->Throw(*error); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 367 | bool RegExpImpl::CompileIrregexp(Handle<JSRegExp> re, |
| 368 | Handle<String> sample_subject, |
| 369 | bool is_one_byte) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 370 | // Compile the RegExp. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 371 | Isolate* isolate = re->GetIsolate(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 372 | Zone zone(isolate); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 373 | PostponeInterruptsScope postpone(isolate); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 374 | // If we had a compilation error the last time this is saved at the |
| 375 | // saved code index. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 376 | Object* entry = re->DataAt(JSRegExp::code_index(is_one_byte)); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 377 | // When arriving here entry can only be a smi, either representing an |
| 378 | // uncompiled regexp, a previous compilation error, or code that has |
| 379 | // been flushed. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 380 | DCHECK(entry->IsSmi()); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 381 | int entry_value = Smi::cast(entry)->value(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 382 | DCHECK(entry_value == JSRegExp::kUninitializedValue || |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 383 | entry_value == JSRegExp::kCompilationErrorValue || |
| 384 | (entry_value < JSRegExp::kCodeAgeMask && entry_value >= 0)); |
| 385 | |
| 386 | if (entry_value == JSRegExp::kCompilationErrorValue) { |
| 387 | // A previous compilation failed and threw an error which we store in |
| 388 | // the saved code index (we store the error message, not the actual |
| 389 | // error). Recreate the error object and throw it. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 390 | Object* error_string = re->DataAt(JSRegExp::saved_code_index(is_one_byte)); |
| 391 | DCHECK(error_string->IsString()); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 392 | Handle<String> error_message(String::cast(error_string)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 393 | CreateRegExpErrorObjectAndThrow(re, error_message, isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 394 | return false; |
| 395 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 396 | |
| 397 | JSRegExp::Flags flags = re->GetFlags(); |
| 398 | |
| 399 | Handle<String> pattern(re->Pattern()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 400 | pattern = String::Flatten(pattern); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 401 | RegExpCompileData compile_data; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 402 | FlatStringReader reader(isolate, pattern); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 403 | if (!RegExpParser::ParseRegExp(&reader, flags.is_multiline(), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 404 | &compile_data, |
| 405 | &zone)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 406 | // Throw an exception if we fail to parse the pattern. |
| 407 | // THIS SHOULD NOT HAPPEN. We already pre-parsed it successfully once. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 408 | USE(ThrowRegExpException(re, |
| 409 | pattern, |
| 410 | compile_data.error, |
| 411 | "malformed_regexp")); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 412 | return false; |
| 413 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 414 | RegExpEngine::CompilationResult result = RegExpEngine::Compile( |
| 415 | &compile_data, flags.is_ignore_case(), flags.is_global(), |
| 416 | flags.is_multiline(), flags.is_sticky(), pattern, sample_subject, |
| 417 | is_one_byte, &zone); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 418 | if (result.error_message != NULL) { |
| 419 | // Unable to compile regexp. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 420 | Handle<String> error_message = isolate->factory()->NewStringFromUtf8( |
| 421 | CStrVector(result.error_message)).ToHandleChecked(); |
| 422 | CreateRegExpErrorObjectAndThrow(re, error_message, isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 423 | return false; |
| 424 | } |
| 425 | |
| 426 | Handle<FixedArray> data = Handle<FixedArray>(FixedArray::cast(re->data())); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 427 | data->set(JSRegExp::code_index(is_one_byte), result.code); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 428 | int register_max = IrregexpMaxRegisterCount(*data); |
| 429 | if (result.num_registers > register_max) { |
| 430 | SetIrregexpMaxRegisterCount(*data, result.num_registers); |
| 431 | } |
| 432 | |
| 433 | return true; |
| 434 | } |
| 435 | |
| 436 | |
| 437 | int RegExpImpl::IrregexpMaxRegisterCount(FixedArray* re) { |
| 438 | return Smi::cast( |
| 439 | re->get(JSRegExp::kIrregexpMaxRegisterCountIndex))->value(); |
| 440 | } |
| 441 | |
| 442 | |
| 443 | void RegExpImpl::SetIrregexpMaxRegisterCount(FixedArray* re, int value) { |
| 444 | re->set(JSRegExp::kIrregexpMaxRegisterCountIndex, Smi::FromInt(value)); |
| 445 | } |
| 446 | |
| 447 | |
| 448 | int RegExpImpl::IrregexpNumberOfCaptures(FixedArray* re) { |
| 449 | return Smi::cast(re->get(JSRegExp::kIrregexpCaptureCountIndex))->value(); |
| 450 | } |
| 451 | |
| 452 | |
| 453 | int RegExpImpl::IrregexpNumberOfRegisters(FixedArray* re) { |
| 454 | return Smi::cast(re->get(JSRegExp::kIrregexpMaxRegisterCountIndex))->value(); |
| 455 | } |
| 456 | |
| 457 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 458 | ByteArray* RegExpImpl::IrregexpByteCode(FixedArray* re, bool is_one_byte) { |
| 459 | return ByteArray::cast(re->get(JSRegExp::code_index(is_one_byte))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 463 | Code* RegExpImpl::IrregexpNativeCode(FixedArray* re, bool is_one_byte) { |
| 464 | return Code::cast(re->get(JSRegExp::code_index(is_one_byte))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 468 | void RegExpImpl::IrregexpInitialize(Handle<JSRegExp> re, |
| 469 | Handle<String> pattern, |
| 470 | JSRegExp::Flags flags, |
| 471 | int capture_count) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 472 | // Initialize compiled code entries to null. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 473 | re->GetIsolate()->factory()->SetRegExpIrregexpData(re, |
| 474 | JSRegExp::IRREGEXP, |
| 475 | pattern, |
| 476 | flags, |
| 477 | capture_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 481 | int RegExpImpl::IrregexpPrepare(Handle<JSRegExp> regexp, |
| 482 | Handle<String> subject) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 483 | subject = String::Flatten(subject); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 484 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 485 | // Check representation of the underlying storage. |
| 486 | bool is_one_byte = subject->IsOneByteRepresentationUnderneath(); |
| 487 | if (!EnsureCompiledIrregexp(regexp, subject, is_one_byte)) return -1; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 488 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 489 | #ifdef V8_INTERPRETED_REGEXP |
| 490 | // Byte-code regexp needs space allocated for all its registers. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 491 | // The result captures are copied to the start of the registers array |
| 492 | // if the match succeeds. This way those registers are not clobbered |
| 493 | // when we set the last match info from last successful match. |
| 494 | return IrregexpNumberOfRegisters(FixedArray::cast(regexp->data())) + |
| 495 | (IrregexpNumberOfCaptures(FixedArray::cast(regexp->data())) + 1) * 2; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 496 | #else // V8_INTERPRETED_REGEXP |
| 497 | // Native regexp only needs room to output captures. Registers are handled |
| 498 | // internally. |
| 499 | return (IrregexpNumberOfCaptures(FixedArray::cast(regexp->data())) + 1) * 2; |
| 500 | #endif // V8_INTERPRETED_REGEXP |
| 501 | } |
| 502 | |
| 503 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 504 | int RegExpImpl::IrregexpExecRaw(Handle<JSRegExp> regexp, |
| 505 | Handle<String> subject, |
| 506 | int index, |
| 507 | int32_t* output, |
| 508 | int output_size) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 509 | Isolate* isolate = regexp->GetIsolate(); |
| 510 | |
| 511 | Handle<FixedArray> irregexp(FixedArray::cast(regexp->data()), isolate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 512 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 513 | DCHECK(index >= 0); |
| 514 | DCHECK(index <= subject->length()); |
| 515 | DCHECK(subject->IsFlat()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 516 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 517 | bool is_one_byte = subject->IsOneByteRepresentationUnderneath(); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 518 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 519 | #ifndef V8_INTERPRETED_REGEXP |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 520 | DCHECK(output_size >= (IrregexpNumberOfCaptures(*irregexp) + 1) * 2); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 521 | do { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 522 | EnsureCompiledIrregexp(regexp, subject, is_one_byte); |
| 523 | Handle<Code> code(IrregexpNativeCode(*irregexp, is_one_byte), isolate); |
| 524 | // The stack is used to allocate registers for the compiled regexp code. |
| 525 | // This means that in case of failure, the output registers array is left |
| 526 | // untouched and contains the capture results from the previous successful |
| 527 | // match. We can use that to set the last match info lazily. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 528 | NativeRegExpMacroAssembler::Result res = |
| 529 | NativeRegExpMacroAssembler::Match(code, |
| 530 | subject, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 531 | output, |
| 532 | output_size, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 533 | index, |
| 534 | isolate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 535 | if (res != NativeRegExpMacroAssembler::RETRY) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 536 | DCHECK(res != NativeRegExpMacroAssembler::EXCEPTION || |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 537 | isolate->has_pending_exception()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 538 | STATIC_ASSERT( |
| 539 | static_cast<int>(NativeRegExpMacroAssembler::SUCCESS) == RE_SUCCESS); |
| 540 | STATIC_ASSERT( |
| 541 | static_cast<int>(NativeRegExpMacroAssembler::FAILURE) == RE_FAILURE); |
| 542 | STATIC_ASSERT(static_cast<int>(NativeRegExpMacroAssembler::EXCEPTION) |
| 543 | == RE_EXCEPTION); |
| 544 | return static_cast<IrregexpResult>(res); |
| 545 | } |
| 546 | // If result is RETRY, the string has changed representation, and we |
| 547 | // must restart from scratch. |
| 548 | // In this case, it means we must make sure we are prepared to handle |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 549 | // the, potentially, different subject (the string can switch between |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 550 | // being internal and external, and even between being Latin1 and UC16, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 551 | // but the characters are always the same). |
| 552 | IrregexpPrepare(regexp, subject); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 553 | is_one_byte = subject->IsOneByteRepresentationUnderneath(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 554 | } while (true); |
| 555 | UNREACHABLE(); |
| 556 | return RE_EXCEPTION; |
| 557 | #else // V8_INTERPRETED_REGEXP |
| 558 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 559 | DCHECK(output_size >= IrregexpNumberOfRegisters(*irregexp)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 560 | // We must have done EnsureCompiledIrregexp, so we can get the number of |
| 561 | // registers. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 562 | int number_of_capture_registers = |
| 563 | (IrregexpNumberOfCaptures(*irregexp) + 1) * 2; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 564 | int32_t* raw_output = &output[number_of_capture_registers]; |
| 565 | // We do not touch the actual capture result registers until we know there |
| 566 | // has been a match so that we can use those capture results to set the |
| 567 | // last match info. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 568 | for (int i = number_of_capture_registers - 1; i >= 0; i--) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 569 | raw_output[i] = -1; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 570 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 571 | Handle<ByteArray> byte_codes(IrregexpByteCode(*irregexp, is_one_byte), |
| 572 | isolate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 573 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 574 | IrregexpResult result = IrregexpInterpreter::Match(isolate, |
| 575 | byte_codes, |
| 576 | subject, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 577 | raw_output, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 578 | index); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 579 | if (result == RE_SUCCESS) { |
| 580 | // Copy capture results to the start of the registers array. |
| 581 | MemCopy(output, raw_output, number_of_capture_registers * sizeof(int32_t)); |
| 582 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 583 | if (result == RE_EXCEPTION) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 584 | DCHECK(!isolate->has_pending_exception()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 585 | isolate->StackOverflow(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 586 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 587 | return result; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 588 | #endif // V8_INTERPRETED_REGEXP |
| 589 | } |
| 590 | |
| 591 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 592 | MaybeHandle<Object> RegExpImpl::IrregexpExec(Handle<JSRegExp> regexp, |
| 593 | Handle<String> subject, |
| 594 | int previous_index, |
| 595 | Handle<JSArray> last_match_info) { |
| 596 | Isolate* isolate = regexp->GetIsolate(); |
| 597 | DCHECK_EQ(regexp->TypeTag(), JSRegExp::IRREGEXP); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 598 | |
| 599 | // Prepare space for the return values. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 600 | #if defined(V8_INTERPRETED_REGEXP) && defined(DEBUG) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 601 | if (FLAG_trace_regexp_bytecodes) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 602 | String* pattern = regexp->Pattern(); |
| 603 | PrintF("\n\nRegexp match: /%s/\n\n", pattern->ToCString().get()); |
| 604 | PrintF("\n\nSubject string: '%s'\n\n", subject->ToCString().get()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 605 | } |
| 606 | #endif |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 607 | int required_registers = RegExpImpl::IrregexpPrepare(regexp, subject); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 608 | if (required_registers < 0) { |
| 609 | // Compiling failed with an exception. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 610 | DCHECK(isolate->has_pending_exception()); |
| 611 | return MaybeHandle<Object>(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 612 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 613 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 614 | int32_t* output_registers = NULL; |
| 615 | if (required_registers > Isolate::kJSRegexpStaticOffsetsVectorSize) { |
| 616 | output_registers = NewArray<int32_t>(required_registers); |
| 617 | } |
| 618 | SmartArrayPointer<int32_t> auto_release(output_registers); |
| 619 | if (output_registers == NULL) { |
| 620 | output_registers = isolate->jsregexp_static_offsets_vector(); |
| 621 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 622 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 623 | int res = RegExpImpl::IrregexpExecRaw( |
| 624 | regexp, subject, previous_index, output_registers, required_registers); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 625 | if (res == RE_SUCCESS) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 626 | int capture_count = |
| 627 | IrregexpNumberOfCaptures(FixedArray::cast(regexp->data())); |
| 628 | return SetLastMatchInfo( |
| 629 | last_match_info, subject, capture_count, output_registers); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 630 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 631 | if (res == RE_EXCEPTION) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 632 | DCHECK(isolate->has_pending_exception()); |
| 633 | return MaybeHandle<Object>(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 634 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 635 | DCHECK(res == RE_FAILURE); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 636 | return isolate->factory()->null_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 640 | Handle<JSArray> RegExpImpl::SetLastMatchInfo(Handle<JSArray> last_match_info, |
| 641 | Handle<String> subject, |
| 642 | int capture_count, |
| 643 | int32_t* match) { |
| 644 | DCHECK(last_match_info->HasFastObjectElements()); |
| 645 | int capture_register_count = (capture_count + 1) * 2; |
| 646 | JSArray::EnsureSize(last_match_info, |
| 647 | capture_register_count + kLastMatchOverhead); |
| 648 | DisallowHeapAllocation no_allocation; |
| 649 | FixedArray* array = FixedArray::cast(last_match_info->elements()); |
| 650 | if (match != NULL) { |
| 651 | for (int i = 0; i < capture_register_count; i += 2) { |
| 652 | SetCapture(array, i, match[i]); |
| 653 | SetCapture(array, i + 1, match[i + 1]); |
| 654 | } |
| 655 | } |
| 656 | SetLastCaptureCount(array, capture_register_count); |
| 657 | SetLastSubject(array, *subject); |
| 658 | SetLastInput(array, *subject); |
| 659 | return last_match_info; |
| 660 | } |
| 661 | |
| 662 | |
| 663 | RegExpImpl::GlobalCache::GlobalCache(Handle<JSRegExp> regexp, |
| 664 | Handle<String> subject, |
| 665 | bool is_global, |
| 666 | Isolate* isolate) |
| 667 | : register_array_(NULL), |
| 668 | register_array_size_(0), |
| 669 | regexp_(regexp), |
| 670 | subject_(subject) { |
| 671 | #ifdef V8_INTERPRETED_REGEXP |
| 672 | bool interpreted = true; |
| 673 | #else |
| 674 | bool interpreted = false; |
| 675 | #endif // V8_INTERPRETED_REGEXP |
| 676 | |
| 677 | if (regexp_->TypeTag() == JSRegExp::ATOM) { |
| 678 | static const int kAtomRegistersPerMatch = 2; |
| 679 | registers_per_match_ = kAtomRegistersPerMatch; |
| 680 | // There is no distinction between interpreted and native for atom regexps. |
| 681 | interpreted = false; |
| 682 | } else { |
| 683 | registers_per_match_ = RegExpImpl::IrregexpPrepare(regexp_, subject_); |
| 684 | if (registers_per_match_ < 0) { |
| 685 | num_matches_ = -1; // Signal exception. |
| 686 | return; |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | if (is_global && !interpreted) { |
| 691 | register_array_size_ = |
| 692 | Max(registers_per_match_, Isolate::kJSRegexpStaticOffsetsVectorSize); |
| 693 | max_matches_ = register_array_size_ / registers_per_match_; |
| 694 | } else { |
| 695 | // Global loop in interpreted regexp is not implemented. We choose |
| 696 | // the size of the offsets vector so that it can only store one match. |
| 697 | register_array_size_ = registers_per_match_; |
| 698 | max_matches_ = 1; |
| 699 | } |
| 700 | |
| 701 | if (register_array_size_ > Isolate::kJSRegexpStaticOffsetsVectorSize) { |
| 702 | register_array_ = NewArray<int32_t>(register_array_size_); |
| 703 | } else { |
| 704 | register_array_ = isolate->jsregexp_static_offsets_vector(); |
| 705 | } |
| 706 | |
| 707 | // Set state so that fetching the results the first time triggers a call |
| 708 | // to the compiled regexp. |
| 709 | current_match_index_ = max_matches_ - 1; |
| 710 | num_matches_ = max_matches_; |
| 711 | DCHECK(registers_per_match_ >= 2); // Each match has at least one capture. |
| 712 | DCHECK_GE(register_array_size_, registers_per_match_); |
| 713 | int32_t* last_match = |
| 714 | ®ister_array_[current_match_index_ * registers_per_match_]; |
| 715 | last_match[0] = -1; |
| 716 | last_match[1] = 0; |
| 717 | } |
| 718 | |
| 719 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 720 | // ------------------------------------------------------------------- |
| 721 | // Implementation of the Irregexp regular expression engine. |
| 722 | // |
| 723 | // The Irregexp regular expression engine is intended to be a complete |
| 724 | // implementation of ECMAScript regular expressions. It generates either |
| 725 | // bytecodes or native code. |
| 726 | |
| 727 | // The Irregexp regexp engine is structured in three steps. |
| 728 | // 1) The parser generates an abstract syntax tree. See ast.cc. |
| 729 | // 2) From the AST a node network is created. The nodes are all |
| 730 | // subclasses of RegExpNode. The nodes represent states when |
| 731 | // executing a regular expression. Several optimizations are |
| 732 | // performed on the node network. |
| 733 | // 3) From the nodes we generate either byte codes or native code |
| 734 | // that can actually execute the regular expression (perform |
| 735 | // the search). The code generation step is described in more |
| 736 | // detail below. |
| 737 | |
| 738 | // Code generation. |
| 739 | // |
| 740 | // The nodes are divided into four main categories. |
| 741 | // * Choice nodes |
| 742 | // These represent places where the regular expression can |
| 743 | // match in more than one way. For example on entry to an |
| 744 | // alternation (foo|bar) or a repetition (*, +, ? or {}). |
| 745 | // * Action nodes |
| 746 | // These represent places where some action should be |
| 747 | // performed. Examples include recording the current position |
| 748 | // in the input string to a register (in order to implement |
| 749 | // captures) or other actions on register for example in order |
| 750 | // to implement the counters needed for {} repetitions. |
| 751 | // * Matching nodes |
| 752 | // These attempt to match some element part of the input string. |
| 753 | // Examples of elements include character classes, plain strings |
| 754 | // or back references. |
| 755 | // * End nodes |
| 756 | // These are used to implement the actions required on finding |
| 757 | // a successful match or failing to find a match. |
| 758 | // |
| 759 | // The code generated (whether as byte codes or native code) maintains |
| 760 | // some state as it runs. This consists of the following elements: |
| 761 | // |
| 762 | // * The capture registers. Used for string captures. |
| 763 | // * Other registers. Used for counters etc. |
| 764 | // * The current position. |
| 765 | // * The stack of backtracking information. Used when a matching node |
| 766 | // fails to find a match and needs to try an alternative. |
| 767 | // |
| 768 | // Conceptual regular expression execution model: |
| 769 | // |
| 770 | // There is a simple conceptual model of regular expression execution |
| 771 | // which will be presented first. The actual code generated is a more |
| 772 | // efficient simulation of the simple conceptual model: |
| 773 | // |
| 774 | // * Choice nodes are implemented as follows: |
| 775 | // For each choice except the last { |
| 776 | // push current position |
| 777 | // push backtrack code location |
| 778 | // <generate code to test for choice> |
| 779 | // backtrack code location: |
| 780 | // pop current position |
| 781 | // } |
| 782 | // <generate code to test for last choice> |
| 783 | // |
| 784 | // * Actions nodes are generated as follows |
| 785 | // <push affected registers on backtrack stack> |
| 786 | // <generate code to perform action> |
| 787 | // push backtrack code location |
| 788 | // <generate code to test for following nodes> |
| 789 | // backtrack code location: |
| 790 | // <pop affected registers to restore their state> |
| 791 | // <pop backtrack location from stack and go to it> |
| 792 | // |
| 793 | // * Matching nodes are generated as follows: |
| 794 | // if input string matches at current position |
| 795 | // update current position |
| 796 | // <generate code to test for following nodes> |
| 797 | // else |
| 798 | // <pop backtrack location from stack and go to it> |
| 799 | // |
| 800 | // Thus it can be seen that the current position is saved and restored |
| 801 | // by the choice nodes, whereas the registers are saved and restored by |
| 802 | // by the action nodes that manipulate them. |
| 803 | // |
| 804 | // The other interesting aspect of this model is that nodes are generated |
| 805 | // at the point where they are needed by a recursive call to Emit(). If |
| 806 | // the node has already been code generated then the Emit() call will |
| 807 | // generate a jump to the previously generated code instead. In order to |
| 808 | // limit recursion it is possible for the Emit() function to put the node |
| 809 | // on a work list for later generation and instead generate a jump. The |
| 810 | // destination of the jump is resolved later when the code is generated. |
| 811 | // |
| 812 | // Actual regular expression code generation. |
| 813 | // |
| 814 | // Code generation is actually more complicated than the above. In order |
| 815 | // to improve the efficiency of the generated code some optimizations are |
| 816 | // performed |
| 817 | // |
| 818 | // * Choice nodes have 1-character lookahead. |
| 819 | // A choice node looks at the following character and eliminates some of |
| 820 | // the choices immediately based on that character. This is not yet |
| 821 | // implemented. |
| 822 | // * Simple greedy loops store reduced backtracking information. |
| 823 | // A quantifier like /.*foo/m will greedily match the whole input. It will |
| 824 | // then need to backtrack to a point where it can match "foo". The naive |
| 825 | // implementation of this would push each character position onto the |
| 826 | // backtracking stack, then pop them off one by one. This would use space |
| 827 | // proportional to the length of the input string. However since the "." |
| 828 | // can only match in one way and always has a constant length (in this case |
| 829 | // of 1) it suffices to store the current position on the top of the stack |
| 830 | // once. Matching now becomes merely incrementing the current position and |
| 831 | // backtracking becomes decrementing the current position and checking the |
| 832 | // result against the stored current position. This is faster and saves |
| 833 | // space. |
| 834 | // * The current state is virtualized. |
| 835 | // This is used to defer expensive operations until it is clear that they |
| 836 | // are needed and to generate code for a node more than once, allowing |
| 837 | // specialized an efficient versions of the code to be created. This is |
| 838 | // explained in the section below. |
| 839 | // |
| 840 | // Execution state virtualization. |
| 841 | // |
| 842 | // Instead of emitting code, nodes that manipulate the state can record their |
| 843 | // manipulation in an object called the Trace. The Trace object can record a |
| 844 | // current position offset, an optional backtrack code location on the top of |
| 845 | // the virtualized backtrack stack and some register changes. When a node is |
| 846 | // to be emitted it can flush the Trace or update it. Flushing the Trace |
| 847 | // will emit code to bring the actual state into line with the virtual state. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 848 | // Avoiding flushing the state can postpone some work (e.g. updates of capture |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 849 | // registers). Postponing work can save time when executing the regular |
| 850 | // expression since it may be found that the work never has to be done as a |
| 851 | // failure to match can occur. In addition it is much faster to jump to a |
| 852 | // known backtrack code location than it is to pop an unknown backtrack |
| 853 | // location from the stack and jump there. |
| 854 | // |
| 855 | // The virtual state found in the Trace affects code generation. For example |
| 856 | // the virtual state contains the difference between the actual current |
| 857 | // position and the virtual current position, and matching code needs to use |
| 858 | // this offset to attempt a match in the correct location of the input |
| 859 | // string. Therefore code generated for a non-trivial trace is specialized |
| 860 | // to that trace. The code generator therefore has the ability to generate |
| 861 | // code for each node several times. In order to limit the size of the |
| 862 | // generated code there is an arbitrary limit on how many specialized sets of |
| 863 | // code may be generated for a given node. If the limit is reached, the |
| 864 | // trace is flushed and a generic version of the code for a node is emitted. |
| 865 | // This is subsequently used for that node. The code emitted for non-generic |
| 866 | // trace is not recorded in the node and so it cannot currently be reused in |
| 867 | // the event that code generation is requested for an identical trace. |
| 868 | |
| 869 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 870 | void RegExpTree::AppendToText(RegExpText* text, Zone* zone) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 871 | UNREACHABLE(); |
| 872 | } |
| 873 | |
| 874 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 875 | void RegExpAtom::AppendToText(RegExpText* text, Zone* zone) { |
| 876 | text->AddElement(TextElement::Atom(this), zone); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 877 | } |
| 878 | |
| 879 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 880 | void RegExpCharacterClass::AppendToText(RegExpText* text, Zone* zone) { |
| 881 | text->AddElement(TextElement::CharClass(this), zone); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 882 | } |
| 883 | |
| 884 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 885 | void RegExpText::AppendToText(RegExpText* text, Zone* zone) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 886 | for (int i = 0; i < elements()->length(); i++) |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 887 | text->AddElement(elements()->at(i), zone); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | |
| 891 | TextElement TextElement::Atom(RegExpAtom* atom) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 892 | return TextElement(ATOM, atom); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 896 | TextElement TextElement::CharClass(RegExpCharacterClass* char_class) { |
| 897 | return TextElement(CHAR_CLASS, char_class); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 901 | int TextElement::length() const { |
| 902 | switch (text_type()) { |
| 903 | case ATOM: |
| 904 | return atom()->length(); |
| 905 | |
| 906 | case CHAR_CLASS: |
| 907 | return 1; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 908 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 909 | UNREACHABLE(); |
| 910 | return 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | |
| 914 | DispatchTable* ChoiceNode::GetTable(bool ignore_case) { |
| 915 | if (table_ == NULL) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 916 | table_ = new(zone()) DispatchTable(zone()); |
| 917 | DispatchTableConstructor cons(table_, ignore_case, zone()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 918 | cons.BuildTable(this); |
| 919 | } |
| 920 | return table_; |
| 921 | } |
| 922 | |
| 923 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 924 | class FrequencyCollator { |
| 925 | public: |
| 926 | FrequencyCollator() : total_samples_(0) { |
| 927 | for (int i = 0; i < RegExpMacroAssembler::kTableSize; i++) { |
| 928 | frequencies_[i] = CharacterFrequency(i); |
| 929 | } |
| 930 | } |
| 931 | |
| 932 | void CountCharacter(int character) { |
| 933 | int index = (character & RegExpMacroAssembler::kTableMask); |
| 934 | frequencies_[index].Increment(); |
| 935 | total_samples_++; |
| 936 | } |
| 937 | |
| 938 | // Does not measure in percent, but rather per-128 (the table size from the |
| 939 | // regexp macro assembler). |
| 940 | int Frequency(int in_character) { |
| 941 | DCHECK((in_character & RegExpMacroAssembler::kTableMask) == in_character); |
| 942 | if (total_samples_ < 1) return 1; // Division by zero. |
| 943 | int freq_in_per128 = |
| 944 | (frequencies_[in_character].counter() * 128) / total_samples_; |
| 945 | return freq_in_per128; |
| 946 | } |
| 947 | |
| 948 | private: |
| 949 | class CharacterFrequency { |
| 950 | public: |
| 951 | CharacterFrequency() : counter_(0), character_(-1) { } |
| 952 | explicit CharacterFrequency(int character) |
| 953 | : counter_(0), character_(character) { } |
| 954 | |
| 955 | void Increment() { counter_++; } |
| 956 | int counter() { return counter_; } |
| 957 | int character() { return character_; } |
| 958 | |
| 959 | private: |
| 960 | int counter_; |
| 961 | int character_; |
| 962 | }; |
| 963 | |
| 964 | |
| 965 | private: |
| 966 | CharacterFrequency frequencies_[RegExpMacroAssembler::kTableSize]; |
| 967 | int total_samples_; |
| 968 | }; |
| 969 | |
| 970 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 971 | class RegExpCompiler { |
| 972 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 973 | RegExpCompiler(int capture_count, bool ignore_case, bool is_one_byte, |
| 974 | Zone* zone); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 975 | |
| 976 | int AllocateRegister() { |
| 977 | if (next_register_ >= RegExpMacroAssembler::kMaxRegister) { |
| 978 | reg_exp_too_big_ = true; |
| 979 | return next_register_; |
| 980 | } |
| 981 | return next_register_++; |
| 982 | } |
| 983 | |
| 984 | RegExpEngine::CompilationResult Assemble(RegExpMacroAssembler* assembler, |
| 985 | RegExpNode* start, |
| 986 | int capture_count, |
| 987 | Handle<String> pattern); |
| 988 | |
| 989 | inline void AddWork(RegExpNode* node) { work_list_->Add(node); } |
| 990 | |
| 991 | static const int kImplementationOffset = 0; |
| 992 | static const int kNumberOfRegistersOffset = 0; |
| 993 | static const int kCodeOffset = 1; |
| 994 | |
| 995 | RegExpMacroAssembler* macro_assembler() { return macro_assembler_; } |
| 996 | EndNode* accept() { return accept_; } |
| 997 | |
| 998 | static const int kMaxRecursion = 100; |
| 999 | inline int recursion_depth() { return recursion_depth_; } |
| 1000 | inline void IncrementRecursionDepth() { recursion_depth_++; } |
| 1001 | inline void DecrementRecursionDepth() { recursion_depth_--; } |
| 1002 | |
| 1003 | void SetRegExpTooBig() { reg_exp_too_big_ = true; } |
| 1004 | |
| 1005 | inline bool ignore_case() { return ignore_case_; } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1006 | inline bool one_byte() { return one_byte_; } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 1007 | inline bool optimize() { return optimize_; } |
| 1008 | inline void set_optimize(bool value) { optimize_ = value; } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1009 | FrequencyCollator* frequency_collator() { return &frequency_collator_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1010 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1011 | int current_expansion_factor() { return current_expansion_factor_; } |
| 1012 | void set_current_expansion_factor(int value) { |
| 1013 | current_expansion_factor_ = value; |
| 1014 | } |
| 1015 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1016 | Zone* zone() const { return zone_; } |
| 1017 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1018 | static const int kNoRegister = -1; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1019 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1020 | private: |
| 1021 | EndNode* accept_; |
| 1022 | int next_register_; |
| 1023 | List<RegExpNode*>* work_list_; |
| 1024 | int recursion_depth_; |
| 1025 | RegExpMacroAssembler* macro_assembler_; |
| 1026 | bool ignore_case_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1027 | bool one_byte_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1028 | bool reg_exp_too_big_; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 1029 | bool optimize_; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1030 | int current_expansion_factor_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1031 | FrequencyCollator frequency_collator_; |
| 1032 | Zone* zone_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1033 | }; |
| 1034 | |
| 1035 | |
| 1036 | class RecursionCheck { |
| 1037 | public: |
| 1038 | explicit RecursionCheck(RegExpCompiler* compiler) : compiler_(compiler) { |
| 1039 | compiler->IncrementRecursionDepth(); |
| 1040 | } |
| 1041 | ~RecursionCheck() { compiler_->DecrementRecursionDepth(); } |
| 1042 | private: |
| 1043 | RegExpCompiler* compiler_; |
| 1044 | }; |
| 1045 | |
| 1046 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1047 | static RegExpEngine::CompilationResult IrregexpRegExpTooBig(Isolate* isolate) { |
| 1048 | return RegExpEngine::CompilationResult(isolate, "RegExp too big"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | |
| 1052 | // Attempts to compile the regexp using an Irregexp code generator. Returns |
| 1053 | // a fixed array or a null handle depending on whether it succeeded. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1054 | RegExpCompiler::RegExpCompiler(int capture_count, bool ignore_case, |
| 1055 | bool one_byte, Zone* zone) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1056 | : next_register_(2 * (capture_count + 1)), |
| 1057 | work_list_(NULL), |
| 1058 | recursion_depth_(0), |
| 1059 | ignore_case_(ignore_case), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1060 | one_byte_(one_byte), |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1061 | reg_exp_too_big_(false), |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 1062 | optimize_(FLAG_regexp_optimization), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1063 | current_expansion_factor_(1), |
| 1064 | frequency_collator_(), |
| 1065 | zone_(zone) { |
| 1066 | accept_ = new(zone) EndNode(EndNode::ACCEPT, zone); |
| 1067 | DCHECK(next_register_ - 1 <= RegExpMacroAssembler::kMaxRegister); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1068 | } |
| 1069 | |
| 1070 | |
| 1071 | RegExpEngine::CompilationResult RegExpCompiler::Assemble( |
| 1072 | RegExpMacroAssembler* macro_assembler, |
| 1073 | RegExpNode* start, |
| 1074 | int capture_count, |
| 1075 | Handle<String> pattern) { |
Steve Block | 053d10c | 2011-06-13 19:13:29 +0100 | [diff] [blame] | 1076 | Heap* heap = pattern->GetHeap(); |
| 1077 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1078 | #ifdef DEBUG |
| 1079 | if (FLAG_trace_regexp_assembler) |
| 1080 | macro_assembler_ = new RegExpMacroAssemblerTracer(macro_assembler); |
| 1081 | else |
| 1082 | #endif |
| 1083 | macro_assembler_ = macro_assembler; |
Steve Block | 053d10c | 2011-06-13 19:13:29 +0100 | [diff] [blame] | 1084 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1085 | List <RegExpNode*> work_list(0); |
| 1086 | work_list_ = &work_list; |
| 1087 | Label fail; |
| 1088 | macro_assembler_->PushBacktrack(&fail); |
| 1089 | Trace new_trace; |
| 1090 | start->Emit(this, &new_trace); |
| 1091 | macro_assembler_->Bind(&fail); |
| 1092 | macro_assembler_->Fail(); |
| 1093 | while (!work_list.is_empty()) { |
| 1094 | work_list.RemoveLast()->Emit(this, &new_trace); |
| 1095 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1096 | if (reg_exp_too_big_) return IrregexpRegExpTooBig(zone_->isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1097 | |
Steve Block | 053d10c | 2011-06-13 19:13:29 +0100 | [diff] [blame] | 1098 | Handle<HeapObject> code = macro_assembler_->GetCode(pattern); |
| 1099 | heap->IncreaseTotalRegexpCodeGenerated(code->Size()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1100 | work_list_ = NULL; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 1101 | #ifdef ENABLE_DISASSEMBLER |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1102 | if (FLAG_print_code) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1103 | CodeTracer::Scope trace_scope(heap->isolate()->GetCodeTracer()); |
| 1104 | OFStream os(trace_scope.file()); |
| 1105 | Handle<Code>::cast(code)->Disassemble(pattern->ToCString().get(), os); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1106 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 1107 | #endif |
| 1108 | #ifdef DEBUG |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1109 | if (FLAG_trace_regexp_assembler) { |
| 1110 | delete macro_assembler_; |
| 1111 | } |
| 1112 | #endif |
| 1113 | return RegExpEngine::CompilationResult(*code, next_register_); |
| 1114 | } |
| 1115 | |
| 1116 | |
| 1117 | bool Trace::DeferredAction::Mentions(int that) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1118 | if (action_type() == ActionNode::CLEAR_CAPTURES) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1119 | Interval range = static_cast<DeferredClearCaptures*>(this)->range(); |
| 1120 | return range.Contains(that); |
| 1121 | } else { |
| 1122 | return reg() == that; |
| 1123 | } |
| 1124 | } |
| 1125 | |
| 1126 | |
| 1127 | bool Trace::mentions_reg(int reg) { |
| 1128 | for (DeferredAction* action = actions_; |
| 1129 | action != NULL; |
| 1130 | action = action->next()) { |
| 1131 | if (action->Mentions(reg)) |
| 1132 | return true; |
| 1133 | } |
| 1134 | return false; |
| 1135 | } |
| 1136 | |
| 1137 | |
| 1138 | bool Trace::GetStoredPosition(int reg, int* cp_offset) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1139 | DCHECK_EQ(0, *cp_offset); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1140 | for (DeferredAction* action = actions_; |
| 1141 | action != NULL; |
| 1142 | action = action->next()) { |
| 1143 | if (action->Mentions(reg)) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1144 | if (action->action_type() == ActionNode::STORE_POSITION) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1145 | *cp_offset = static_cast<DeferredCapture*>(action)->cp_offset(); |
| 1146 | return true; |
| 1147 | } else { |
| 1148 | return false; |
| 1149 | } |
| 1150 | } |
| 1151 | } |
| 1152 | return false; |
| 1153 | } |
| 1154 | |
| 1155 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1156 | int Trace::FindAffectedRegisters(OutSet* affected_registers, |
| 1157 | Zone* zone) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1158 | int max_register = RegExpCompiler::kNoRegister; |
| 1159 | for (DeferredAction* action = actions_; |
| 1160 | action != NULL; |
| 1161 | action = action->next()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1162 | if (action->action_type() == ActionNode::CLEAR_CAPTURES) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1163 | Interval range = static_cast<DeferredClearCaptures*>(action)->range(); |
| 1164 | for (int i = range.from(); i <= range.to(); i++) |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1165 | affected_registers->Set(i, zone); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1166 | if (range.to() > max_register) max_register = range.to(); |
| 1167 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1168 | affected_registers->Set(action->reg(), zone); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1169 | if (action->reg() > max_register) max_register = action->reg(); |
| 1170 | } |
| 1171 | } |
| 1172 | return max_register; |
| 1173 | } |
| 1174 | |
| 1175 | |
| 1176 | void Trace::RestoreAffectedRegisters(RegExpMacroAssembler* assembler, |
| 1177 | int max_register, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1178 | const OutSet& registers_to_pop, |
| 1179 | const OutSet& registers_to_clear) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1180 | for (int reg = max_register; reg >= 0; reg--) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1181 | if (registers_to_pop.Get(reg)) { |
| 1182 | assembler->PopRegister(reg); |
| 1183 | } else if (registers_to_clear.Get(reg)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1184 | int clear_to = reg; |
| 1185 | while (reg > 0 && registers_to_clear.Get(reg - 1)) { |
| 1186 | reg--; |
| 1187 | } |
| 1188 | assembler->ClearRegisters(reg, clear_to); |
| 1189 | } |
| 1190 | } |
| 1191 | } |
| 1192 | |
| 1193 | |
| 1194 | void Trace::PerformDeferredActions(RegExpMacroAssembler* assembler, |
| 1195 | int max_register, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1196 | const OutSet& affected_registers, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1197 | OutSet* registers_to_pop, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1198 | OutSet* registers_to_clear, |
| 1199 | Zone* zone) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1200 | // The "+1" is to avoid a push_limit of zero if stack_limit_slack() is 1. |
| 1201 | const int push_limit = (assembler->stack_limit_slack() + 1) / 2; |
| 1202 | |
| 1203 | // Count pushes performed to force a stack limit check occasionally. |
| 1204 | int pushes = 0; |
| 1205 | |
| 1206 | for (int reg = 0; reg <= max_register; reg++) { |
| 1207 | if (!affected_registers.Get(reg)) { |
| 1208 | continue; |
| 1209 | } |
| 1210 | |
| 1211 | // The chronologically first deferred action in the trace |
| 1212 | // is used to infer the action needed to restore a register |
| 1213 | // to its previous state (or not, if it's safe to ignore it). |
| 1214 | enum DeferredActionUndoType { IGNORE, RESTORE, CLEAR }; |
| 1215 | DeferredActionUndoType undo_action = IGNORE; |
| 1216 | |
| 1217 | int value = 0; |
| 1218 | bool absolute = false; |
| 1219 | bool clear = false; |
| 1220 | int store_position = -1; |
| 1221 | // This is a little tricky because we are scanning the actions in reverse |
| 1222 | // historical order (newest first). |
| 1223 | for (DeferredAction* action = actions_; |
| 1224 | action != NULL; |
| 1225 | action = action->next()) { |
| 1226 | if (action->Mentions(reg)) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1227 | switch (action->action_type()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1228 | case ActionNode::SET_REGISTER: { |
| 1229 | Trace::DeferredSetRegister* psr = |
| 1230 | static_cast<Trace::DeferredSetRegister*>(action); |
| 1231 | if (!absolute) { |
| 1232 | value += psr->value(); |
| 1233 | absolute = true; |
| 1234 | } |
| 1235 | // SET_REGISTER is currently only used for newly introduced loop |
| 1236 | // counters. They can have a significant previous value if they |
| 1237 | // occour in a loop. TODO(lrn): Propagate this information, so |
| 1238 | // we can set undo_action to IGNORE if we know there is no value to |
| 1239 | // restore. |
| 1240 | undo_action = RESTORE; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1241 | DCHECK_EQ(store_position, -1); |
| 1242 | DCHECK(!clear); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1243 | break; |
| 1244 | } |
| 1245 | case ActionNode::INCREMENT_REGISTER: |
| 1246 | if (!absolute) { |
| 1247 | value++; |
| 1248 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1249 | DCHECK_EQ(store_position, -1); |
| 1250 | DCHECK(!clear); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1251 | undo_action = RESTORE; |
| 1252 | break; |
| 1253 | case ActionNode::STORE_POSITION: { |
| 1254 | Trace::DeferredCapture* pc = |
| 1255 | static_cast<Trace::DeferredCapture*>(action); |
| 1256 | if (!clear && store_position == -1) { |
| 1257 | store_position = pc->cp_offset(); |
| 1258 | } |
| 1259 | |
| 1260 | // For captures we know that stores and clears alternate. |
| 1261 | // Other register, are never cleared, and if the occur |
| 1262 | // inside a loop, they might be assigned more than once. |
| 1263 | if (reg <= 1) { |
| 1264 | // Registers zero and one, aka "capture zero", is |
| 1265 | // always set correctly if we succeed. There is no |
| 1266 | // need to undo a setting on backtrack, because we |
| 1267 | // will set it again or fail. |
| 1268 | undo_action = IGNORE; |
| 1269 | } else { |
| 1270 | undo_action = pc->is_capture() ? CLEAR : RESTORE; |
| 1271 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1272 | DCHECK(!absolute); |
| 1273 | DCHECK_EQ(value, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1274 | break; |
| 1275 | } |
| 1276 | case ActionNode::CLEAR_CAPTURES: { |
| 1277 | // Since we're scanning in reverse order, if we've already |
| 1278 | // set the position we have to ignore historically earlier |
| 1279 | // clearing operations. |
| 1280 | if (store_position == -1) { |
| 1281 | clear = true; |
| 1282 | } |
| 1283 | undo_action = RESTORE; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1284 | DCHECK(!absolute); |
| 1285 | DCHECK_EQ(value, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1286 | break; |
| 1287 | } |
| 1288 | default: |
| 1289 | UNREACHABLE(); |
| 1290 | break; |
| 1291 | } |
| 1292 | } |
| 1293 | } |
| 1294 | // Prepare for the undo-action (e.g., push if it's going to be popped). |
| 1295 | if (undo_action == RESTORE) { |
| 1296 | pushes++; |
| 1297 | RegExpMacroAssembler::StackCheckFlag stack_check = |
| 1298 | RegExpMacroAssembler::kNoStackLimitCheck; |
| 1299 | if (pushes == push_limit) { |
| 1300 | stack_check = RegExpMacroAssembler::kCheckStackLimit; |
| 1301 | pushes = 0; |
| 1302 | } |
| 1303 | |
| 1304 | assembler->PushRegister(reg, stack_check); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1305 | registers_to_pop->Set(reg, zone); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1306 | } else if (undo_action == CLEAR) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1307 | registers_to_clear->Set(reg, zone); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1308 | } |
| 1309 | // Perform the chronologically last action (or accumulated increment) |
| 1310 | // for the register. |
| 1311 | if (store_position != -1) { |
| 1312 | assembler->WriteCurrentPositionToRegister(reg, store_position); |
| 1313 | } else if (clear) { |
| 1314 | assembler->ClearRegisters(reg, reg); |
| 1315 | } else if (absolute) { |
| 1316 | assembler->SetRegister(reg, value); |
| 1317 | } else if (value != 0) { |
| 1318 | assembler->AdvanceRegister(reg, value); |
| 1319 | } |
| 1320 | } |
| 1321 | } |
| 1322 | |
| 1323 | |
| 1324 | // This is called as we come into a loop choice node and some other tricky |
| 1325 | // nodes. It normalizes the state of the code generator to ensure we can |
| 1326 | // generate generic code. |
| 1327 | void Trace::Flush(RegExpCompiler* compiler, RegExpNode* successor) { |
| 1328 | RegExpMacroAssembler* assembler = compiler->macro_assembler(); |
| 1329 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1330 | DCHECK(!is_trivial()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1331 | |
| 1332 | if (actions_ == NULL && backtrack() == NULL) { |
| 1333 | // Here we just have some deferred cp advances to fix and we are back to |
| 1334 | // a normal situation. We may also have to forget some information gained |
| 1335 | // through a quick check that was already performed. |
| 1336 | if (cp_offset_ != 0) assembler->AdvanceCurrentPosition(cp_offset_); |
| 1337 | // Create a new trivial state and generate the node with that. |
| 1338 | Trace new_state; |
| 1339 | successor->Emit(compiler, &new_state); |
| 1340 | return; |
| 1341 | } |
| 1342 | |
| 1343 | // Generate deferred actions here along with code to undo them again. |
| 1344 | OutSet affected_registers; |
| 1345 | |
| 1346 | if (backtrack() != NULL) { |
| 1347 | // Here we have a concrete backtrack location. These are set up by choice |
| 1348 | // nodes and so they indicate that we have a deferred save of the current |
| 1349 | // position which we may need to emit here. |
| 1350 | assembler->PushCurrentPosition(); |
| 1351 | } |
| 1352 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1353 | int max_register = FindAffectedRegisters(&affected_registers, |
| 1354 | compiler->zone()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1355 | OutSet registers_to_pop; |
| 1356 | OutSet registers_to_clear; |
| 1357 | PerformDeferredActions(assembler, |
| 1358 | max_register, |
| 1359 | affected_registers, |
| 1360 | ®isters_to_pop, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1361 | ®isters_to_clear, |
| 1362 | compiler->zone()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1363 | if (cp_offset_ != 0) { |
| 1364 | assembler->AdvanceCurrentPosition(cp_offset_); |
| 1365 | } |
| 1366 | |
| 1367 | // Create a new trivial state and generate the node with that. |
| 1368 | Label undo; |
| 1369 | assembler->PushBacktrack(&undo); |
| 1370 | Trace new_state; |
| 1371 | successor->Emit(compiler, &new_state); |
| 1372 | |
| 1373 | // On backtrack we need to restore state. |
| 1374 | assembler->Bind(&undo); |
| 1375 | RestoreAffectedRegisters(assembler, |
| 1376 | max_register, |
| 1377 | registers_to_pop, |
| 1378 | registers_to_clear); |
| 1379 | if (backtrack() == NULL) { |
| 1380 | assembler->Backtrack(); |
| 1381 | } else { |
| 1382 | assembler->PopCurrentPosition(); |
| 1383 | assembler->GoTo(backtrack()); |
| 1384 | } |
| 1385 | } |
| 1386 | |
| 1387 | |
| 1388 | void NegativeSubmatchSuccess::Emit(RegExpCompiler* compiler, Trace* trace) { |
| 1389 | RegExpMacroAssembler* assembler = compiler->macro_assembler(); |
| 1390 | |
| 1391 | // Omit flushing the trace. We discard the entire stack frame anyway. |
| 1392 | |
| 1393 | if (!label()->is_bound()) { |
| 1394 | // We are completely independent of the trace, since we ignore it, |
| 1395 | // so this code can be used as the generic version. |
| 1396 | assembler->Bind(label()); |
| 1397 | } |
| 1398 | |
| 1399 | // Throw away everything on the backtrack stack since the start |
| 1400 | // of the negative submatch and restore the character position. |
| 1401 | assembler->ReadCurrentPositionFromRegister(current_position_register_); |
| 1402 | assembler->ReadStackPointerFromRegister(stack_pointer_register_); |
| 1403 | if (clear_capture_count_ > 0) { |
| 1404 | // Clear any captures that might have been performed during the success |
| 1405 | // of the body of the negative look-ahead. |
| 1406 | int clear_capture_end = clear_capture_start_ + clear_capture_count_ - 1; |
| 1407 | assembler->ClearRegisters(clear_capture_start_, clear_capture_end); |
| 1408 | } |
| 1409 | // Now that we have unwound the stack we find at the top of the stack the |
| 1410 | // backtrack that the BeginSubmatch node got. |
| 1411 | assembler->Backtrack(); |
| 1412 | } |
| 1413 | |
| 1414 | |
| 1415 | void EndNode::Emit(RegExpCompiler* compiler, Trace* trace) { |
| 1416 | if (!trace->is_trivial()) { |
| 1417 | trace->Flush(compiler, this); |
| 1418 | return; |
| 1419 | } |
| 1420 | RegExpMacroAssembler* assembler = compiler->macro_assembler(); |
| 1421 | if (!label()->is_bound()) { |
| 1422 | assembler->Bind(label()); |
| 1423 | } |
| 1424 | switch (action_) { |
| 1425 | case ACCEPT: |
| 1426 | assembler->Succeed(); |
| 1427 | return; |
| 1428 | case BACKTRACK: |
| 1429 | assembler->GoTo(trace->backtrack()); |
| 1430 | return; |
| 1431 | case NEGATIVE_SUBMATCH_SUCCESS: |
| 1432 | // This case is handled in a different virtual method. |
| 1433 | UNREACHABLE(); |
| 1434 | } |
| 1435 | UNIMPLEMENTED(); |
| 1436 | } |
| 1437 | |
| 1438 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1439 | void GuardedAlternative::AddGuard(Guard* guard, Zone* zone) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1440 | if (guards_ == NULL) |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1441 | guards_ = new(zone) ZoneList<Guard*>(1, zone); |
| 1442 | guards_->Add(guard, zone); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1443 | } |
| 1444 | |
| 1445 | |
| 1446 | ActionNode* ActionNode::SetRegister(int reg, |
| 1447 | int val, |
| 1448 | RegExpNode* on_success) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1449 | ActionNode* result = |
| 1450 | new(on_success->zone()) ActionNode(SET_REGISTER, on_success); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1451 | result->data_.u_store_register.reg = reg; |
| 1452 | result->data_.u_store_register.value = val; |
| 1453 | return result; |
| 1454 | } |
| 1455 | |
| 1456 | |
| 1457 | ActionNode* ActionNode::IncrementRegister(int reg, RegExpNode* on_success) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1458 | ActionNode* result = |
| 1459 | new(on_success->zone()) ActionNode(INCREMENT_REGISTER, on_success); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1460 | result->data_.u_increment_register.reg = reg; |
| 1461 | return result; |
| 1462 | } |
| 1463 | |
| 1464 | |
| 1465 | ActionNode* ActionNode::StorePosition(int reg, |
| 1466 | bool is_capture, |
| 1467 | RegExpNode* on_success) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1468 | ActionNode* result = |
| 1469 | new(on_success->zone()) ActionNode(STORE_POSITION, on_success); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1470 | result->data_.u_position_register.reg = reg; |
| 1471 | result->data_.u_position_register.is_capture = is_capture; |
| 1472 | return result; |
| 1473 | } |
| 1474 | |
| 1475 | |
| 1476 | ActionNode* ActionNode::ClearCaptures(Interval range, |
| 1477 | RegExpNode* on_success) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1478 | ActionNode* result = |
| 1479 | new(on_success->zone()) ActionNode(CLEAR_CAPTURES, on_success); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1480 | result->data_.u_clear_captures.range_from = range.from(); |
| 1481 | result->data_.u_clear_captures.range_to = range.to(); |
| 1482 | return result; |
| 1483 | } |
| 1484 | |
| 1485 | |
| 1486 | ActionNode* ActionNode::BeginSubmatch(int stack_reg, |
| 1487 | int position_reg, |
| 1488 | RegExpNode* on_success) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1489 | ActionNode* result = |
| 1490 | new(on_success->zone()) ActionNode(BEGIN_SUBMATCH, on_success); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1491 | result->data_.u_submatch.stack_pointer_register = stack_reg; |
| 1492 | result->data_.u_submatch.current_position_register = position_reg; |
| 1493 | return result; |
| 1494 | } |
| 1495 | |
| 1496 | |
| 1497 | ActionNode* ActionNode::PositiveSubmatchSuccess(int stack_reg, |
| 1498 | int position_reg, |
| 1499 | int clear_register_count, |
| 1500 | int clear_register_from, |
| 1501 | RegExpNode* on_success) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1502 | ActionNode* result = |
| 1503 | new(on_success->zone()) ActionNode(POSITIVE_SUBMATCH_SUCCESS, on_success); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1504 | result->data_.u_submatch.stack_pointer_register = stack_reg; |
| 1505 | result->data_.u_submatch.current_position_register = position_reg; |
| 1506 | result->data_.u_submatch.clear_register_count = clear_register_count; |
| 1507 | result->data_.u_submatch.clear_register_from = clear_register_from; |
| 1508 | return result; |
| 1509 | } |
| 1510 | |
| 1511 | |
| 1512 | ActionNode* ActionNode::EmptyMatchCheck(int start_register, |
| 1513 | int repetition_register, |
| 1514 | int repetition_limit, |
| 1515 | RegExpNode* on_success) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1516 | ActionNode* result = |
| 1517 | new(on_success->zone()) ActionNode(EMPTY_MATCH_CHECK, on_success); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1518 | result->data_.u_empty_match_check.start_register = start_register; |
| 1519 | result->data_.u_empty_match_check.repetition_register = repetition_register; |
| 1520 | result->data_.u_empty_match_check.repetition_limit = repetition_limit; |
| 1521 | return result; |
| 1522 | } |
| 1523 | |
| 1524 | |
| 1525 | #define DEFINE_ACCEPT(Type) \ |
| 1526 | void Type##Node::Accept(NodeVisitor* visitor) { \ |
| 1527 | visitor->Visit##Type(this); \ |
| 1528 | } |
| 1529 | FOR_EACH_NODE_TYPE(DEFINE_ACCEPT) |
| 1530 | #undef DEFINE_ACCEPT |
| 1531 | |
| 1532 | |
| 1533 | void LoopChoiceNode::Accept(NodeVisitor* visitor) { |
| 1534 | visitor->VisitLoopChoice(this); |
| 1535 | } |
| 1536 | |
| 1537 | |
| 1538 | // ------------------------------------------------------------------- |
| 1539 | // Emit code. |
| 1540 | |
| 1541 | |
| 1542 | void ChoiceNode::GenerateGuard(RegExpMacroAssembler* macro_assembler, |
| 1543 | Guard* guard, |
| 1544 | Trace* trace) { |
| 1545 | switch (guard->op()) { |
| 1546 | case Guard::LT: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1547 | DCHECK(!trace->mentions_reg(guard->reg())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1548 | macro_assembler->IfRegisterGE(guard->reg(), |
| 1549 | guard->value(), |
| 1550 | trace->backtrack()); |
| 1551 | break; |
| 1552 | case Guard::GEQ: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1553 | DCHECK(!trace->mentions_reg(guard->reg())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1554 | macro_assembler->IfRegisterLT(guard->reg(), |
| 1555 | guard->value(), |
| 1556 | trace->backtrack()); |
| 1557 | break; |
| 1558 | } |
| 1559 | } |
| 1560 | |
| 1561 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1562 | // Returns the number of characters in the equivalence class, omitting those |
| 1563 | // that cannot occur in the source string because it is ASCII. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1564 | static int GetCaseIndependentLetters(Isolate* isolate, uc16 character, |
| 1565 | bool one_byte_subject, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1566 | unibrow::uchar* letters) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1567 | int length = |
| 1568 | isolate->jsregexp_uncanonicalize()->get(character, '\0', letters); |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 1569 | // Unibrow returns 0 or 1 for characters where case independence is |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1570 | // trivial. |
| 1571 | if (length == 0) { |
| 1572 | letters[0] = character; |
| 1573 | length = 1; |
| 1574 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1575 | if (!one_byte_subject || character <= String::kMaxOneByteCharCode) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1576 | return length; |
| 1577 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1578 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1579 | // The standard requires that non-ASCII characters cannot have ASCII |
| 1580 | // character codes in their equivalence class. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1581 | // TODO(dcarney): issue 3550 this is not actually true for Latin1 anymore, |
| 1582 | // is it? For example, \u00C5 is equivalent to \u212B. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1583 | return 0; |
| 1584 | } |
| 1585 | |
| 1586 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1587 | static inline bool EmitSimpleCharacter(Isolate* isolate, |
| 1588 | RegExpCompiler* compiler, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1589 | uc16 c, |
| 1590 | Label* on_failure, |
| 1591 | int cp_offset, |
| 1592 | bool check, |
| 1593 | bool preloaded) { |
| 1594 | RegExpMacroAssembler* assembler = compiler->macro_assembler(); |
| 1595 | bool bound_checked = false; |
| 1596 | if (!preloaded) { |
| 1597 | assembler->LoadCurrentCharacter( |
| 1598 | cp_offset, |
| 1599 | on_failure, |
| 1600 | check); |
| 1601 | bound_checked = true; |
| 1602 | } |
| 1603 | assembler->CheckNotCharacter(c, on_failure); |
| 1604 | return bound_checked; |
| 1605 | } |
| 1606 | |
| 1607 | |
| 1608 | // Only emits non-letters (things that don't have case). Only used for case |
| 1609 | // independent matches. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1610 | static inline bool EmitAtomNonLetter(Isolate* isolate, |
| 1611 | RegExpCompiler* compiler, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1612 | uc16 c, |
| 1613 | Label* on_failure, |
| 1614 | int cp_offset, |
| 1615 | bool check, |
| 1616 | bool preloaded) { |
| 1617 | RegExpMacroAssembler* macro_assembler = compiler->macro_assembler(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1618 | bool one_byte = compiler->one_byte(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1619 | unibrow::uchar chars[unibrow::Ecma262UnCanonicalize::kMaxWidth]; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1620 | int length = GetCaseIndependentLetters(isolate, c, one_byte, chars); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1621 | if (length < 1) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1622 | // This can't match. Must be an one-byte subject and a non-one-byte |
| 1623 | // character. We do not need to do anything since the one-byte pass |
| 1624 | // already handled this. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1625 | return false; // Bounds not checked. |
| 1626 | } |
| 1627 | bool checked = false; |
| 1628 | // We handle the length > 1 case in a later pass. |
| 1629 | if (length == 1) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1630 | if (one_byte && c > String::kMaxOneByteCharCodeU) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1631 | // Can't match - see above. |
| 1632 | return false; // Bounds not checked. |
| 1633 | } |
| 1634 | if (!preloaded) { |
| 1635 | macro_assembler->LoadCurrentCharacter(cp_offset, on_failure, check); |
| 1636 | checked = check; |
| 1637 | } |
| 1638 | macro_assembler->CheckNotCharacter(c, on_failure); |
| 1639 | } |
| 1640 | return checked; |
| 1641 | } |
| 1642 | |
| 1643 | |
| 1644 | static bool ShortCutEmitCharacterPair(RegExpMacroAssembler* macro_assembler, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1645 | bool one_byte, uc16 c1, uc16 c2, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1646 | Label* on_failure) { |
| 1647 | uc16 char_mask; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1648 | if (one_byte) { |
| 1649 | char_mask = String::kMaxOneByteCharCode; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1650 | } else { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1651 | char_mask = String::kMaxUtf16CodeUnit; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1652 | } |
| 1653 | uc16 exor = c1 ^ c2; |
| 1654 | // Check whether exor has only one bit set. |
| 1655 | if (((exor - 1) & exor) == 0) { |
| 1656 | // If c1 and c2 differ only by one bit. |
| 1657 | // Ecma262UnCanonicalize always gives the highest number last. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1658 | DCHECK(c2 > c1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1659 | uc16 mask = char_mask ^ exor; |
| 1660 | macro_assembler->CheckNotCharacterAfterAnd(c1, mask, on_failure); |
| 1661 | return true; |
| 1662 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1663 | DCHECK(c2 > c1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1664 | uc16 diff = c2 - c1; |
| 1665 | if (((diff - 1) & diff) == 0 && c1 >= diff) { |
| 1666 | // If the characters differ by 2^n but don't differ by one bit then |
| 1667 | // subtract the difference from the found character, then do the or |
| 1668 | // trick. We avoid the theoretical case where negative numbers are |
| 1669 | // involved in order to simplify code generation. |
| 1670 | uc16 mask = char_mask ^ diff; |
| 1671 | macro_assembler->CheckNotCharacterAfterMinusAnd(c1 - diff, |
| 1672 | diff, |
| 1673 | mask, |
| 1674 | on_failure); |
| 1675 | return true; |
| 1676 | } |
| 1677 | return false; |
| 1678 | } |
| 1679 | |
| 1680 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1681 | typedef bool EmitCharacterFunction(Isolate* isolate, |
| 1682 | RegExpCompiler* compiler, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1683 | uc16 c, |
| 1684 | Label* on_failure, |
| 1685 | int cp_offset, |
| 1686 | bool check, |
| 1687 | bool preloaded); |
| 1688 | |
| 1689 | // Only emits letters (things that have case). Only used for case independent |
| 1690 | // matches. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1691 | static inline bool EmitAtomLetter(Isolate* isolate, |
| 1692 | RegExpCompiler* compiler, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1693 | uc16 c, |
| 1694 | Label* on_failure, |
| 1695 | int cp_offset, |
| 1696 | bool check, |
| 1697 | bool preloaded) { |
| 1698 | RegExpMacroAssembler* macro_assembler = compiler->macro_assembler(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1699 | bool one_byte = compiler->one_byte(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1700 | unibrow::uchar chars[unibrow::Ecma262UnCanonicalize::kMaxWidth]; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1701 | int length = GetCaseIndependentLetters(isolate, c, one_byte, chars); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1702 | if (length <= 1) return false; |
| 1703 | // We may not need to check against the end of the input string |
| 1704 | // if this character lies before a character that matched. |
| 1705 | if (!preloaded) { |
| 1706 | macro_assembler->LoadCurrentCharacter(cp_offset, on_failure, check); |
| 1707 | } |
| 1708 | Label ok; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1709 | DCHECK(unibrow::Ecma262UnCanonicalize::kMaxWidth == 4); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1710 | switch (length) { |
| 1711 | case 2: { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1712 | if (ShortCutEmitCharacterPair(macro_assembler, one_byte, chars[0], |
| 1713 | chars[1], on_failure)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1714 | } else { |
| 1715 | macro_assembler->CheckCharacter(chars[0], &ok); |
| 1716 | macro_assembler->CheckNotCharacter(chars[1], on_failure); |
| 1717 | macro_assembler->Bind(&ok); |
| 1718 | } |
| 1719 | break; |
| 1720 | } |
| 1721 | case 4: |
| 1722 | macro_assembler->CheckCharacter(chars[3], &ok); |
| 1723 | // Fall through! |
| 1724 | case 3: |
| 1725 | macro_assembler->CheckCharacter(chars[0], &ok); |
| 1726 | macro_assembler->CheckCharacter(chars[1], &ok); |
| 1727 | macro_assembler->CheckNotCharacter(chars[2], on_failure); |
| 1728 | macro_assembler->Bind(&ok); |
| 1729 | break; |
| 1730 | default: |
| 1731 | UNREACHABLE(); |
| 1732 | break; |
| 1733 | } |
| 1734 | return true; |
| 1735 | } |
| 1736 | |
| 1737 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1738 | static void EmitBoundaryTest(RegExpMacroAssembler* masm, |
| 1739 | int border, |
| 1740 | Label* fall_through, |
| 1741 | Label* above_or_equal, |
| 1742 | Label* below) { |
| 1743 | if (below != fall_through) { |
| 1744 | masm->CheckCharacterLT(border, below); |
| 1745 | if (above_or_equal != fall_through) masm->GoTo(above_or_equal); |
| 1746 | } else { |
| 1747 | masm->CheckCharacterGT(border - 1, above_or_equal); |
| 1748 | } |
| 1749 | } |
| 1750 | |
| 1751 | |
| 1752 | static void EmitDoubleBoundaryTest(RegExpMacroAssembler* masm, |
| 1753 | int first, |
| 1754 | int last, |
| 1755 | Label* fall_through, |
| 1756 | Label* in_range, |
| 1757 | Label* out_of_range) { |
| 1758 | if (in_range == fall_through) { |
| 1759 | if (first == last) { |
| 1760 | masm->CheckNotCharacter(first, out_of_range); |
| 1761 | } else { |
| 1762 | masm->CheckCharacterNotInRange(first, last, out_of_range); |
| 1763 | } |
| 1764 | } else { |
| 1765 | if (first == last) { |
| 1766 | masm->CheckCharacter(first, in_range); |
| 1767 | } else { |
| 1768 | masm->CheckCharacterInRange(first, last, in_range); |
| 1769 | } |
| 1770 | if (out_of_range != fall_through) masm->GoTo(out_of_range); |
| 1771 | } |
| 1772 | } |
| 1773 | |
| 1774 | |
| 1775 | // even_label is for ranges[i] to ranges[i + 1] where i - start_index is even. |
| 1776 | // odd_label is for ranges[i] to ranges[i + 1] where i - start_index is odd. |
| 1777 | static void EmitUseLookupTable( |
| 1778 | RegExpMacroAssembler* masm, |
| 1779 | ZoneList<int>* ranges, |
| 1780 | int start_index, |
| 1781 | int end_index, |
| 1782 | int min_char, |
| 1783 | Label* fall_through, |
| 1784 | Label* even_label, |
| 1785 | Label* odd_label) { |
| 1786 | static const int kSize = RegExpMacroAssembler::kTableSize; |
| 1787 | static const int kMask = RegExpMacroAssembler::kTableMask; |
| 1788 | |
| 1789 | int base = (min_char & ~kMask); |
| 1790 | USE(base); |
| 1791 | |
| 1792 | // Assert that everything is on one kTableSize page. |
| 1793 | for (int i = start_index; i <= end_index; i++) { |
| 1794 | DCHECK_EQ(ranges->at(i) & ~kMask, base); |
| 1795 | } |
| 1796 | DCHECK(start_index == 0 || (ranges->at(start_index - 1) & ~kMask) <= base); |
| 1797 | |
| 1798 | char templ[kSize]; |
| 1799 | Label* on_bit_set; |
| 1800 | Label* on_bit_clear; |
| 1801 | int bit; |
| 1802 | if (even_label == fall_through) { |
| 1803 | on_bit_set = odd_label; |
| 1804 | on_bit_clear = even_label; |
| 1805 | bit = 1; |
| 1806 | } else { |
| 1807 | on_bit_set = even_label; |
| 1808 | on_bit_clear = odd_label; |
| 1809 | bit = 0; |
| 1810 | } |
| 1811 | for (int i = 0; i < (ranges->at(start_index) & kMask) && i < kSize; i++) { |
| 1812 | templ[i] = bit; |
| 1813 | } |
| 1814 | int j = 0; |
| 1815 | bit ^= 1; |
| 1816 | for (int i = start_index; i < end_index; i++) { |
| 1817 | for (j = (ranges->at(i) & kMask); j < (ranges->at(i + 1) & kMask); j++) { |
| 1818 | templ[j] = bit; |
| 1819 | } |
| 1820 | bit ^= 1; |
| 1821 | } |
| 1822 | for (int i = j; i < kSize; i++) { |
| 1823 | templ[i] = bit; |
| 1824 | } |
| 1825 | Factory* factory = masm->zone()->isolate()->factory(); |
| 1826 | // TODO(erikcorry): Cache these. |
| 1827 | Handle<ByteArray> ba = factory->NewByteArray(kSize, TENURED); |
| 1828 | for (int i = 0; i < kSize; i++) { |
| 1829 | ba->set(i, templ[i]); |
| 1830 | } |
| 1831 | masm->CheckBitInTable(ba, on_bit_set); |
| 1832 | if (on_bit_clear != fall_through) masm->GoTo(on_bit_clear); |
| 1833 | } |
| 1834 | |
| 1835 | |
| 1836 | static void CutOutRange(RegExpMacroAssembler* masm, |
| 1837 | ZoneList<int>* ranges, |
| 1838 | int start_index, |
| 1839 | int end_index, |
| 1840 | int cut_index, |
| 1841 | Label* even_label, |
| 1842 | Label* odd_label) { |
| 1843 | bool odd = (((cut_index - start_index) & 1) == 1); |
| 1844 | Label* in_range_label = odd ? odd_label : even_label; |
| 1845 | Label dummy; |
| 1846 | EmitDoubleBoundaryTest(masm, |
| 1847 | ranges->at(cut_index), |
| 1848 | ranges->at(cut_index + 1) - 1, |
| 1849 | &dummy, |
| 1850 | in_range_label, |
| 1851 | &dummy); |
| 1852 | DCHECK(!dummy.is_linked()); |
| 1853 | // Cut out the single range by rewriting the array. This creates a new |
| 1854 | // range that is a merger of the two ranges on either side of the one we |
| 1855 | // are cutting out. The oddity of the labels is preserved. |
| 1856 | for (int j = cut_index; j > start_index; j--) { |
| 1857 | ranges->at(j) = ranges->at(j - 1); |
| 1858 | } |
| 1859 | for (int j = cut_index + 1; j < end_index; j++) { |
| 1860 | ranges->at(j) = ranges->at(j + 1); |
| 1861 | } |
| 1862 | } |
| 1863 | |
| 1864 | |
| 1865 | // Unicode case. Split the search space into kSize spaces that are handled |
| 1866 | // with recursion. |
| 1867 | static void SplitSearchSpace(ZoneList<int>* ranges, |
| 1868 | int start_index, |
| 1869 | int end_index, |
| 1870 | int* new_start_index, |
| 1871 | int* new_end_index, |
| 1872 | int* border) { |
| 1873 | static const int kSize = RegExpMacroAssembler::kTableSize; |
| 1874 | static const int kMask = RegExpMacroAssembler::kTableMask; |
| 1875 | |
| 1876 | int first = ranges->at(start_index); |
| 1877 | int last = ranges->at(end_index) - 1; |
| 1878 | |
| 1879 | *new_start_index = start_index; |
| 1880 | *border = (ranges->at(start_index) & ~kMask) + kSize; |
| 1881 | while (*new_start_index < end_index) { |
| 1882 | if (ranges->at(*new_start_index) > *border) break; |
| 1883 | (*new_start_index)++; |
| 1884 | } |
| 1885 | // new_start_index is the index of the first edge that is beyond the |
| 1886 | // current kSize space. |
| 1887 | |
| 1888 | // For very large search spaces we do a binary chop search of the non-Latin1 |
| 1889 | // space instead of just going to the end of the current kSize space. The |
| 1890 | // heuristics are complicated a little by the fact that any 128-character |
| 1891 | // encoding space can be quickly tested with a table lookup, so we don't |
| 1892 | // wish to do binary chop search at a smaller granularity than that. A |
| 1893 | // 128-character space can take up a lot of space in the ranges array if, |
| 1894 | // for example, we only want to match every second character (eg. the lower |
| 1895 | // case characters on some Unicode pages). |
| 1896 | int binary_chop_index = (end_index + start_index) / 2; |
| 1897 | // The first test ensures that we get to the code that handles the Latin1 |
| 1898 | // range with a single not-taken branch, speeding up this important |
| 1899 | // character range (even non-Latin1 charset-based text has spaces and |
| 1900 | // punctuation). |
| 1901 | if (*border - 1 > String::kMaxOneByteCharCode && // Latin1 case. |
| 1902 | end_index - start_index > (*new_start_index - start_index) * 2 && |
| 1903 | last - first > kSize * 2 && binary_chop_index > *new_start_index && |
| 1904 | ranges->at(binary_chop_index) >= first + 2 * kSize) { |
| 1905 | int scan_forward_for_section_border = binary_chop_index;; |
| 1906 | int new_border = (ranges->at(binary_chop_index) | kMask) + 1; |
| 1907 | |
| 1908 | while (scan_forward_for_section_border < end_index) { |
| 1909 | if (ranges->at(scan_forward_for_section_border) > new_border) { |
| 1910 | *new_start_index = scan_forward_for_section_border; |
| 1911 | *border = new_border; |
| 1912 | break; |
| 1913 | } |
| 1914 | scan_forward_for_section_border++; |
| 1915 | } |
| 1916 | } |
| 1917 | |
| 1918 | DCHECK(*new_start_index > start_index); |
| 1919 | *new_end_index = *new_start_index - 1; |
| 1920 | if (ranges->at(*new_end_index) == *border) { |
| 1921 | (*new_end_index)--; |
| 1922 | } |
| 1923 | if (*border >= ranges->at(end_index)) { |
| 1924 | *border = ranges->at(end_index); |
| 1925 | *new_start_index = end_index; // Won't be used. |
| 1926 | *new_end_index = end_index - 1; |
| 1927 | } |
| 1928 | } |
| 1929 | |
| 1930 | |
| 1931 | // Gets a series of segment boundaries representing a character class. If the |
| 1932 | // character is in the range between an even and an odd boundary (counting from |
| 1933 | // start_index) then go to even_label, otherwise go to odd_label. We already |
| 1934 | // know that the character is in the range of min_char to max_char inclusive. |
| 1935 | // Either label can be NULL indicating backtracking. Either label can also be |
| 1936 | // equal to the fall_through label. |
| 1937 | static void GenerateBranches(RegExpMacroAssembler* masm, |
| 1938 | ZoneList<int>* ranges, |
| 1939 | int start_index, |
| 1940 | int end_index, |
| 1941 | uc16 min_char, |
| 1942 | uc16 max_char, |
| 1943 | Label* fall_through, |
| 1944 | Label* even_label, |
| 1945 | Label* odd_label) { |
| 1946 | int first = ranges->at(start_index); |
| 1947 | int last = ranges->at(end_index) - 1; |
| 1948 | |
| 1949 | DCHECK_LT(min_char, first); |
| 1950 | |
| 1951 | // Just need to test if the character is before or on-or-after |
| 1952 | // a particular character. |
| 1953 | if (start_index == end_index) { |
| 1954 | EmitBoundaryTest(masm, first, fall_through, even_label, odd_label); |
| 1955 | return; |
| 1956 | } |
| 1957 | |
| 1958 | // Another almost trivial case: There is one interval in the middle that is |
| 1959 | // different from the end intervals. |
| 1960 | if (start_index + 1 == end_index) { |
| 1961 | EmitDoubleBoundaryTest( |
| 1962 | masm, first, last, fall_through, even_label, odd_label); |
| 1963 | return; |
| 1964 | } |
| 1965 | |
| 1966 | // It's not worth using table lookup if there are very few intervals in the |
| 1967 | // character class. |
| 1968 | if (end_index - start_index <= 6) { |
| 1969 | // It is faster to test for individual characters, so we look for those |
| 1970 | // first, then try arbitrary ranges in the second round. |
| 1971 | static int kNoCutIndex = -1; |
| 1972 | int cut = kNoCutIndex; |
| 1973 | for (int i = start_index; i < end_index; i++) { |
| 1974 | if (ranges->at(i) == ranges->at(i + 1) - 1) { |
| 1975 | cut = i; |
| 1976 | break; |
| 1977 | } |
| 1978 | } |
| 1979 | if (cut == kNoCutIndex) cut = start_index; |
| 1980 | CutOutRange( |
| 1981 | masm, ranges, start_index, end_index, cut, even_label, odd_label); |
| 1982 | DCHECK_GE(end_index - start_index, 2); |
| 1983 | GenerateBranches(masm, |
| 1984 | ranges, |
| 1985 | start_index + 1, |
| 1986 | end_index - 1, |
| 1987 | min_char, |
| 1988 | max_char, |
| 1989 | fall_through, |
| 1990 | even_label, |
| 1991 | odd_label); |
| 1992 | return; |
| 1993 | } |
| 1994 | |
| 1995 | // If there are a lot of intervals in the regexp, then we will use tables to |
| 1996 | // determine whether the character is inside or outside the character class. |
| 1997 | static const int kBits = RegExpMacroAssembler::kTableSizeBits; |
| 1998 | |
| 1999 | if ((max_char >> kBits) == (min_char >> kBits)) { |
| 2000 | EmitUseLookupTable(masm, |
| 2001 | ranges, |
| 2002 | start_index, |
| 2003 | end_index, |
| 2004 | min_char, |
| 2005 | fall_through, |
| 2006 | even_label, |
| 2007 | odd_label); |
| 2008 | return; |
| 2009 | } |
| 2010 | |
| 2011 | if ((min_char >> kBits) != (first >> kBits)) { |
| 2012 | masm->CheckCharacterLT(first, odd_label); |
| 2013 | GenerateBranches(masm, |
| 2014 | ranges, |
| 2015 | start_index + 1, |
| 2016 | end_index, |
| 2017 | first, |
| 2018 | max_char, |
| 2019 | fall_through, |
| 2020 | odd_label, |
| 2021 | even_label); |
| 2022 | return; |
| 2023 | } |
| 2024 | |
| 2025 | int new_start_index = 0; |
| 2026 | int new_end_index = 0; |
| 2027 | int border = 0; |
| 2028 | |
| 2029 | SplitSearchSpace(ranges, |
| 2030 | start_index, |
| 2031 | end_index, |
| 2032 | &new_start_index, |
| 2033 | &new_end_index, |
| 2034 | &border); |
| 2035 | |
| 2036 | Label handle_rest; |
| 2037 | Label* above = &handle_rest; |
| 2038 | if (border == last + 1) { |
| 2039 | // We didn't find any section that started after the limit, so everything |
| 2040 | // above the border is one of the terminal labels. |
| 2041 | above = (end_index & 1) != (start_index & 1) ? odd_label : even_label; |
| 2042 | DCHECK(new_end_index == end_index - 1); |
| 2043 | } |
| 2044 | |
| 2045 | DCHECK_LE(start_index, new_end_index); |
| 2046 | DCHECK_LE(new_start_index, end_index); |
| 2047 | DCHECK_LT(start_index, new_start_index); |
| 2048 | DCHECK_LT(new_end_index, end_index); |
| 2049 | DCHECK(new_end_index + 1 == new_start_index || |
| 2050 | (new_end_index + 2 == new_start_index && |
| 2051 | border == ranges->at(new_end_index + 1))); |
| 2052 | DCHECK_LT(min_char, border - 1); |
| 2053 | DCHECK_LT(border, max_char); |
| 2054 | DCHECK_LT(ranges->at(new_end_index), border); |
| 2055 | DCHECK(border < ranges->at(new_start_index) || |
| 2056 | (border == ranges->at(new_start_index) && |
| 2057 | new_start_index == end_index && |
| 2058 | new_end_index == end_index - 1 && |
| 2059 | border == last + 1)); |
| 2060 | DCHECK(new_start_index == 0 || border >= ranges->at(new_start_index - 1)); |
| 2061 | |
| 2062 | masm->CheckCharacterGT(border - 1, above); |
| 2063 | Label dummy; |
| 2064 | GenerateBranches(masm, |
| 2065 | ranges, |
| 2066 | start_index, |
| 2067 | new_end_index, |
| 2068 | min_char, |
| 2069 | border - 1, |
| 2070 | &dummy, |
| 2071 | even_label, |
| 2072 | odd_label); |
| 2073 | if (handle_rest.is_linked()) { |
| 2074 | masm->Bind(&handle_rest); |
| 2075 | bool flip = (new_start_index & 1) != (start_index & 1); |
| 2076 | GenerateBranches(masm, |
| 2077 | ranges, |
| 2078 | new_start_index, |
| 2079 | end_index, |
| 2080 | border, |
| 2081 | max_char, |
| 2082 | &dummy, |
| 2083 | flip ? odd_label : even_label, |
| 2084 | flip ? even_label : odd_label); |
| 2085 | } |
| 2086 | } |
| 2087 | |
| 2088 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2089 | static void EmitCharClass(RegExpMacroAssembler* macro_assembler, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2090 | RegExpCharacterClass* cc, bool one_byte, |
| 2091 | Label* on_failure, int cp_offset, bool check_offset, |
| 2092 | bool preloaded, Zone* zone) { |
| 2093 | ZoneList<CharacterRange>* ranges = cc->ranges(zone); |
| 2094 | if (!CharacterRange::IsCanonical(ranges)) { |
| 2095 | CharacterRange::Canonicalize(ranges); |
| 2096 | } |
| 2097 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2098 | int max_char; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2099 | if (one_byte) { |
| 2100 | max_char = String::kMaxOneByteCharCode; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2101 | } else { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2102 | max_char = String::kMaxUtf16CodeUnit; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2103 | } |
| 2104 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2105 | int range_count = ranges->length(); |
| 2106 | |
| 2107 | int last_valid_range = range_count - 1; |
| 2108 | while (last_valid_range >= 0) { |
| 2109 | CharacterRange& range = ranges->at(last_valid_range); |
| 2110 | if (range.from() <= max_char) { |
| 2111 | break; |
| 2112 | } |
| 2113 | last_valid_range--; |
| 2114 | } |
| 2115 | |
| 2116 | if (last_valid_range < 0) { |
| 2117 | if (!cc->is_negated()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2118 | macro_assembler->GoTo(on_failure); |
| 2119 | } |
| 2120 | if (check_offset) { |
| 2121 | macro_assembler->CheckPosition(cp_offset, on_failure); |
| 2122 | } |
| 2123 | return; |
| 2124 | } |
| 2125 | |
| 2126 | if (last_valid_range == 0 && |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2127 | ranges->at(0).IsEverything(max_char)) { |
| 2128 | if (cc->is_negated()) { |
| 2129 | macro_assembler->GoTo(on_failure); |
| 2130 | } else { |
| 2131 | // This is a common case hit by non-anchored expressions. |
| 2132 | if (check_offset) { |
| 2133 | macro_assembler->CheckPosition(cp_offset, on_failure); |
| 2134 | } |
| 2135 | } |
| 2136 | return; |
| 2137 | } |
| 2138 | if (last_valid_range == 0 && |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2139 | !cc->is_negated() && |
| 2140 | ranges->at(0).IsEverything(max_char)) { |
| 2141 | // This is a common case hit by non-anchored expressions. |
| 2142 | if (check_offset) { |
| 2143 | macro_assembler->CheckPosition(cp_offset, on_failure); |
| 2144 | } |
| 2145 | return; |
| 2146 | } |
| 2147 | |
| 2148 | if (!preloaded) { |
| 2149 | macro_assembler->LoadCurrentCharacter(cp_offset, on_failure, check_offset); |
| 2150 | } |
| 2151 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2152 | if (cc->is_standard(zone) && |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2153 | macro_assembler->CheckSpecialCharacterClass(cc->standard_type(), |
| 2154 | on_failure)) { |
| 2155 | return; |
| 2156 | } |
| 2157 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2158 | |
| 2159 | // A new list with ascending entries. Each entry is a code unit |
| 2160 | // where there is a boundary between code units that are part of |
| 2161 | // the class and code units that are not. Normally we insert an |
| 2162 | // entry at zero which goes to the failure label, but if there |
| 2163 | // was already one there we fall through for success on that entry. |
| 2164 | // Subsequent entries have alternating meaning (success/failure). |
| 2165 | ZoneList<int>* range_boundaries = |
| 2166 | new(zone) ZoneList<int>(last_valid_range, zone); |
| 2167 | |
| 2168 | bool zeroth_entry_is_failure = !cc->is_negated(); |
| 2169 | |
| 2170 | for (int i = 0; i <= last_valid_range; i++) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2171 | CharacterRange& range = ranges->at(i); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2172 | if (range.from() == 0) { |
| 2173 | DCHECK_EQ(i, 0); |
| 2174 | zeroth_entry_is_failure = !zeroth_entry_is_failure; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2175 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2176 | range_boundaries->Add(range.from(), zone); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2177 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2178 | range_boundaries->Add(range.to() + 1, zone); |
| 2179 | } |
| 2180 | int end_index = range_boundaries->length() - 1; |
| 2181 | if (range_boundaries->at(end_index) > max_char) { |
| 2182 | end_index--; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2183 | } |
| 2184 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2185 | Label fall_through; |
| 2186 | GenerateBranches(macro_assembler, |
| 2187 | range_boundaries, |
| 2188 | 0, // start_index. |
| 2189 | end_index, |
| 2190 | 0, // min_char. |
| 2191 | max_char, |
| 2192 | &fall_through, |
| 2193 | zeroth_entry_is_failure ? &fall_through : on_failure, |
| 2194 | zeroth_entry_is_failure ? on_failure : &fall_through); |
| 2195 | macro_assembler->Bind(&fall_through); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2196 | } |
| 2197 | |
| 2198 | |
| 2199 | RegExpNode::~RegExpNode() { |
| 2200 | } |
| 2201 | |
| 2202 | |
| 2203 | RegExpNode::LimitResult RegExpNode::LimitVersions(RegExpCompiler* compiler, |
| 2204 | Trace* trace) { |
| 2205 | // If we are generating a greedy loop then don't stop and don't reuse code. |
| 2206 | if (trace->stop_node() != NULL) { |
| 2207 | return CONTINUE; |
| 2208 | } |
| 2209 | |
| 2210 | RegExpMacroAssembler* macro_assembler = compiler->macro_assembler(); |
| 2211 | if (trace->is_trivial()) { |
| 2212 | if (label_.is_bound()) { |
| 2213 | // We are being asked to generate a generic version, but that's already |
| 2214 | // been done so just go to it. |
| 2215 | macro_assembler->GoTo(&label_); |
| 2216 | return DONE; |
| 2217 | } |
| 2218 | if (compiler->recursion_depth() >= RegExpCompiler::kMaxRecursion) { |
| 2219 | // To avoid too deep recursion we push the node to the work queue and just |
| 2220 | // generate a goto here. |
| 2221 | compiler->AddWork(this); |
| 2222 | macro_assembler->GoTo(&label_); |
| 2223 | return DONE; |
| 2224 | } |
| 2225 | // Generate generic version of the node and bind the label for later use. |
| 2226 | macro_assembler->Bind(&label_); |
| 2227 | return CONTINUE; |
| 2228 | } |
| 2229 | |
| 2230 | // We are being asked to make a non-generic version. Keep track of how many |
| 2231 | // non-generic versions we generate so as not to overdo it. |
| 2232 | trace_count_++; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 2233 | if (compiler->optimize() && trace_count_ < kMaxCopiesCodeGenerated && |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2234 | compiler->recursion_depth() <= RegExpCompiler::kMaxRecursion) { |
| 2235 | return CONTINUE; |
| 2236 | } |
| 2237 | |
| 2238 | // If we get here code has been generated for this node too many times or |
| 2239 | // recursion is too deep. Time to switch to a generic version. The code for |
| 2240 | // generic versions above can handle deep recursion properly. |
| 2241 | trace->Flush(compiler, this); |
| 2242 | return DONE; |
| 2243 | } |
| 2244 | |
| 2245 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2246 | int ActionNode::EatsAtLeast(int still_to_find, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2247 | int budget, |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2248 | bool not_at_start) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2249 | if (budget <= 0) return 0; |
| 2250 | if (action_type_ == POSITIVE_SUBMATCH_SUCCESS) return 0; // Rewinds input! |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2251 | return on_success()->EatsAtLeast(still_to_find, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2252 | budget - 1, |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2253 | not_at_start); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2254 | } |
| 2255 | |
| 2256 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2257 | void ActionNode::FillInBMInfo(int offset, |
| 2258 | int budget, |
| 2259 | BoyerMooreLookahead* bm, |
| 2260 | bool not_at_start) { |
| 2261 | if (action_type_ == BEGIN_SUBMATCH) { |
| 2262 | bm->SetRest(offset); |
| 2263 | } else if (action_type_ != POSITIVE_SUBMATCH_SUCCESS) { |
| 2264 | on_success()->FillInBMInfo(offset, budget - 1, bm, not_at_start); |
| 2265 | } |
| 2266 | SaveBMInfo(bm, not_at_start, offset); |
| 2267 | } |
| 2268 | |
| 2269 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2270 | int AssertionNode::EatsAtLeast(int still_to_find, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2271 | int budget, |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2272 | bool not_at_start) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2273 | if (budget <= 0) return 0; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2274 | // If we know we are not at the start and we are asked "how many characters |
| 2275 | // will you match if you succeed?" then we can answer anything since false |
| 2276 | // implies false. So lets just return the max answer (still_to_find) since |
| 2277 | // that won't prevent us from preloading a lot of characters for the other |
| 2278 | // branches in the node graph. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2279 | if (assertion_type() == AT_START && not_at_start) return still_to_find; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2280 | return on_success()->EatsAtLeast(still_to_find, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2281 | budget - 1, |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2282 | not_at_start); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2283 | } |
| 2284 | |
| 2285 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2286 | void AssertionNode::FillInBMInfo(int offset, |
| 2287 | int budget, |
| 2288 | BoyerMooreLookahead* bm, |
| 2289 | bool not_at_start) { |
| 2290 | // Match the behaviour of EatsAtLeast on this node. |
| 2291 | if (assertion_type() == AT_START && not_at_start) return; |
| 2292 | on_success()->FillInBMInfo(offset, budget - 1, bm, not_at_start); |
| 2293 | SaveBMInfo(bm, not_at_start, offset); |
| 2294 | } |
| 2295 | |
| 2296 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2297 | int BackReferenceNode::EatsAtLeast(int still_to_find, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2298 | int budget, |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2299 | bool not_at_start) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2300 | if (budget <= 0) return 0; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2301 | return on_success()->EatsAtLeast(still_to_find, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2302 | budget - 1, |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2303 | not_at_start); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2304 | } |
| 2305 | |
| 2306 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2307 | int TextNode::EatsAtLeast(int still_to_find, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2308 | int budget, |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2309 | bool not_at_start) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2310 | int answer = Length(); |
| 2311 | if (answer >= still_to_find) return answer; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2312 | if (budget <= 0) return answer; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2313 | // We are not at start after this node so we set the last argument to 'true'. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2314 | return answer + on_success()->EatsAtLeast(still_to_find - answer, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2315 | budget - 1, |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2316 | true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2317 | } |
| 2318 | |
| 2319 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2320 | int NegativeLookaheadChoiceNode::EatsAtLeast(int still_to_find, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2321 | int budget, |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2322 | bool not_at_start) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2323 | if (budget <= 0) return 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2324 | // Alternative 0 is the negative lookahead, alternative 1 is what comes |
| 2325 | // afterwards. |
| 2326 | RegExpNode* node = alternatives_->at(1).node(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2327 | return node->EatsAtLeast(still_to_find, budget - 1, not_at_start); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2328 | } |
| 2329 | |
| 2330 | |
| 2331 | void NegativeLookaheadChoiceNode::GetQuickCheckDetails( |
| 2332 | QuickCheckDetails* details, |
| 2333 | RegExpCompiler* compiler, |
| 2334 | int filled_in, |
| 2335 | bool not_at_start) { |
| 2336 | // Alternative 0 is the negative lookahead, alternative 1 is what comes |
| 2337 | // afterwards. |
| 2338 | RegExpNode* node = alternatives_->at(1).node(); |
| 2339 | return node->GetQuickCheckDetails(details, compiler, filled_in, not_at_start); |
| 2340 | } |
| 2341 | |
| 2342 | |
| 2343 | int ChoiceNode::EatsAtLeastHelper(int still_to_find, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2344 | int budget, |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2345 | RegExpNode* ignore_this_node, |
| 2346 | bool not_at_start) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2347 | if (budget <= 0) return 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2348 | int min = 100; |
| 2349 | int choice_count = alternatives_->length(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2350 | budget = (budget - 1) / choice_count; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2351 | for (int i = 0; i < choice_count; i++) { |
| 2352 | RegExpNode* node = alternatives_->at(i).node(); |
| 2353 | if (node == ignore_this_node) continue; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2354 | int node_eats_at_least = |
| 2355 | node->EatsAtLeast(still_to_find, budget, not_at_start); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2356 | if (node_eats_at_least < min) min = node_eats_at_least; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2357 | if (min == 0) return 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2358 | } |
| 2359 | return min; |
| 2360 | } |
| 2361 | |
| 2362 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2363 | int LoopChoiceNode::EatsAtLeast(int still_to_find, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2364 | int budget, |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2365 | bool not_at_start) { |
| 2366 | return EatsAtLeastHelper(still_to_find, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2367 | budget - 1, |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2368 | loop_node_, |
| 2369 | not_at_start); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2370 | } |
| 2371 | |
| 2372 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2373 | int ChoiceNode::EatsAtLeast(int still_to_find, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2374 | int budget, |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2375 | bool not_at_start) { |
| 2376 | return EatsAtLeastHelper(still_to_find, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2377 | budget, |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2378 | NULL, |
| 2379 | not_at_start); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2380 | } |
| 2381 | |
| 2382 | |
| 2383 | // Takes the left-most 1-bit and smears it out, setting all bits to its right. |
| 2384 | static inline uint32_t SmearBitsRight(uint32_t v) { |
| 2385 | v |= v >> 1; |
| 2386 | v |= v >> 2; |
| 2387 | v |= v >> 4; |
| 2388 | v |= v >> 8; |
| 2389 | v |= v >> 16; |
| 2390 | return v; |
| 2391 | } |
| 2392 | |
| 2393 | |
| 2394 | bool QuickCheckDetails::Rationalize(bool asc) { |
| 2395 | bool found_useful_op = false; |
| 2396 | uint32_t char_mask; |
| 2397 | if (asc) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2398 | char_mask = String::kMaxOneByteCharCode; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2399 | } else { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2400 | char_mask = String::kMaxUtf16CodeUnit; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2401 | } |
| 2402 | mask_ = 0; |
| 2403 | value_ = 0; |
| 2404 | int char_shift = 0; |
| 2405 | for (int i = 0; i < characters_; i++) { |
| 2406 | Position* pos = &positions_[i]; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2407 | if ((pos->mask & String::kMaxOneByteCharCode) != 0) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2408 | found_useful_op = true; |
| 2409 | } |
| 2410 | mask_ |= (pos->mask & char_mask) << char_shift; |
| 2411 | value_ |= (pos->value & char_mask) << char_shift; |
| 2412 | char_shift += asc ? 8 : 16; |
| 2413 | } |
| 2414 | return found_useful_op; |
| 2415 | } |
| 2416 | |
| 2417 | |
| 2418 | bool RegExpNode::EmitQuickCheck(RegExpCompiler* compiler, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2419 | Trace* bounds_check_trace, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2420 | Trace* trace, |
| 2421 | bool preload_has_checked_bounds, |
| 2422 | Label* on_possible_success, |
| 2423 | QuickCheckDetails* details, |
| 2424 | bool fall_through_on_failure) { |
| 2425 | if (details->characters() == 0) return false; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2426 | GetQuickCheckDetails( |
| 2427 | details, compiler, 0, trace->at_start() == Trace::FALSE_VALUE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2428 | if (details->cannot_match()) return false; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2429 | if (!details->Rationalize(compiler->one_byte())) return false; |
| 2430 | DCHECK(details->characters() == 1 || |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2431 | compiler->macro_assembler()->CanReadUnaligned()); |
| 2432 | uint32_t mask = details->mask(); |
| 2433 | uint32_t value = details->value(); |
| 2434 | |
| 2435 | RegExpMacroAssembler* assembler = compiler->macro_assembler(); |
| 2436 | |
| 2437 | if (trace->characters_preloaded() != details->characters()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2438 | DCHECK(trace->cp_offset() == bounds_check_trace->cp_offset()); |
| 2439 | // We are attempting to preload the minimum number of characters |
| 2440 | // any choice would eat, so if the bounds check fails, then none of the |
| 2441 | // choices can succeed, so we can just immediately backtrack, rather |
| 2442 | // than go to the next choice. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2443 | assembler->LoadCurrentCharacter(trace->cp_offset(), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2444 | bounds_check_trace->backtrack(), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2445 | !preload_has_checked_bounds, |
| 2446 | details->characters()); |
| 2447 | } |
| 2448 | |
| 2449 | |
| 2450 | bool need_mask = true; |
| 2451 | |
| 2452 | if (details->characters() == 1) { |
| 2453 | // If number of characters preloaded is 1 then we used a byte or 16 bit |
| 2454 | // load so the value is already masked down. |
| 2455 | uint32_t char_mask; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2456 | if (compiler->one_byte()) { |
| 2457 | char_mask = String::kMaxOneByteCharCode; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2458 | } else { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2459 | char_mask = String::kMaxUtf16CodeUnit; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2460 | } |
| 2461 | if ((mask & char_mask) == char_mask) need_mask = false; |
| 2462 | mask &= char_mask; |
| 2463 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2464 | // For 2-character preloads in one-byte mode or 1-character preloads in |
| 2465 | // two-byte mode we also use a 16 bit load with zero extend. |
| 2466 | if (details->characters() == 2 && compiler->one_byte()) { |
| 2467 | if ((mask & 0xffff) == 0xffff) need_mask = false; |
| 2468 | } else if (details->characters() == 1 && !compiler->one_byte()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2469 | if ((mask & 0xffff) == 0xffff) need_mask = false; |
| 2470 | } else { |
| 2471 | if (mask == 0xffffffff) need_mask = false; |
| 2472 | } |
| 2473 | } |
| 2474 | |
| 2475 | if (fall_through_on_failure) { |
| 2476 | if (need_mask) { |
| 2477 | assembler->CheckCharacterAfterAnd(value, mask, on_possible_success); |
| 2478 | } else { |
| 2479 | assembler->CheckCharacter(value, on_possible_success); |
| 2480 | } |
| 2481 | } else { |
| 2482 | if (need_mask) { |
| 2483 | assembler->CheckNotCharacterAfterAnd(value, mask, trace->backtrack()); |
| 2484 | } else { |
| 2485 | assembler->CheckNotCharacter(value, trace->backtrack()); |
| 2486 | } |
| 2487 | } |
| 2488 | return true; |
| 2489 | } |
| 2490 | |
| 2491 | |
| 2492 | // Here is the meat of GetQuickCheckDetails (see also the comment on the |
| 2493 | // super-class in the .h file). |
| 2494 | // |
| 2495 | // We iterate along the text object, building up for each character a |
| 2496 | // mask and value that can be used to test for a quick failure to match. |
| 2497 | // The masks and values for the positions will be combined into a single |
| 2498 | // machine word for the current character width in order to be used in |
| 2499 | // generating a quick check. |
| 2500 | void TextNode::GetQuickCheckDetails(QuickCheckDetails* details, |
| 2501 | RegExpCompiler* compiler, |
| 2502 | int characters_filled_in, |
| 2503 | bool not_at_start) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2504 | Isolate* isolate = compiler->macro_assembler()->zone()->isolate(); |
| 2505 | DCHECK(characters_filled_in < details->characters()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2506 | int characters = details->characters(); |
| 2507 | int char_mask; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2508 | if (compiler->one_byte()) { |
| 2509 | char_mask = String::kMaxOneByteCharCode; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2510 | } else { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2511 | char_mask = String::kMaxUtf16CodeUnit; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2512 | } |
| 2513 | for (int k = 0; k < elms_->length(); k++) { |
| 2514 | TextElement elm = elms_->at(k); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2515 | if (elm.text_type() == TextElement::ATOM) { |
| 2516 | Vector<const uc16> quarks = elm.atom()->data(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2517 | for (int i = 0; i < characters && i < quarks.length(); i++) { |
| 2518 | QuickCheckDetails::Position* pos = |
| 2519 | details->positions(characters_filled_in); |
| 2520 | uc16 c = quarks[i]; |
| 2521 | if (c > char_mask) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2522 | // If we expect a non-Latin1 character from an one-byte string, |
| 2523 | // there is no way we can match. Not even case-independent |
| 2524 | // matching can turn an Latin1 character into non-Latin1 or |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2525 | // vice versa. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2526 | // TODO(dcarney): issue 3550. Verify that this works as expected. |
| 2527 | // For example, \u0178 is uppercase of \u00ff (y-umlaut). |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2528 | details->set_cannot_match(); |
| 2529 | pos->determines_perfectly = false; |
| 2530 | return; |
| 2531 | } |
| 2532 | if (compiler->ignore_case()) { |
| 2533 | unibrow::uchar chars[unibrow::Ecma262UnCanonicalize::kMaxWidth]; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2534 | int length = GetCaseIndependentLetters(isolate, c, |
| 2535 | compiler->one_byte(), chars); |
| 2536 | DCHECK(length != 0); // Can only happen if c > char_mask (see above). |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2537 | if (length == 1) { |
| 2538 | // This letter has no case equivalents, so it's nice and simple |
| 2539 | // and the mask-compare will determine definitely whether we have |
| 2540 | // a match at this character position. |
| 2541 | pos->mask = char_mask; |
| 2542 | pos->value = c; |
| 2543 | pos->determines_perfectly = true; |
| 2544 | } else { |
| 2545 | uint32_t common_bits = char_mask; |
| 2546 | uint32_t bits = chars[0]; |
| 2547 | for (int j = 1; j < length; j++) { |
| 2548 | uint32_t differing_bits = ((chars[j] & common_bits) ^ bits); |
| 2549 | common_bits ^= differing_bits; |
| 2550 | bits &= common_bits; |
| 2551 | } |
| 2552 | // If length is 2 and common bits has only one zero in it then |
| 2553 | // our mask and compare instruction will determine definitely |
| 2554 | // whether we have a match at this character position. Otherwise |
| 2555 | // it can only be an approximate check. |
| 2556 | uint32_t one_zero = (common_bits | ~char_mask); |
| 2557 | if (length == 2 && ((~one_zero) & ((~one_zero) - 1)) == 0) { |
| 2558 | pos->determines_perfectly = true; |
| 2559 | } |
| 2560 | pos->mask = common_bits; |
| 2561 | pos->value = bits; |
| 2562 | } |
| 2563 | } else { |
| 2564 | // Don't ignore case. Nice simple case where the mask-compare will |
| 2565 | // determine definitely whether we have a match at this character |
| 2566 | // position. |
| 2567 | pos->mask = char_mask; |
| 2568 | pos->value = c; |
| 2569 | pos->determines_perfectly = true; |
| 2570 | } |
| 2571 | characters_filled_in++; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2572 | DCHECK(characters_filled_in <= details->characters()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2573 | if (characters_filled_in == details->characters()) { |
| 2574 | return; |
| 2575 | } |
| 2576 | } |
| 2577 | } else { |
| 2578 | QuickCheckDetails::Position* pos = |
| 2579 | details->positions(characters_filled_in); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2580 | RegExpCharacterClass* tree = elm.char_class(); |
| 2581 | ZoneList<CharacterRange>* ranges = tree->ranges(zone()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2582 | if (tree->is_negated()) { |
| 2583 | // A quick check uses multi-character mask and compare. There is no |
| 2584 | // useful way to incorporate a negative char class into this scheme |
| 2585 | // so we just conservatively create a mask and value that will always |
| 2586 | // succeed. |
| 2587 | pos->mask = 0; |
| 2588 | pos->value = 0; |
| 2589 | } else { |
| 2590 | int first_range = 0; |
| 2591 | while (ranges->at(first_range).from() > char_mask) { |
| 2592 | first_range++; |
| 2593 | if (first_range == ranges->length()) { |
| 2594 | details->set_cannot_match(); |
| 2595 | pos->determines_perfectly = false; |
| 2596 | return; |
| 2597 | } |
| 2598 | } |
| 2599 | CharacterRange range = ranges->at(first_range); |
| 2600 | uc16 from = range.from(); |
| 2601 | uc16 to = range.to(); |
| 2602 | if (to > char_mask) { |
| 2603 | to = char_mask; |
| 2604 | } |
| 2605 | uint32_t differing_bits = (from ^ to); |
| 2606 | // A mask and compare is only perfect if the differing bits form a |
| 2607 | // number like 00011111 with one single block of trailing 1s. |
| 2608 | if ((differing_bits & (differing_bits + 1)) == 0 && |
| 2609 | from + differing_bits == to) { |
| 2610 | pos->determines_perfectly = true; |
| 2611 | } |
| 2612 | uint32_t common_bits = ~SmearBitsRight(differing_bits); |
| 2613 | uint32_t bits = (from & common_bits); |
| 2614 | for (int i = first_range + 1; i < ranges->length(); i++) { |
| 2615 | CharacterRange range = ranges->at(i); |
| 2616 | uc16 from = range.from(); |
| 2617 | uc16 to = range.to(); |
| 2618 | if (from > char_mask) continue; |
| 2619 | if (to > char_mask) to = char_mask; |
| 2620 | // Here we are combining more ranges into the mask and compare |
| 2621 | // value. With each new range the mask becomes more sparse and |
| 2622 | // so the chances of a false positive rise. A character class |
| 2623 | // with multiple ranges is assumed never to be equivalent to a |
| 2624 | // mask and compare operation. |
| 2625 | pos->determines_perfectly = false; |
| 2626 | uint32_t new_common_bits = (from ^ to); |
| 2627 | new_common_bits = ~SmearBitsRight(new_common_bits); |
| 2628 | common_bits &= new_common_bits; |
| 2629 | bits &= new_common_bits; |
| 2630 | uint32_t differing_bits = (from & common_bits) ^ bits; |
| 2631 | common_bits ^= differing_bits; |
| 2632 | bits &= common_bits; |
| 2633 | } |
| 2634 | pos->mask = common_bits; |
| 2635 | pos->value = bits; |
| 2636 | } |
| 2637 | characters_filled_in++; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2638 | DCHECK(characters_filled_in <= details->characters()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2639 | if (characters_filled_in == details->characters()) { |
| 2640 | return; |
| 2641 | } |
| 2642 | } |
| 2643 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2644 | DCHECK(characters_filled_in != details->characters()); |
| 2645 | if (!details->cannot_match()) { |
| 2646 | on_success()-> GetQuickCheckDetails(details, |
| 2647 | compiler, |
| 2648 | characters_filled_in, |
| 2649 | true); |
| 2650 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2651 | } |
| 2652 | |
| 2653 | |
| 2654 | void QuickCheckDetails::Clear() { |
| 2655 | for (int i = 0; i < characters_; i++) { |
| 2656 | positions_[i].mask = 0; |
| 2657 | positions_[i].value = 0; |
| 2658 | positions_[i].determines_perfectly = false; |
| 2659 | } |
| 2660 | characters_ = 0; |
| 2661 | } |
| 2662 | |
| 2663 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2664 | void QuickCheckDetails::Advance(int by, bool one_byte) { |
| 2665 | DCHECK(by >= 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2666 | if (by >= characters_) { |
| 2667 | Clear(); |
| 2668 | return; |
| 2669 | } |
| 2670 | for (int i = 0; i < characters_ - by; i++) { |
| 2671 | positions_[i] = positions_[by + i]; |
| 2672 | } |
| 2673 | for (int i = characters_ - by; i < characters_; i++) { |
| 2674 | positions_[i].mask = 0; |
| 2675 | positions_[i].value = 0; |
| 2676 | positions_[i].determines_perfectly = false; |
| 2677 | } |
| 2678 | characters_ -= by; |
| 2679 | // We could change mask_ and value_ here but we would never advance unless |
| 2680 | // they had already been used in a check and they won't be used again because |
| 2681 | // it would gain us nothing. So there's no point. |
| 2682 | } |
| 2683 | |
| 2684 | |
| 2685 | void QuickCheckDetails::Merge(QuickCheckDetails* other, int from_index) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2686 | DCHECK(characters_ == other->characters_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2687 | if (other->cannot_match_) { |
| 2688 | return; |
| 2689 | } |
| 2690 | if (cannot_match_) { |
| 2691 | *this = *other; |
| 2692 | return; |
| 2693 | } |
| 2694 | for (int i = from_index; i < characters_; i++) { |
| 2695 | QuickCheckDetails::Position* pos = positions(i); |
| 2696 | QuickCheckDetails::Position* other_pos = other->positions(i); |
| 2697 | if (pos->mask != other_pos->mask || |
| 2698 | pos->value != other_pos->value || |
| 2699 | !other_pos->determines_perfectly) { |
| 2700 | // Our mask-compare operation will be approximate unless we have the |
| 2701 | // exact same operation on both sides of the alternation. |
| 2702 | pos->determines_perfectly = false; |
| 2703 | } |
| 2704 | pos->mask &= other_pos->mask; |
| 2705 | pos->value &= pos->mask; |
| 2706 | other_pos->value &= pos->mask; |
| 2707 | uc16 differing_bits = (pos->value ^ other_pos->value); |
| 2708 | pos->mask &= ~differing_bits; |
| 2709 | pos->value &= pos->mask; |
| 2710 | } |
| 2711 | } |
| 2712 | |
| 2713 | |
| 2714 | class VisitMarker { |
| 2715 | public: |
| 2716 | explicit VisitMarker(NodeInfo* info) : info_(info) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2717 | DCHECK(!info->visited); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2718 | info->visited = true; |
| 2719 | } |
| 2720 | ~VisitMarker() { |
| 2721 | info_->visited = false; |
| 2722 | } |
| 2723 | private: |
| 2724 | NodeInfo* info_; |
| 2725 | }; |
| 2726 | |
| 2727 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2728 | RegExpNode* SeqRegExpNode::FilterOneByte(int depth, bool ignore_case) { |
| 2729 | if (info()->replacement_calculated) return replacement(); |
| 2730 | if (depth < 0) return this; |
| 2731 | DCHECK(!info()->visited); |
| 2732 | VisitMarker marker(info()); |
| 2733 | return FilterSuccessor(depth - 1, ignore_case); |
| 2734 | } |
| 2735 | |
| 2736 | |
| 2737 | RegExpNode* SeqRegExpNode::FilterSuccessor(int depth, bool ignore_case) { |
| 2738 | RegExpNode* next = on_success_->FilterOneByte(depth - 1, ignore_case); |
| 2739 | if (next == NULL) return set_replacement(NULL); |
| 2740 | on_success_ = next; |
| 2741 | return set_replacement(this); |
| 2742 | } |
| 2743 | |
| 2744 | |
| 2745 | // We need to check for the following characters: 0x39c 0x3bc 0x178. |
| 2746 | static inline bool RangeContainsLatin1Equivalents(CharacterRange range) { |
| 2747 | // TODO(dcarney): this could be a lot more efficient. |
| 2748 | return range.Contains(0x39c) || |
| 2749 | range.Contains(0x3bc) || range.Contains(0x178); |
| 2750 | } |
| 2751 | |
| 2752 | |
| 2753 | static bool RangesContainLatin1Equivalents(ZoneList<CharacterRange>* ranges) { |
| 2754 | for (int i = 0; i < ranges->length(); i++) { |
| 2755 | // TODO(dcarney): this could be a lot more efficient. |
| 2756 | if (RangeContainsLatin1Equivalents(ranges->at(i))) return true; |
| 2757 | } |
| 2758 | return false; |
| 2759 | } |
| 2760 | |
| 2761 | |
| 2762 | RegExpNode* TextNode::FilterOneByte(int depth, bool ignore_case) { |
| 2763 | if (info()->replacement_calculated) return replacement(); |
| 2764 | if (depth < 0) return this; |
| 2765 | DCHECK(!info()->visited); |
| 2766 | VisitMarker marker(info()); |
| 2767 | int element_count = elms_->length(); |
| 2768 | for (int i = 0; i < element_count; i++) { |
| 2769 | TextElement elm = elms_->at(i); |
| 2770 | if (elm.text_type() == TextElement::ATOM) { |
| 2771 | Vector<const uc16> quarks = elm.atom()->data(); |
| 2772 | for (int j = 0; j < quarks.length(); j++) { |
| 2773 | uint16_t c = quarks[j]; |
| 2774 | if (c <= String::kMaxOneByteCharCode) continue; |
| 2775 | if (!ignore_case) return set_replacement(NULL); |
| 2776 | // Here, we need to check for characters whose upper and lower cases |
| 2777 | // are outside the Latin-1 range. |
| 2778 | uint16_t converted = unibrow::Latin1::ConvertNonLatin1ToLatin1(c); |
| 2779 | // Character is outside Latin-1 completely |
| 2780 | if (converted == 0) return set_replacement(NULL); |
| 2781 | // Convert quark to Latin-1 in place. |
| 2782 | uint16_t* copy = const_cast<uint16_t*>(quarks.start()); |
| 2783 | copy[j] = converted; |
| 2784 | } |
| 2785 | } else { |
| 2786 | DCHECK(elm.text_type() == TextElement::CHAR_CLASS); |
| 2787 | RegExpCharacterClass* cc = elm.char_class(); |
| 2788 | ZoneList<CharacterRange>* ranges = cc->ranges(zone()); |
| 2789 | if (!CharacterRange::IsCanonical(ranges)) { |
| 2790 | CharacterRange::Canonicalize(ranges); |
| 2791 | } |
| 2792 | // Now they are in order so we only need to look at the first. |
| 2793 | int range_count = ranges->length(); |
| 2794 | if (cc->is_negated()) { |
| 2795 | if (range_count != 0 && |
| 2796 | ranges->at(0).from() == 0 && |
| 2797 | ranges->at(0).to() >= String::kMaxOneByteCharCode) { |
| 2798 | // This will be handled in a later filter. |
| 2799 | if (ignore_case && RangesContainLatin1Equivalents(ranges)) continue; |
| 2800 | return set_replacement(NULL); |
| 2801 | } |
| 2802 | } else { |
| 2803 | if (range_count == 0 || |
| 2804 | ranges->at(0).from() > String::kMaxOneByteCharCode) { |
| 2805 | // This will be handled in a later filter. |
| 2806 | if (ignore_case && RangesContainLatin1Equivalents(ranges)) continue; |
| 2807 | return set_replacement(NULL); |
| 2808 | } |
| 2809 | } |
| 2810 | } |
| 2811 | } |
| 2812 | return FilterSuccessor(depth - 1, ignore_case); |
| 2813 | } |
| 2814 | |
| 2815 | |
| 2816 | RegExpNode* LoopChoiceNode::FilterOneByte(int depth, bool ignore_case) { |
| 2817 | if (info()->replacement_calculated) return replacement(); |
| 2818 | if (depth < 0) return this; |
| 2819 | if (info()->visited) return this; |
| 2820 | { |
| 2821 | VisitMarker marker(info()); |
| 2822 | |
| 2823 | RegExpNode* continue_replacement = |
| 2824 | continue_node_->FilterOneByte(depth - 1, ignore_case); |
| 2825 | // If we can't continue after the loop then there is no sense in doing the |
| 2826 | // loop. |
| 2827 | if (continue_replacement == NULL) return set_replacement(NULL); |
| 2828 | } |
| 2829 | |
| 2830 | return ChoiceNode::FilterOneByte(depth - 1, ignore_case); |
| 2831 | } |
| 2832 | |
| 2833 | |
| 2834 | RegExpNode* ChoiceNode::FilterOneByte(int depth, bool ignore_case) { |
| 2835 | if (info()->replacement_calculated) return replacement(); |
| 2836 | if (depth < 0) return this; |
| 2837 | if (info()->visited) return this; |
| 2838 | VisitMarker marker(info()); |
| 2839 | int choice_count = alternatives_->length(); |
| 2840 | |
| 2841 | for (int i = 0; i < choice_count; i++) { |
| 2842 | GuardedAlternative alternative = alternatives_->at(i); |
| 2843 | if (alternative.guards() != NULL && alternative.guards()->length() != 0) { |
| 2844 | set_replacement(this); |
| 2845 | return this; |
| 2846 | } |
| 2847 | } |
| 2848 | |
| 2849 | int surviving = 0; |
| 2850 | RegExpNode* survivor = NULL; |
| 2851 | for (int i = 0; i < choice_count; i++) { |
| 2852 | GuardedAlternative alternative = alternatives_->at(i); |
| 2853 | RegExpNode* replacement = |
| 2854 | alternative.node()->FilterOneByte(depth - 1, ignore_case); |
| 2855 | DCHECK(replacement != this); // No missing EMPTY_MATCH_CHECK. |
| 2856 | if (replacement != NULL) { |
| 2857 | alternatives_->at(i).set_node(replacement); |
| 2858 | surviving++; |
| 2859 | survivor = replacement; |
| 2860 | } |
| 2861 | } |
| 2862 | if (surviving < 2) return set_replacement(survivor); |
| 2863 | |
| 2864 | set_replacement(this); |
| 2865 | if (surviving == choice_count) { |
| 2866 | return this; |
| 2867 | } |
| 2868 | // Only some of the nodes survived the filtering. We need to rebuild the |
| 2869 | // alternatives list. |
| 2870 | ZoneList<GuardedAlternative>* new_alternatives = |
| 2871 | new(zone()) ZoneList<GuardedAlternative>(surviving, zone()); |
| 2872 | for (int i = 0; i < choice_count; i++) { |
| 2873 | RegExpNode* replacement = |
| 2874 | alternatives_->at(i).node()->FilterOneByte(depth - 1, ignore_case); |
| 2875 | if (replacement != NULL) { |
| 2876 | alternatives_->at(i).set_node(replacement); |
| 2877 | new_alternatives->Add(alternatives_->at(i), zone()); |
| 2878 | } |
| 2879 | } |
| 2880 | alternatives_ = new_alternatives; |
| 2881 | return this; |
| 2882 | } |
| 2883 | |
| 2884 | |
| 2885 | RegExpNode* NegativeLookaheadChoiceNode::FilterOneByte(int depth, |
| 2886 | bool ignore_case) { |
| 2887 | if (info()->replacement_calculated) return replacement(); |
| 2888 | if (depth < 0) return this; |
| 2889 | if (info()->visited) return this; |
| 2890 | VisitMarker marker(info()); |
| 2891 | // Alternative 0 is the negative lookahead, alternative 1 is what comes |
| 2892 | // afterwards. |
| 2893 | RegExpNode* node = alternatives_->at(1).node(); |
| 2894 | RegExpNode* replacement = node->FilterOneByte(depth - 1, ignore_case); |
| 2895 | if (replacement == NULL) return set_replacement(NULL); |
| 2896 | alternatives_->at(1).set_node(replacement); |
| 2897 | |
| 2898 | RegExpNode* neg_node = alternatives_->at(0).node(); |
| 2899 | RegExpNode* neg_replacement = neg_node->FilterOneByte(depth - 1, ignore_case); |
| 2900 | // If the negative lookahead is always going to fail then |
| 2901 | // we don't need to check it. |
| 2902 | if (neg_replacement == NULL) return set_replacement(replacement); |
| 2903 | alternatives_->at(0).set_node(neg_replacement); |
| 2904 | return set_replacement(this); |
| 2905 | } |
| 2906 | |
| 2907 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2908 | void LoopChoiceNode::GetQuickCheckDetails(QuickCheckDetails* details, |
| 2909 | RegExpCompiler* compiler, |
| 2910 | int characters_filled_in, |
| 2911 | bool not_at_start) { |
| 2912 | if (body_can_be_zero_length_ || info()->visited) return; |
| 2913 | VisitMarker marker(info()); |
| 2914 | return ChoiceNode::GetQuickCheckDetails(details, |
| 2915 | compiler, |
| 2916 | characters_filled_in, |
| 2917 | not_at_start); |
| 2918 | } |
| 2919 | |
| 2920 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2921 | void LoopChoiceNode::FillInBMInfo(int offset, |
| 2922 | int budget, |
| 2923 | BoyerMooreLookahead* bm, |
| 2924 | bool not_at_start) { |
| 2925 | if (body_can_be_zero_length_ || budget <= 0) { |
| 2926 | bm->SetRest(offset); |
| 2927 | SaveBMInfo(bm, not_at_start, offset); |
| 2928 | return; |
| 2929 | } |
| 2930 | ChoiceNode::FillInBMInfo(offset, budget - 1, bm, not_at_start); |
| 2931 | SaveBMInfo(bm, not_at_start, offset); |
| 2932 | } |
| 2933 | |
| 2934 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2935 | void ChoiceNode::GetQuickCheckDetails(QuickCheckDetails* details, |
| 2936 | RegExpCompiler* compiler, |
| 2937 | int characters_filled_in, |
| 2938 | bool not_at_start) { |
| 2939 | not_at_start = (not_at_start || not_at_start_); |
| 2940 | int choice_count = alternatives_->length(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2941 | DCHECK(choice_count > 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2942 | alternatives_->at(0).node()->GetQuickCheckDetails(details, |
| 2943 | compiler, |
| 2944 | characters_filled_in, |
| 2945 | not_at_start); |
| 2946 | for (int i = 1; i < choice_count; i++) { |
| 2947 | QuickCheckDetails new_details(details->characters()); |
| 2948 | RegExpNode* node = alternatives_->at(i).node(); |
| 2949 | node->GetQuickCheckDetails(&new_details, compiler, |
| 2950 | characters_filled_in, |
| 2951 | not_at_start); |
| 2952 | // Here we merge the quick match details of the two branches. |
| 2953 | details->Merge(&new_details, characters_filled_in); |
| 2954 | } |
| 2955 | } |
| 2956 | |
| 2957 | |
| 2958 | // Check for [0-9A-Z_a-z]. |
| 2959 | static void EmitWordCheck(RegExpMacroAssembler* assembler, |
| 2960 | Label* word, |
| 2961 | Label* non_word, |
| 2962 | bool fall_through_on_word) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2963 | if (assembler->CheckSpecialCharacterClass( |
| 2964 | fall_through_on_word ? 'w' : 'W', |
| 2965 | fall_through_on_word ? non_word : word)) { |
| 2966 | // Optimized implementation available. |
| 2967 | return; |
| 2968 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2969 | assembler->CheckCharacterGT('z', non_word); |
| 2970 | assembler->CheckCharacterLT('0', non_word); |
| 2971 | assembler->CheckCharacterGT('a' - 1, word); |
| 2972 | assembler->CheckCharacterLT('9' + 1, word); |
| 2973 | assembler->CheckCharacterLT('A', non_word); |
| 2974 | assembler->CheckCharacterLT('Z' + 1, word); |
| 2975 | if (fall_through_on_word) { |
| 2976 | assembler->CheckNotCharacter('_', non_word); |
| 2977 | } else { |
| 2978 | assembler->CheckCharacter('_', word); |
| 2979 | } |
| 2980 | } |
| 2981 | |
| 2982 | |
| 2983 | // Emit the code to check for a ^ in multiline mode (1-character lookbehind |
| 2984 | // that matches newline or the start of input). |
| 2985 | static void EmitHat(RegExpCompiler* compiler, |
| 2986 | RegExpNode* on_success, |
| 2987 | Trace* trace) { |
| 2988 | RegExpMacroAssembler* assembler = compiler->macro_assembler(); |
| 2989 | // We will be loading the previous character into the current character |
| 2990 | // register. |
| 2991 | Trace new_trace(*trace); |
| 2992 | new_trace.InvalidateCurrentCharacter(); |
| 2993 | |
| 2994 | Label ok; |
| 2995 | if (new_trace.cp_offset() == 0) { |
| 2996 | // The start of input counts as a newline in this context, so skip to |
| 2997 | // ok if we are at the start. |
| 2998 | assembler->CheckAtStart(&ok); |
| 2999 | } |
| 3000 | // We already checked that we are not at the start of input so it must be |
| 3001 | // OK to load the previous character. |
| 3002 | assembler->LoadCurrentCharacter(new_trace.cp_offset() -1, |
| 3003 | new_trace.backtrack(), |
| 3004 | false); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3005 | if (!assembler->CheckSpecialCharacterClass('n', |
| 3006 | new_trace.backtrack())) { |
| 3007 | // Newline means \n, \r, 0x2028 or 0x2029. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3008 | if (!compiler->one_byte()) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3009 | assembler->CheckCharacterAfterAnd(0x2028, 0xfffe, &ok); |
| 3010 | } |
| 3011 | assembler->CheckCharacter('\n', &ok); |
| 3012 | assembler->CheckNotCharacter('\r', new_trace.backtrack()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3013 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3014 | assembler->Bind(&ok); |
| 3015 | on_success->Emit(compiler, &new_trace); |
| 3016 | } |
| 3017 | |
| 3018 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3019 | // Emit the code to handle \b and \B (word-boundary or non-word-boundary). |
| 3020 | void AssertionNode::EmitBoundaryCheck(RegExpCompiler* compiler, Trace* trace) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3021 | RegExpMacroAssembler* assembler = compiler->macro_assembler(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3022 | Trace::TriBool next_is_word_character = Trace::UNKNOWN; |
| 3023 | bool not_at_start = (trace->at_start() == Trace::FALSE_VALUE); |
| 3024 | BoyerMooreLookahead* lookahead = bm_info(not_at_start); |
| 3025 | if (lookahead == NULL) { |
| 3026 | int eats_at_least = |
| 3027 | Min(kMaxLookaheadForBoyerMoore, EatsAtLeast(kMaxLookaheadForBoyerMoore, |
| 3028 | kRecursionBudget, |
| 3029 | not_at_start)); |
| 3030 | if (eats_at_least >= 1) { |
| 3031 | BoyerMooreLookahead* bm = |
| 3032 | new(zone()) BoyerMooreLookahead(eats_at_least, compiler, zone()); |
| 3033 | FillInBMInfo(0, kRecursionBudget, bm, not_at_start); |
| 3034 | if (bm->at(0)->is_non_word()) |
| 3035 | next_is_word_character = Trace::FALSE_VALUE; |
| 3036 | if (bm->at(0)->is_word()) next_is_word_character = Trace::TRUE_VALUE; |
| 3037 | } |
| 3038 | } else { |
| 3039 | if (lookahead->at(0)->is_non_word()) |
| 3040 | next_is_word_character = Trace::FALSE_VALUE; |
| 3041 | if (lookahead->at(0)->is_word()) |
| 3042 | next_is_word_character = Trace::TRUE_VALUE; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3043 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3044 | bool at_boundary = (assertion_type_ == AssertionNode::AT_BOUNDARY); |
| 3045 | if (next_is_word_character == Trace::UNKNOWN) { |
| 3046 | Label before_non_word; |
| 3047 | Label before_word; |
| 3048 | if (trace->characters_preloaded() != 1) { |
| 3049 | assembler->LoadCurrentCharacter(trace->cp_offset(), &before_non_word); |
| 3050 | } |
| 3051 | // Fall through on non-word. |
| 3052 | EmitWordCheck(assembler, &before_word, &before_non_word, false); |
| 3053 | // Next character is not a word character. |
| 3054 | assembler->Bind(&before_non_word); |
| 3055 | Label ok; |
| 3056 | BacktrackIfPrevious(compiler, trace, at_boundary ? kIsNonWord : kIsWord); |
| 3057 | assembler->GoTo(&ok); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3058 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3059 | assembler->Bind(&before_word); |
| 3060 | BacktrackIfPrevious(compiler, trace, at_boundary ? kIsWord : kIsNonWord); |
| 3061 | assembler->Bind(&ok); |
| 3062 | } else if (next_is_word_character == Trace::TRUE_VALUE) { |
| 3063 | BacktrackIfPrevious(compiler, trace, at_boundary ? kIsWord : kIsNonWord); |
| 3064 | } else { |
| 3065 | DCHECK(next_is_word_character == Trace::FALSE_VALUE); |
| 3066 | BacktrackIfPrevious(compiler, trace, at_boundary ? kIsNonWord : kIsWord); |
| 3067 | } |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3068 | } |
| 3069 | |
| 3070 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3071 | void AssertionNode::BacktrackIfPrevious( |
| 3072 | RegExpCompiler* compiler, |
| 3073 | Trace* trace, |
| 3074 | AssertionNode::IfPrevious backtrack_if_previous) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3075 | RegExpMacroAssembler* assembler = compiler->macro_assembler(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3076 | Trace new_trace(*trace); |
| 3077 | new_trace.InvalidateCurrentCharacter(); |
| 3078 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3079 | Label fall_through, dummy; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3080 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3081 | Label* non_word = backtrack_if_previous == kIsNonWord ? |
| 3082 | new_trace.backtrack() : |
| 3083 | &fall_through; |
| 3084 | Label* word = backtrack_if_previous == kIsNonWord ? |
| 3085 | &fall_through : |
| 3086 | new_trace.backtrack(); |
| 3087 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3088 | if (new_trace.cp_offset() == 0) { |
| 3089 | // The start of input counts as a non-word character, so the question is |
| 3090 | // decided if we are at the start. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3091 | assembler->CheckAtStart(non_word); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3092 | } |
| 3093 | // We already checked that we are not at the start of input so it must be |
| 3094 | // OK to load the previous character. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3095 | assembler->LoadCurrentCharacter(new_trace.cp_offset() - 1, &dummy, false); |
| 3096 | EmitWordCheck(assembler, word, non_word, backtrack_if_previous == kIsNonWord); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3097 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3098 | assembler->Bind(&fall_through); |
| 3099 | on_success()->Emit(compiler, &new_trace); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3100 | } |
| 3101 | |
| 3102 | |
| 3103 | void AssertionNode::GetQuickCheckDetails(QuickCheckDetails* details, |
| 3104 | RegExpCompiler* compiler, |
| 3105 | int filled_in, |
| 3106 | bool not_at_start) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3107 | if (assertion_type_ == AT_START && not_at_start) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3108 | details->set_cannot_match(); |
| 3109 | return; |
| 3110 | } |
| 3111 | return on_success()->GetQuickCheckDetails(details, |
| 3112 | compiler, |
| 3113 | filled_in, |
| 3114 | not_at_start); |
| 3115 | } |
| 3116 | |
| 3117 | |
| 3118 | void AssertionNode::Emit(RegExpCompiler* compiler, Trace* trace) { |
| 3119 | RegExpMacroAssembler* assembler = compiler->macro_assembler(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3120 | switch (assertion_type_) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3121 | case AT_END: { |
| 3122 | Label ok; |
| 3123 | assembler->CheckPosition(trace->cp_offset(), &ok); |
| 3124 | assembler->GoTo(trace->backtrack()); |
| 3125 | assembler->Bind(&ok); |
| 3126 | break; |
| 3127 | } |
| 3128 | case AT_START: { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3129 | if (trace->at_start() == Trace::FALSE_VALUE) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3130 | assembler->GoTo(trace->backtrack()); |
| 3131 | return; |
| 3132 | } |
| 3133 | if (trace->at_start() == Trace::UNKNOWN) { |
| 3134 | assembler->CheckNotAtStart(trace->backtrack()); |
| 3135 | Trace at_start_trace = *trace; |
| 3136 | at_start_trace.set_at_start(true); |
| 3137 | on_success()->Emit(compiler, &at_start_trace); |
| 3138 | return; |
| 3139 | } |
| 3140 | } |
| 3141 | break; |
| 3142 | case AFTER_NEWLINE: |
| 3143 | EmitHat(compiler, on_success(), trace); |
| 3144 | return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3145 | case AT_BOUNDARY: |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3146 | case AT_NON_BOUNDARY: { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3147 | EmitBoundaryCheck(compiler, trace); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3148 | return; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3149 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3150 | } |
| 3151 | on_success()->Emit(compiler, trace); |
| 3152 | } |
| 3153 | |
| 3154 | |
| 3155 | static bool DeterminedAlready(QuickCheckDetails* quick_check, int offset) { |
| 3156 | if (quick_check == NULL) return false; |
| 3157 | if (offset >= quick_check->characters()) return false; |
| 3158 | return quick_check->positions(offset)->determines_perfectly; |
| 3159 | } |
| 3160 | |
| 3161 | |
| 3162 | static void UpdateBoundsCheck(int index, int* checked_up_to) { |
| 3163 | if (index > *checked_up_to) { |
| 3164 | *checked_up_to = index; |
| 3165 | } |
| 3166 | } |
| 3167 | |
| 3168 | |
| 3169 | // We call this repeatedly to generate code for each pass over the text node. |
| 3170 | // The passes are in increasing order of difficulty because we hope one |
| 3171 | // of the first passes will fail in which case we are saved the work of the |
| 3172 | // later passes. for example for the case independent regexp /%[asdfghjkl]a/ |
| 3173 | // we will check the '%' in the first pass, the case independent 'a' in the |
| 3174 | // second pass and the character class in the last pass. |
| 3175 | // |
| 3176 | // The passes are done from right to left, so for example to test for /bar/ |
| 3177 | // we will first test for an 'r' with offset 2, then an 'a' with offset 1 |
| 3178 | // and then a 'b' with offset 0. This means we can avoid the end-of-input |
| 3179 | // bounds check most of the time. In the example we only need to check for |
| 3180 | // end-of-input when loading the putative 'r'. |
| 3181 | // |
| 3182 | // A slight complication involves the fact that the first character may already |
| 3183 | // be fetched into a register by the previous node. In this case we want to |
| 3184 | // do the test for that character first. We do this in separate passes. The |
| 3185 | // 'preloaded' argument indicates that we are doing such a 'pass'. If such a |
| 3186 | // pass has been performed then subsequent passes will have true in |
| 3187 | // first_element_checked to indicate that that character does not need to be |
| 3188 | // checked again. |
| 3189 | // |
| 3190 | // In addition to all this we are passed a Trace, which can |
| 3191 | // contain an AlternativeGeneration object. In this AlternativeGeneration |
| 3192 | // object we can see details of any quick check that was already passed in |
| 3193 | // order to get to the code we are now generating. The quick check can involve |
| 3194 | // loading characters, which means we do not need to recheck the bounds |
| 3195 | // up to the limit the quick check already checked. In addition the quick |
| 3196 | // check can have involved a mask and compare operation which may simplify |
| 3197 | // or obviate the need for further checks at some character positions. |
| 3198 | void TextNode::TextEmitPass(RegExpCompiler* compiler, |
| 3199 | TextEmitPassType pass, |
| 3200 | bool preloaded, |
| 3201 | Trace* trace, |
| 3202 | bool first_element_checked, |
| 3203 | int* checked_up_to) { |
| 3204 | RegExpMacroAssembler* assembler = compiler->macro_assembler(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3205 | Isolate* isolate = assembler->zone()->isolate(); |
| 3206 | bool one_byte = compiler->one_byte(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3207 | Label* backtrack = trace->backtrack(); |
| 3208 | QuickCheckDetails* quick_check = trace->quick_check_performed(); |
| 3209 | int element_count = elms_->length(); |
| 3210 | for (int i = preloaded ? 0 : element_count - 1; i >= 0; i--) { |
| 3211 | TextElement elm = elms_->at(i); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3212 | int cp_offset = trace->cp_offset() + elm.cp_offset(); |
| 3213 | if (elm.text_type() == TextElement::ATOM) { |
| 3214 | Vector<const uc16> quarks = elm.atom()->data(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3215 | for (int j = preloaded ? 0 : quarks.length() - 1; j >= 0; j--) { |
| 3216 | if (first_element_checked && i == 0 && j == 0) continue; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3217 | if (DeterminedAlready(quick_check, elm.cp_offset() + j)) continue; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3218 | EmitCharacterFunction* emit_function = NULL; |
| 3219 | switch (pass) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3220 | case NON_LATIN1_MATCH: |
| 3221 | DCHECK(one_byte); |
| 3222 | if (quarks[j] > String::kMaxOneByteCharCode) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3223 | assembler->GoTo(backtrack); |
| 3224 | return; |
| 3225 | } |
| 3226 | break; |
| 3227 | case NON_LETTER_CHARACTER_MATCH: |
| 3228 | emit_function = &EmitAtomNonLetter; |
| 3229 | break; |
| 3230 | case SIMPLE_CHARACTER_MATCH: |
| 3231 | emit_function = &EmitSimpleCharacter; |
| 3232 | break; |
| 3233 | case CASE_CHARACTER_MATCH: |
| 3234 | emit_function = &EmitAtomLetter; |
| 3235 | break; |
| 3236 | default: |
| 3237 | break; |
| 3238 | } |
| 3239 | if (emit_function != NULL) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3240 | bool bound_checked = emit_function(isolate, |
| 3241 | compiler, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3242 | quarks[j], |
| 3243 | backtrack, |
| 3244 | cp_offset + j, |
| 3245 | *checked_up_to < cp_offset + j, |
| 3246 | preloaded); |
| 3247 | if (bound_checked) UpdateBoundsCheck(cp_offset + j, checked_up_to); |
| 3248 | } |
| 3249 | } |
| 3250 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3251 | DCHECK_EQ(TextElement::CHAR_CLASS, elm.text_type()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3252 | if (pass == CHARACTER_CLASS_MATCH) { |
| 3253 | if (first_element_checked && i == 0) continue; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3254 | if (DeterminedAlready(quick_check, elm.cp_offset())) continue; |
| 3255 | RegExpCharacterClass* cc = elm.char_class(); |
| 3256 | EmitCharClass(assembler, cc, one_byte, backtrack, cp_offset, |
| 3257 | *checked_up_to < cp_offset, preloaded, zone()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3258 | UpdateBoundsCheck(cp_offset, checked_up_to); |
| 3259 | } |
| 3260 | } |
| 3261 | } |
| 3262 | } |
| 3263 | |
| 3264 | |
| 3265 | int TextNode::Length() { |
| 3266 | TextElement elm = elms_->last(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3267 | DCHECK(elm.cp_offset() >= 0); |
| 3268 | return elm.cp_offset() + elm.length(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3269 | } |
| 3270 | |
| 3271 | |
| 3272 | bool TextNode::SkipPass(int int_pass, bool ignore_case) { |
| 3273 | TextEmitPassType pass = static_cast<TextEmitPassType>(int_pass); |
| 3274 | if (ignore_case) { |
| 3275 | return pass == SIMPLE_CHARACTER_MATCH; |
| 3276 | } else { |
| 3277 | return pass == NON_LETTER_CHARACTER_MATCH || pass == CASE_CHARACTER_MATCH; |
| 3278 | } |
| 3279 | } |
| 3280 | |
| 3281 | |
| 3282 | // This generates the code to match a text node. A text node can contain |
| 3283 | // straight character sequences (possibly to be matched in a case-independent |
| 3284 | // way) and character classes. For efficiency we do not do this in a single |
| 3285 | // pass from left to right. Instead we pass over the text node several times, |
| 3286 | // emitting code for some character positions every time. See the comment on |
| 3287 | // TextEmitPass for details. |
| 3288 | void TextNode::Emit(RegExpCompiler* compiler, Trace* trace) { |
| 3289 | LimitResult limit_result = LimitVersions(compiler, trace); |
| 3290 | if (limit_result == DONE) return; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3291 | DCHECK(limit_result == CONTINUE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3292 | |
| 3293 | if (trace->cp_offset() + Length() > RegExpMacroAssembler::kMaxCPOffset) { |
| 3294 | compiler->SetRegExpTooBig(); |
| 3295 | return; |
| 3296 | } |
| 3297 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3298 | if (compiler->one_byte()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3299 | int dummy = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3300 | TextEmitPass(compiler, NON_LATIN1_MATCH, false, trace, false, &dummy); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3301 | } |
| 3302 | |
| 3303 | bool first_elt_done = false; |
| 3304 | int bound_checked_to = trace->cp_offset() - 1; |
| 3305 | bound_checked_to += trace->bound_checked_up_to(); |
| 3306 | |
| 3307 | // If a character is preloaded into the current character register then |
| 3308 | // check that now. |
| 3309 | if (trace->characters_preloaded() == 1) { |
| 3310 | for (int pass = kFirstRealPass; pass <= kLastPass; pass++) { |
| 3311 | if (!SkipPass(pass, compiler->ignore_case())) { |
| 3312 | TextEmitPass(compiler, |
| 3313 | static_cast<TextEmitPassType>(pass), |
| 3314 | true, |
| 3315 | trace, |
| 3316 | false, |
| 3317 | &bound_checked_to); |
| 3318 | } |
| 3319 | } |
| 3320 | first_elt_done = true; |
| 3321 | } |
| 3322 | |
| 3323 | for (int pass = kFirstRealPass; pass <= kLastPass; pass++) { |
| 3324 | if (!SkipPass(pass, compiler->ignore_case())) { |
| 3325 | TextEmitPass(compiler, |
| 3326 | static_cast<TextEmitPassType>(pass), |
| 3327 | false, |
| 3328 | trace, |
| 3329 | first_elt_done, |
| 3330 | &bound_checked_to); |
| 3331 | } |
| 3332 | } |
| 3333 | |
| 3334 | Trace successor_trace(*trace); |
| 3335 | successor_trace.set_at_start(false); |
| 3336 | successor_trace.AdvanceCurrentPositionInTrace(Length(), compiler); |
| 3337 | RecursionCheck rc(compiler); |
| 3338 | on_success()->Emit(compiler, &successor_trace); |
| 3339 | } |
| 3340 | |
| 3341 | |
| 3342 | void Trace::InvalidateCurrentCharacter() { |
| 3343 | characters_preloaded_ = 0; |
| 3344 | } |
| 3345 | |
| 3346 | |
| 3347 | void Trace::AdvanceCurrentPositionInTrace(int by, RegExpCompiler* compiler) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3348 | DCHECK(by > 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3349 | // We don't have an instruction for shifting the current character register |
| 3350 | // down or for using a shifted value for anything so lets just forget that |
| 3351 | // we preloaded any characters into it. |
| 3352 | characters_preloaded_ = 0; |
| 3353 | // Adjust the offsets of the quick check performed information. This |
| 3354 | // information is used to find out what we already determined about the |
| 3355 | // characters by means of mask and compare. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3356 | quick_check_performed_.Advance(by, compiler->one_byte()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3357 | cp_offset_ += by; |
| 3358 | if (cp_offset_ > RegExpMacroAssembler::kMaxCPOffset) { |
| 3359 | compiler->SetRegExpTooBig(); |
| 3360 | cp_offset_ = 0; |
| 3361 | } |
| 3362 | bound_checked_up_to_ = Max(0, bound_checked_up_to_ - by); |
| 3363 | } |
| 3364 | |
| 3365 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3366 | void TextNode::MakeCaseIndependent(bool is_one_byte) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3367 | int element_count = elms_->length(); |
| 3368 | for (int i = 0; i < element_count; i++) { |
| 3369 | TextElement elm = elms_->at(i); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3370 | if (elm.text_type() == TextElement::CHAR_CLASS) { |
| 3371 | RegExpCharacterClass* cc = elm.char_class(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3372 | // None of the standard character classes is different in the case |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3373 | // independent case and it slows us down if we don't know that. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3374 | if (cc->is_standard(zone())) continue; |
| 3375 | ZoneList<CharacterRange>* ranges = cc->ranges(zone()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3376 | int range_count = ranges->length(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3377 | for (int j = 0; j < range_count; j++) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3378 | ranges->at(j).AddCaseEquivalents(ranges, is_one_byte, zone()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3379 | } |
| 3380 | } |
| 3381 | } |
| 3382 | } |
| 3383 | |
| 3384 | |
| 3385 | int TextNode::GreedyLoopTextLength() { |
| 3386 | TextElement elm = elms_->at(elms_->length() - 1); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3387 | return elm.cp_offset() + elm.length(); |
| 3388 | } |
| 3389 | |
| 3390 | |
| 3391 | RegExpNode* TextNode::GetSuccessorOfOmnivorousTextNode( |
| 3392 | RegExpCompiler* compiler) { |
| 3393 | if (elms_->length() != 1) return NULL; |
| 3394 | TextElement elm = elms_->at(0); |
| 3395 | if (elm.text_type() != TextElement::CHAR_CLASS) return NULL; |
| 3396 | RegExpCharacterClass* node = elm.char_class(); |
| 3397 | ZoneList<CharacterRange>* ranges = node->ranges(zone()); |
| 3398 | if (!CharacterRange::IsCanonical(ranges)) { |
| 3399 | CharacterRange::Canonicalize(ranges); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3400 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3401 | if (node->is_negated()) { |
| 3402 | return ranges->length() == 0 ? on_success() : NULL; |
| 3403 | } |
| 3404 | if (ranges->length() != 1) return NULL; |
| 3405 | uint32_t max_char; |
| 3406 | if (compiler->one_byte()) { |
| 3407 | max_char = String::kMaxOneByteCharCode; |
| 3408 | } else { |
| 3409 | max_char = String::kMaxUtf16CodeUnit; |
| 3410 | } |
| 3411 | return ranges->at(0).IsEverything(max_char) ? on_success() : NULL; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3412 | } |
| 3413 | |
| 3414 | |
| 3415 | // Finds the fixed match length of a sequence of nodes that goes from |
| 3416 | // this alternative and back to this choice node. If there are variable |
| 3417 | // length nodes or other complications in the way then return a sentinel |
| 3418 | // value indicating that a greedy loop cannot be constructed. |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 3419 | int ChoiceNode::GreedyLoopTextLengthForAlternative( |
| 3420 | GuardedAlternative* alternative) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3421 | int length = 0; |
| 3422 | RegExpNode* node = alternative->node(); |
| 3423 | // Later we will generate code for all these text nodes using recursion |
| 3424 | // so we have to limit the max number. |
| 3425 | int recursion_depth = 0; |
| 3426 | while (node != this) { |
| 3427 | if (recursion_depth++ > RegExpCompiler::kMaxRecursion) { |
| 3428 | return kNodeIsTooComplexForGreedyLoops; |
| 3429 | } |
| 3430 | int node_length = node->GreedyLoopTextLength(); |
| 3431 | if (node_length == kNodeIsTooComplexForGreedyLoops) { |
| 3432 | return kNodeIsTooComplexForGreedyLoops; |
| 3433 | } |
| 3434 | length += node_length; |
| 3435 | SeqRegExpNode* seq_node = static_cast<SeqRegExpNode*>(node); |
| 3436 | node = seq_node->on_success(); |
| 3437 | } |
| 3438 | return length; |
| 3439 | } |
| 3440 | |
| 3441 | |
| 3442 | void LoopChoiceNode::AddLoopAlternative(GuardedAlternative alt) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3443 | DCHECK_EQ(loop_node_, NULL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3444 | AddAlternative(alt); |
| 3445 | loop_node_ = alt.node(); |
| 3446 | } |
| 3447 | |
| 3448 | |
| 3449 | void LoopChoiceNode::AddContinueAlternative(GuardedAlternative alt) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3450 | DCHECK_EQ(continue_node_, NULL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3451 | AddAlternative(alt); |
| 3452 | continue_node_ = alt.node(); |
| 3453 | } |
| 3454 | |
| 3455 | |
| 3456 | void LoopChoiceNode::Emit(RegExpCompiler* compiler, Trace* trace) { |
| 3457 | RegExpMacroAssembler* macro_assembler = compiler->macro_assembler(); |
| 3458 | if (trace->stop_node() == this) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3459 | // Back edge of greedy optimized loop node graph. |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 3460 | int text_length = |
| 3461 | GreedyLoopTextLengthForAlternative(&(alternatives_->at(0))); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3462 | DCHECK(text_length != kNodeIsTooComplexForGreedyLoops); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3463 | // Update the counter-based backtracking info on the stack. This is an |
| 3464 | // optimization for greedy loops (see below). |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3465 | DCHECK(trace->cp_offset() == text_length); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3466 | macro_assembler->AdvanceCurrentPosition(text_length); |
| 3467 | macro_assembler->GoTo(trace->loop_label()); |
| 3468 | return; |
| 3469 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3470 | DCHECK(trace->stop_node() == NULL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3471 | if (!trace->is_trivial()) { |
| 3472 | trace->Flush(compiler, this); |
| 3473 | return; |
| 3474 | } |
| 3475 | ChoiceNode::Emit(compiler, trace); |
| 3476 | } |
| 3477 | |
| 3478 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3479 | int ChoiceNode::CalculatePreloadCharacters(RegExpCompiler* compiler, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3480 | int eats_at_least) { |
| 3481 | int preload_characters = Min(4, eats_at_least); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3482 | if (compiler->macro_assembler()->CanReadUnaligned()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3483 | bool one_byte = compiler->one_byte(); |
| 3484 | if (one_byte) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3485 | if (preload_characters > 4) preload_characters = 4; |
| 3486 | // We can't preload 3 characters because there is no machine instruction |
| 3487 | // to do that. We can't just load 4 because we could be reading |
| 3488 | // beyond the end of the string, which could cause a memory fault. |
| 3489 | if (preload_characters == 3) preload_characters = 2; |
| 3490 | } else { |
| 3491 | if (preload_characters > 2) preload_characters = 2; |
| 3492 | } |
| 3493 | } else { |
| 3494 | if (preload_characters > 1) preload_characters = 1; |
| 3495 | } |
| 3496 | return preload_characters; |
| 3497 | } |
| 3498 | |
| 3499 | |
| 3500 | // This class is used when generating the alternatives in a choice node. It |
| 3501 | // records the way the alternative is being code generated. |
| 3502 | class AlternativeGeneration: public Malloced { |
| 3503 | public: |
| 3504 | AlternativeGeneration() |
| 3505 | : possible_success(), |
| 3506 | expects_preload(false), |
| 3507 | after(), |
| 3508 | quick_check_details() { } |
| 3509 | Label possible_success; |
| 3510 | bool expects_preload; |
| 3511 | Label after; |
| 3512 | QuickCheckDetails quick_check_details; |
| 3513 | }; |
| 3514 | |
| 3515 | |
| 3516 | // Creates a list of AlternativeGenerations. If the list has a reasonable |
| 3517 | // size then it is on the stack, otherwise the excess is on the heap. |
| 3518 | class AlternativeGenerationList { |
| 3519 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3520 | AlternativeGenerationList(int count, Zone* zone) |
| 3521 | : alt_gens_(count, zone) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3522 | for (int i = 0; i < count && i < kAFew; i++) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3523 | alt_gens_.Add(a_few_alt_gens_ + i, zone); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3524 | } |
| 3525 | for (int i = kAFew; i < count; i++) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3526 | alt_gens_.Add(new AlternativeGeneration(), zone); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3527 | } |
| 3528 | } |
| 3529 | ~AlternativeGenerationList() { |
| 3530 | for (int i = kAFew; i < alt_gens_.length(); i++) { |
| 3531 | delete alt_gens_[i]; |
| 3532 | alt_gens_[i] = NULL; |
| 3533 | } |
| 3534 | } |
| 3535 | |
| 3536 | AlternativeGeneration* at(int i) { |
| 3537 | return alt_gens_[i]; |
| 3538 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 3539 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3540 | private: |
| 3541 | static const int kAFew = 10; |
| 3542 | ZoneList<AlternativeGeneration*> alt_gens_; |
| 3543 | AlternativeGeneration a_few_alt_gens_[kAFew]; |
| 3544 | }; |
| 3545 | |
| 3546 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3547 | // The '2' variant is has inclusive from and exclusive to. |
| 3548 | // This covers \s as defined in ECMA-262 5.1, 15.10.2.12, |
| 3549 | // which include WhiteSpace (7.2) or LineTerminator (7.3) values. |
| 3550 | static const int kSpaceRanges[] = { '\t', '\r' + 1, ' ', ' ' + 1, |
| 3551 | 0x00A0, 0x00A1, 0x1680, 0x1681, 0x180E, 0x180F, 0x2000, 0x200B, |
| 3552 | 0x2028, 0x202A, 0x202F, 0x2030, 0x205F, 0x2060, 0x3000, 0x3001, |
| 3553 | 0xFEFF, 0xFF00, 0x10000 }; |
| 3554 | static const int kSpaceRangeCount = arraysize(kSpaceRanges); |
| 3555 | |
| 3556 | static const int kWordRanges[] = { |
| 3557 | '0', '9' + 1, 'A', 'Z' + 1, '_', '_' + 1, 'a', 'z' + 1, 0x10000 }; |
| 3558 | static const int kWordRangeCount = arraysize(kWordRanges); |
| 3559 | static const int kDigitRanges[] = { '0', '9' + 1, 0x10000 }; |
| 3560 | static const int kDigitRangeCount = arraysize(kDigitRanges); |
| 3561 | static const int kSurrogateRanges[] = { 0xd800, 0xe000, 0x10000 }; |
| 3562 | static const int kSurrogateRangeCount = arraysize(kSurrogateRanges); |
| 3563 | static const int kLineTerminatorRanges[] = { 0x000A, 0x000B, 0x000D, 0x000E, |
| 3564 | 0x2028, 0x202A, 0x10000 }; |
| 3565 | static const int kLineTerminatorRangeCount = arraysize(kLineTerminatorRanges); |
| 3566 | |
| 3567 | |
| 3568 | void BoyerMoorePositionInfo::Set(int character) { |
| 3569 | SetInterval(Interval(character, character)); |
| 3570 | } |
| 3571 | |
| 3572 | |
| 3573 | void BoyerMoorePositionInfo::SetInterval(const Interval& interval) { |
| 3574 | s_ = AddRange(s_, kSpaceRanges, kSpaceRangeCount, interval); |
| 3575 | w_ = AddRange(w_, kWordRanges, kWordRangeCount, interval); |
| 3576 | d_ = AddRange(d_, kDigitRanges, kDigitRangeCount, interval); |
| 3577 | surrogate_ = |
| 3578 | AddRange(surrogate_, kSurrogateRanges, kSurrogateRangeCount, interval); |
| 3579 | if (interval.to() - interval.from() >= kMapSize - 1) { |
| 3580 | if (map_count_ != kMapSize) { |
| 3581 | map_count_ = kMapSize; |
| 3582 | for (int i = 0; i < kMapSize; i++) map_->at(i) = true; |
| 3583 | } |
| 3584 | return; |
| 3585 | } |
| 3586 | for (int i = interval.from(); i <= interval.to(); i++) { |
| 3587 | int mod_character = (i & kMask); |
| 3588 | if (!map_->at(mod_character)) { |
| 3589 | map_count_++; |
| 3590 | map_->at(mod_character) = true; |
| 3591 | } |
| 3592 | if (map_count_ == kMapSize) return; |
| 3593 | } |
| 3594 | } |
| 3595 | |
| 3596 | |
| 3597 | void BoyerMoorePositionInfo::SetAll() { |
| 3598 | s_ = w_ = d_ = kLatticeUnknown; |
| 3599 | if (map_count_ != kMapSize) { |
| 3600 | map_count_ = kMapSize; |
| 3601 | for (int i = 0; i < kMapSize; i++) map_->at(i) = true; |
| 3602 | } |
| 3603 | } |
| 3604 | |
| 3605 | |
| 3606 | BoyerMooreLookahead::BoyerMooreLookahead( |
| 3607 | int length, RegExpCompiler* compiler, Zone* zone) |
| 3608 | : length_(length), |
| 3609 | compiler_(compiler) { |
| 3610 | if (compiler->one_byte()) { |
| 3611 | max_char_ = String::kMaxOneByteCharCode; |
| 3612 | } else { |
| 3613 | max_char_ = String::kMaxUtf16CodeUnit; |
| 3614 | } |
| 3615 | bitmaps_ = new(zone) ZoneList<BoyerMoorePositionInfo*>(length, zone); |
| 3616 | for (int i = 0; i < length; i++) { |
| 3617 | bitmaps_->Add(new(zone) BoyerMoorePositionInfo(zone), zone); |
| 3618 | } |
| 3619 | } |
| 3620 | |
| 3621 | |
| 3622 | // Find the longest range of lookahead that has the fewest number of different |
| 3623 | // characters that can occur at a given position. Since we are optimizing two |
| 3624 | // different parameters at once this is a tradeoff. |
| 3625 | bool BoyerMooreLookahead::FindWorthwhileInterval(int* from, int* to) { |
| 3626 | int biggest_points = 0; |
| 3627 | // If more than 32 characters out of 128 can occur it is unlikely that we can |
| 3628 | // be lucky enough to step forwards much of the time. |
| 3629 | const int kMaxMax = 32; |
| 3630 | for (int max_number_of_chars = 4; |
| 3631 | max_number_of_chars < kMaxMax; |
| 3632 | max_number_of_chars *= 2) { |
| 3633 | biggest_points = |
| 3634 | FindBestInterval(max_number_of_chars, biggest_points, from, to); |
| 3635 | } |
| 3636 | if (biggest_points == 0) return false; |
| 3637 | return true; |
| 3638 | } |
| 3639 | |
| 3640 | |
| 3641 | // Find the highest-points range between 0 and length_ where the character |
| 3642 | // information is not too vague. 'Too vague' means that there are more than |
| 3643 | // max_number_of_chars that can occur at this position. Calculates the number |
| 3644 | // of points as the product of width-of-the-range and |
| 3645 | // probability-of-finding-one-of-the-characters, where the probability is |
| 3646 | // calculated using the frequency distribution of the sample subject string. |
| 3647 | int BoyerMooreLookahead::FindBestInterval( |
| 3648 | int max_number_of_chars, int old_biggest_points, int* from, int* to) { |
| 3649 | int biggest_points = old_biggest_points; |
| 3650 | static const int kSize = RegExpMacroAssembler::kTableSize; |
| 3651 | for (int i = 0; i < length_; ) { |
| 3652 | while (i < length_ && Count(i) > max_number_of_chars) i++; |
| 3653 | if (i == length_) break; |
| 3654 | int remembered_from = i; |
| 3655 | bool union_map[kSize]; |
| 3656 | for (int j = 0; j < kSize; j++) union_map[j] = false; |
| 3657 | while (i < length_ && Count(i) <= max_number_of_chars) { |
| 3658 | BoyerMoorePositionInfo* map = bitmaps_->at(i); |
| 3659 | for (int j = 0; j < kSize; j++) union_map[j] |= map->at(j); |
| 3660 | i++; |
| 3661 | } |
| 3662 | int frequency = 0; |
| 3663 | for (int j = 0; j < kSize; j++) { |
| 3664 | if (union_map[j]) { |
| 3665 | // Add 1 to the frequency to give a small per-character boost for |
| 3666 | // the cases where our sampling is not good enough and many |
| 3667 | // characters have a frequency of zero. This means the frequency |
| 3668 | // can theoretically be up to 2*kSize though we treat it mostly as |
| 3669 | // a fraction of kSize. |
| 3670 | frequency += compiler_->frequency_collator()->Frequency(j) + 1; |
| 3671 | } |
| 3672 | } |
| 3673 | // We use the probability of skipping times the distance we are skipping to |
| 3674 | // judge the effectiveness of this. Actually we have a cut-off: By |
| 3675 | // dividing by 2 we switch off the skipping if the probability of skipping |
| 3676 | // is less than 50%. This is because the multibyte mask-and-compare |
| 3677 | // skipping in quickcheck is more likely to do well on this case. |
| 3678 | bool in_quickcheck_range = |
| 3679 | ((i - remembered_from < 4) || |
| 3680 | (compiler_->one_byte() ? remembered_from <= 4 : remembered_from <= 2)); |
| 3681 | // Called 'probability' but it is only a rough estimate and can actually |
| 3682 | // be outside the 0-kSize range. |
| 3683 | int probability = (in_quickcheck_range ? kSize / 2 : kSize) - frequency; |
| 3684 | int points = (i - remembered_from) * probability; |
| 3685 | if (points > biggest_points) { |
| 3686 | *from = remembered_from; |
| 3687 | *to = i - 1; |
| 3688 | biggest_points = points; |
| 3689 | } |
| 3690 | } |
| 3691 | return biggest_points; |
| 3692 | } |
| 3693 | |
| 3694 | |
| 3695 | // Take all the characters that will not prevent a successful match if they |
| 3696 | // occur in the subject string in the range between min_lookahead and |
| 3697 | // max_lookahead (inclusive) measured from the current position. If the |
| 3698 | // character at max_lookahead offset is not one of these characters, then we |
| 3699 | // can safely skip forwards by the number of characters in the range. |
| 3700 | int BoyerMooreLookahead::GetSkipTable(int min_lookahead, |
| 3701 | int max_lookahead, |
| 3702 | Handle<ByteArray> boolean_skip_table) { |
| 3703 | const int kSize = RegExpMacroAssembler::kTableSize; |
| 3704 | |
| 3705 | const int kSkipArrayEntry = 0; |
| 3706 | const int kDontSkipArrayEntry = 1; |
| 3707 | |
| 3708 | for (int i = 0; i < kSize; i++) { |
| 3709 | boolean_skip_table->set(i, kSkipArrayEntry); |
| 3710 | } |
| 3711 | int skip = max_lookahead + 1 - min_lookahead; |
| 3712 | |
| 3713 | for (int i = max_lookahead; i >= min_lookahead; i--) { |
| 3714 | BoyerMoorePositionInfo* map = bitmaps_->at(i); |
| 3715 | for (int j = 0; j < kSize; j++) { |
| 3716 | if (map->at(j)) { |
| 3717 | boolean_skip_table->set(j, kDontSkipArrayEntry); |
| 3718 | } |
| 3719 | } |
| 3720 | } |
| 3721 | |
| 3722 | return skip; |
| 3723 | } |
| 3724 | |
| 3725 | |
| 3726 | // See comment above on the implementation of GetSkipTable. |
| 3727 | void BoyerMooreLookahead::EmitSkipInstructions(RegExpMacroAssembler* masm) { |
| 3728 | const int kSize = RegExpMacroAssembler::kTableSize; |
| 3729 | |
| 3730 | int min_lookahead = 0; |
| 3731 | int max_lookahead = 0; |
| 3732 | |
| 3733 | if (!FindWorthwhileInterval(&min_lookahead, &max_lookahead)) return; |
| 3734 | |
| 3735 | bool found_single_character = false; |
| 3736 | int single_character = 0; |
| 3737 | for (int i = max_lookahead; i >= min_lookahead; i--) { |
| 3738 | BoyerMoorePositionInfo* map = bitmaps_->at(i); |
| 3739 | if (map->map_count() > 1 || |
| 3740 | (found_single_character && map->map_count() != 0)) { |
| 3741 | found_single_character = false; |
| 3742 | break; |
| 3743 | } |
| 3744 | for (int j = 0; j < kSize; j++) { |
| 3745 | if (map->at(j)) { |
| 3746 | found_single_character = true; |
| 3747 | single_character = j; |
| 3748 | break; |
| 3749 | } |
| 3750 | } |
| 3751 | } |
| 3752 | |
| 3753 | int lookahead_width = max_lookahead + 1 - min_lookahead; |
| 3754 | |
| 3755 | if (found_single_character && lookahead_width == 1 && max_lookahead < 3) { |
| 3756 | // The mask-compare can probably handle this better. |
| 3757 | return; |
| 3758 | } |
| 3759 | |
| 3760 | if (found_single_character) { |
| 3761 | Label cont, again; |
| 3762 | masm->Bind(&again); |
| 3763 | masm->LoadCurrentCharacter(max_lookahead, &cont, true); |
| 3764 | if (max_char_ > kSize) { |
| 3765 | masm->CheckCharacterAfterAnd(single_character, |
| 3766 | RegExpMacroAssembler::kTableMask, |
| 3767 | &cont); |
| 3768 | } else { |
| 3769 | masm->CheckCharacter(single_character, &cont); |
| 3770 | } |
| 3771 | masm->AdvanceCurrentPosition(lookahead_width); |
| 3772 | masm->GoTo(&again); |
| 3773 | masm->Bind(&cont); |
| 3774 | return; |
| 3775 | } |
| 3776 | |
| 3777 | Factory* factory = masm->zone()->isolate()->factory(); |
| 3778 | Handle<ByteArray> boolean_skip_table = factory->NewByteArray(kSize, TENURED); |
| 3779 | int skip_distance = GetSkipTable( |
| 3780 | min_lookahead, max_lookahead, boolean_skip_table); |
| 3781 | DCHECK(skip_distance != 0); |
| 3782 | |
| 3783 | Label cont, again; |
| 3784 | masm->Bind(&again); |
| 3785 | masm->LoadCurrentCharacter(max_lookahead, &cont, true); |
| 3786 | masm->CheckBitInTable(boolean_skip_table, &cont); |
| 3787 | masm->AdvanceCurrentPosition(skip_distance); |
| 3788 | masm->GoTo(&again); |
| 3789 | masm->Bind(&cont); |
| 3790 | } |
| 3791 | |
| 3792 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3793 | /* Code generation for choice nodes. |
| 3794 | * |
| 3795 | * We generate quick checks that do a mask and compare to eliminate a |
| 3796 | * choice. If the quick check succeeds then it jumps to the continuation to |
| 3797 | * do slow checks and check subsequent nodes. If it fails (the common case) |
| 3798 | * it falls through to the next choice. |
| 3799 | * |
| 3800 | * Here is the desired flow graph. Nodes directly below each other imply |
| 3801 | * fallthrough. Alternatives 1 and 2 have quick checks. Alternative |
| 3802 | * 3 doesn't have a quick check so we have to call the slow check. |
| 3803 | * Nodes are marked Qn for quick checks and Sn for slow checks. The entire |
| 3804 | * regexp continuation is generated directly after the Sn node, up to the |
| 3805 | * next GoTo if we decide to reuse some already generated code. Some |
| 3806 | * nodes expect preload_characters to be preloaded into the current |
| 3807 | * character register. R nodes do this preloading. Vertices are marked |
| 3808 | * F for failures and S for success (possible success in the case of quick |
| 3809 | * nodes). L, V, < and > are used as arrow heads. |
| 3810 | * |
| 3811 | * ----------> R |
| 3812 | * | |
| 3813 | * V |
| 3814 | * Q1 -----> S1 |
| 3815 | * | S / |
| 3816 | * F| / |
| 3817 | * | F/ |
| 3818 | * | / |
| 3819 | * | R |
| 3820 | * | / |
| 3821 | * V L |
| 3822 | * Q2 -----> S2 |
| 3823 | * | S / |
| 3824 | * F| / |
| 3825 | * | F/ |
| 3826 | * | / |
| 3827 | * | R |
| 3828 | * | / |
| 3829 | * V L |
| 3830 | * S3 |
| 3831 | * | |
| 3832 | * F| |
| 3833 | * | |
| 3834 | * R |
| 3835 | * | |
| 3836 | * backtrack V |
| 3837 | * <----------Q4 |
| 3838 | * \ F | |
| 3839 | * \ |S |
| 3840 | * \ F V |
| 3841 | * \-----S4 |
| 3842 | * |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3843 | * For greedy loops we push the current position, then generate the code that |
| 3844 | * eats the input specially in EmitGreedyLoop. The other choice (the |
| 3845 | * continuation) is generated by the normal code in EmitChoices, and steps back |
| 3846 | * in the input to the starting position when it fails to match. The loop code |
| 3847 | * looks like this (U is the unwind code that steps back in the greedy loop). |
| 3848 | * |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3849 | * _____ |
| 3850 | * / \ |
| 3851 | * V | |
| 3852 | * ----------> S1 | |
| 3853 | * /| | |
| 3854 | * / |S | |
| 3855 | * F/ \_____/ |
| 3856 | * / |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3857 | * |<----- |
| 3858 | * | \ |
| 3859 | * V |S |
| 3860 | * Q2 ---> U----->backtrack |
| 3861 | * | F / |
| 3862 | * S| / |
| 3863 | * V F / |
| 3864 | * S2--/ |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3865 | */ |
| 3866 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3867 | GreedyLoopState::GreedyLoopState(bool not_at_start) { |
| 3868 | counter_backtrack_trace_.set_backtrack(&label_); |
| 3869 | if (not_at_start) counter_backtrack_trace_.set_at_start(false); |
| 3870 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3871 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3872 | |
| 3873 | void ChoiceNode::AssertGuardsMentionRegisters(Trace* trace) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3874 | #ifdef DEBUG |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3875 | int choice_count = alternatives_->length(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3876 | for (int i = 0; i < choice_count - 1; i++) { |
| 3877 | GuardedAlternative alternative = alternatives_->at(i); |
| 3878 | ZoneList<Guard*>* guards = alternative.guards(); |
| 3879 | int guard_count = (guards == NULL) ? 0 : guards->length(); |
| 3880 | for (int j = 0; j < guard_count; j++) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3881 | DCHECK(!trace->mentions_reg(guards->at(j)->reg())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3882 | } |
| 3883 | } |
| 3884 | #endif |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3885 | } |
| 3886 | |
| 3887 | |
| 3888 | void ChoiceNode::SetUpPreLoad(RegExpCompiler* compiler, |
| 3889 | Trace* current_trace, |
| 3890 | PreloadState* state) { |
| 3891 | if (state->eats_at_least_ == PreloadState::kEatsAtLeastNotYetInitialized) { |
| 3892 | // Save some time by looking at most one machine word ahead. |
| 3893 | state->eats_at_least_ = |
| 3894 | EatsAtLeast(compiler->one_byte() ? 4 : 2, kRecursionBudget, |
| 3895 | current_trace->at_start() == Trace::FALSE_VALUE); |
| 3896 | } |
| 3897 | state->preload_characters_ = |
| 3898 | CalculatePreloadCharacters(compiler, state->eats_at_least_); |
| 3899 | |
| 3900 | state->preload_is_current_ = |
| 3901 | (current_trace->characters_preloaded() == state->preload_characters_); |
| 3902 | state->preload_has_checked_bounds_ = state->preload_is_current_; |
| 3903 | } |
| 3904 | |
| 3905 | |
| 3906 | void ChoiceNode::Emit(RegExpCompiler* compiler, Trace* trace) { |
| 3907 | int choice_count = alternatives_->length(); |
| 3908 | |
| 3909 | AssertGuardsMentionRegisters(trace); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3910 | |
| 3911 | LimitResult limit_result = LimitVersions(compiler, trace); |
| 3912 | if (limit_result == DONE) return; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3913 | DCHECK(limit_result == CONTINUE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3914 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3915 | // For loop nodes we already flushed (see LoopChoiceNode::Emit), but for |
| 3916 | // other choice nodes we only flush if we are out of code size budget. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3917 | if (trace->flush_budget() == 0 && trace->actions() != NULL) { |
| 3918 | trace->Flush(compiler, this); |
| 3919 | return; |
| 3920 | } |
| 3921 | |
| 3922 | RecursionCheck rc(compiler); |
| 3923 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3924 | PreloadState preload; |
| 3925 | preload.init(); |
| 3926 | GreedyLoopState greedy_loop_state(not_at_start()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3927 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3928 | int text_length = GreedyLoopTextLengthForAlternative(&alternatives_->at(0)); |
| 3929 | AlternativeGenerationList alt_gens(choice_count, zone()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3930 | |
| 3931 | if (choice_count > 1 && text_length != kNodeIsTooComplexForGreedyLoops) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3932 | trace = EmitGreedyLoop(compiler, |
| 3933 | trace, |
| 3934 | &alt_gens, |
| 3935 | &preload, |
| 3936 | &greedy_loop_state, |
| 3937 | text_length); |
| 3938 | } else { |
| 3939 | // TODO(erikcorry): Delete this. We don't need this label, but it makes us |
| 3940 | // match the traces produced pre-cleanup. |
| 3941 | Label second_choice; |
| 3942 | compiler->macro_assembler()->Bind(&second_choice); |
| 3943 | |
| 3944 | preload.eats_at_least_ = EmitOptimizedUnanchoredSearch(compiler, trace); |
| 3945 | |
| 3946 | EmitChoices(compiler, |
| 3947 | &alt_gens, |
| 3948 | 0, |
| 3949 | trace, |
| 3950 | &preload); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3951 | } |
| 3952 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3953 | // At this point we need to generate slow checks for the alternatives where |
| 3954 | // the quick check was inlined. We can recognize these because the associated |
| 3955 | // label was bound. |
| 3956 | int new_flush_budget = trace->flush_budget() / choice_count; |
| 3957 | for (int i = 0; i < choice_count; i++) { |
| 3958 | AlternativeGeneration* alt_gen = alt_gens.at(i); |
| 3959 | Trace new_trace(*trace); |
| 3960 | // If there are actions to be flushed we have to limit how many times |
| 3961 | // they are flushed. Take the budget of the parent trace and distribute |
| 3962 | // it fairly amongst the children. |
| 3963 | if (new_trace.actions() != NULL) { |
| 3964 | new_trace.set_flush_budget(new_flush_budget); |
| 3965 | } |
| 3966 | bool next_expects_preload = |
| 3967 | i == choice_count - 1 ? false : alt_gens.at(i + 1)->expects_preload; |
| 3968 | EmitOutOfLineContinuation(compiler, |
| 3969 | &new_trace, |
| 3970 | alternatives_->at(i), |
| 3971 | alt_gen, |
| 3972 | preload.preload_characters_, |
| 3973 | next_expects_preload); |
| 3974 | } |
| 3975 | } |
| 3976 | |
| 3977 | |
| 3978 | Trace* ChoiceNode::EmitGreedyLoop(RegExpCompiler* compiler, |
| 3979 | Trace* trace, |
| 3980 | AlternativeGenerationList* alt_gens, |
| 3981 | PreloadState* preload, |
| 3982 | GreedyLoopState* greedy_loop_state, |
| 3983 | int text_length) { |
| 3984 | RegExpMacroAssembler* macro_assembler = compiler->macro_assembler(); |
| 3985 | // Here we have special handling for greedy loops containing only text nodes |
| 3986 | // and other simple nodes. These are handled by pushing the current |
| 3987 | // position on the stack and then incrementing the current position each |
| 3988 | // time around the switch. On backtrack we decrement the current position |
| 3989 | // and check it against the pushed value. This avoids pushing backtrack |
| 3990 | // information for each iteration of the loop, which could take up a lot of |
| 3991 | // space. |
| 3992 | DCHECK(trace->stop_node() == NULL); |
| 3993 | macro_assembler->PushCurrentPosition(); |
| 3994 | Label greedy_match_failed; |
| 3995 | Trace greedy_match_trace; |
| 3996 | if (not_at_start()) greedy_match_trace.set_at_start(false); |
| 3997 | greedy_match_trace.set_backtrack(&greedy_match_failed); |
| 3998 | Label loop_label; |
| 3999 | macro_assembler->Bind(&loop_label); |
| 4000 | greedy_match_trace.set_stop_node(this); |
| 4001 | greedy_match_trace.set_loop_label(&loop_label); |
| 4002 | alternatives_->at(0).node()->Emit(compiler, &greedy_match_trace); |
| 4003 | macro_assembler->Bind(&greedy_match_failed); |
| 4004 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4005 | Label second_choice; // For use in greedy matches. |
| 4006 | macro_assembler->Bind(&second_choice); |
| 4007 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4008 | Trace* new_trace = greedy_loop_state->counter_backtrack_trace(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4009 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4010 | EmitChoices(compiler, |
| 4011 | alt_gens, |
| 4012 | 1, |
| 4013 | new_trace, |
| 4014 | preload); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4015 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4016 | macro_assembler->Bind(greedy_loop_state->label()); |
| 4017 | // If we have unwound to the bottom then backtrack. |
| 4018 | macro_assembler->CheckGreedyLoop(trace->backtrack()); |
| 4019 | // Otherwise try the second priority at an earlier position. |
| 4020 | macro_assembler->AdvanceCurrentPosition(-text_length); |
| 4021 | macro_assembler->GoTo(&second_choice); |
| 4022 | return new_trace; |
| 4023 | } |
| 4024 | |
| 4025 | int ChoiceNode::EmitOptimizedUnanchoredSearch(RegExpCompiler* compiler, |
| 4026 | Trace* trace) { |
| 4027 | int eats_at_least = PreloadState::kEatsAtLeastNotYetInitialized; |
| 4028 | if (alternatives_->length() != 2) return eats_at_least; |
| 4029 | |
| 4030 | GuardedAlternative alt1 = alternatives_->at(1); |
| 4031 | if (alt1.guards() != NULL && alt1.guards()->length() != 0) { |
| 4032 | return eats_at_least; |
| 4033 | } |
| 4034 | RegExpNode* eats_anything_node = alt1.node(); |
| 4035 | if (eats_anything_node->GetSuccessorOfOmnivorousTextNode(compiler) != this) { |
| 4036 | return eats_at_least; |
| 4037 | } |
| 4038 | |
| 4039 | // Really we should be creating a new trace when we execute this function, |
| 4040 | // but there is no need, because the code it generates cannot backtrack, and |
| 4041 | // we always arrive here with a trivial trace (since it's the entry to a |
| 4042 | // loop. That also implies that there are no preloaded characters, which is |
| 4043 | // good, because it means we won't be violating any assumptions by |
| 4044 | // overwriting those characters with new load instructions. |
| 4045 | DCHECK(trace->is_trivial()); |
| 4046 | |
| 4047 | RegExpMacroAssembler* macro_assembler = compiler->macro_assembler(); |
| 4048 | // At this point we know that we are at a non-greedy loop that will eat |
| 4049 | // any character one at a time. Any non-anchored regexp has such a |
| 4050 | // loop prepended to it in order to find where it starts. We look for |
| 4051 | // a pattern of the form ...abc... where we can look 6 characters ahead |
| 4052 | // and step forwards 3 if the character is not one of abc. Abc need |
| 4053 | // not be atoms, they can be any reasonably limited character class or |
| 4054 | // small alternation. |
| 4055 | BoyerMooreLookahead* bm = bm_info(false); |
| 4056 | if (bm == NULL) { |
| 4057 | eats_at_least = Min(kMaxLookaheadForBoyerMoore, |
| 4058 | EatsAtLeast(kMaxLookaheadForBoyerMoore, |
| 4059 | kRecursionBudget, |
| 4060 | false)); |
| 4061 | if (eats_at_least >= 1) { |
| 4062 | bm = new(zone()) BoyerMooreLookahead(eats_at_least, |
| 4063 | compiler, |
| 4064 | zone()); |
| 4065 | GuardedAlternative alt0 = alternatives_->at(0); |
| 4066 | alt0.node()->FillInBMInfo(0, kRecursionBudget, bm, false); |
| 4067 | } |
| 4068 | } |
| 4069 | if (bm != NULL) { |
| 4070 | bm->EmitSkipInstructions(macro_assembler); |
| 4071 | } |
| 4072 | return eats_at_least; |
| 4073 | } |
| 4074 | |
| 4075 | |
| 4076 | void ChoiceNode::EmitChoices(RegExpCompiler* compiler, |
| 4077 | AlternativeGenerationList* alt_gens, |
| 4078 | int first_choice, |
| 4079 | Trace* trace, |
| 4080 | PreloadState* preload) { |
| 4081 | RegExpMacroAssembler* macro_assembler = compiler->macro_assembler(); |
| 4082 | SetUpPreLoad(compiler, trace, preload); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4083 | |
| 4084 | // For now we just call all choices one after the other. The idea ultimately |
| 4085 | // is to use the Dispatch table to try only the relevant ones. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4086 | int choice_count = alternatives_->length(); |
| 4087 | |
| 4088 | int new_flush_budget = trace->flush_budget() / choice_count; |
| 4089 | |
| 4090 | for (int i = first_choice; i < choice_count; i++) { |
| 4091 | bool is_last = i == choice_count - 1; |
| 4092 | bool fall_through_on_failure = !is_last; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4093 | GuardedAlternative alternative = alternatives_->at(i); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4094 | AlternativeGeneration* alt_gen = alt_gens->at(i); |
| 4095 | alt_gen->quick_check_details.set_characters(preload->preload_characters_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4096 | ZoneList<Guard*>* guards = alternative.guards(); |
| 4097 | int guard_count = (guards == NULL) ? 0 : guards->length(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4098 | Trace new_trace(*trace); |
| 4099 | new_trace.set_characters_preloaded(preload->preload_is_current_ ? |
| 4100 | preload->preload_characters_ : |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4101 | 0); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4102 | if (preload->preload_has_checked_bounds_) { |
| 4103 | new_trace.set_bound_checked_up_to(preload->preload_characters_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4104 | } |
| 4105 | new_trace.quick_check_performed()->Clear(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4106 | if (not_at_start_) new_trace.set_at_start(Trace::FALSE_VALUE); |
| 4107 | if (!is_last) { |
| 4108 | new_trace.set_backtrack(&alt_gen->after); |
| 4109 | } |
| 4110 | alt_gen->expects_preload = preload->preload_is_current_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4111 | bool generate_full_check_inline = false; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 4112 | if (compiler->optimize() && |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4113 | try_to_emit_quick_check_for_alternative(i == 0) && |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 4114 | alternative.node()->EmitQuickCheck( |
| 4115 | compiler, trace, &new_trace, preload->preload_has_checked_bounds_, |
| 4116 | &alt_gen->possible_success, &alt_gen->quick_check_details, |
| 4117 | fall_through_on_failure)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4118 | // Quick check was generated for this choice. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4119 | preload->preload_is_current_ = true; |
| 4120 | preload->preload_has_checked_bounds_ = true; |
| 4121 | // If we generated the quick check to fall through on possible success, |
| 4122 | // we now need to generate the full check inline. |
| 4123 | if (!fall_through_on_failure) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4124 | macro_assembler->Bind(&alt_gen->possible_success); |
| 4125 | new_trace.set_quick_check_performed(&alt_gen->quick_check_details); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4126 | new_trace.set_characters_preloaded(preload->preload_characters_); |
| 4127 | new_trace.set_bound_checked_up_to(preload->preload_characters_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4128 | generate_full_check_inline = true; |
| 4129 | } |
| 4130 | } else if (alt_gen->quick_check_details.cannot_match()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4131 | if (!fall_through_on_failure) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4132 | macro_assembler->GoTo(trace->backtrack()); |
| 4133 | } |
| 4134 | continue; |
| 4135 | } else { |
| 4136 | // No quick check was generated. Put the full code here. |
| 4137 | // If this is not the first choice then there could be slow checks from |
| 4138 | // previous cases that go here when they fail. There's no reason to |
| 4139 | // insist that they preload characters since the slow check we are about |
| 4140 | // to generate probably can't use it. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4141 | if (i != first_choice) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4142 | alt_gen->expects_preload = false; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4143 | new_trace.InvalidateCurrentCharacter(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4144 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4145 | generate_full_check_inline = true; |
| 4146 | } |
| 4147 | if (generate_full_check_inline) { |
| 4148 | if (new_trace.actions() != NULL) { |
| 4149 | new_trace.set_flush_budget(new_flush_budget); |
| 4150 | } |
| 4151 | for (int j = 0; j < guard_count; j++) { |
| 4152 | GenerateGuard(macro_assembler, guards->at(j), &new_trace); |
| 4153 | } |
| 4154 | alternative.node()->Emit(compiler, &new_trace); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4155 | preload->preload_is_current_ = false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4156 | } |
| 4157 | macro_assembler->Bind(&alt_gen->after); |
| 4158 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4159 | } |
| 4160 | |
| 4161 | |
| 4162 | void ChoiceNode::EmitOutOfLineContinuation(RegExpCompiler* compiler, |
| 4163 | Trace* trace, |
| 4164 | GuardedAlternative alternative, |
| 4165 | AlternativeGeneration* alt_gen, |
| 4166 | int preload_characters, |
| 4167 | bool next_expects_preload) { |
| 4168 | if (!alt_gen->possible_success.is_linked()) return; |
| 4169 | |
| 4170 | RegExpMacroAssembler* macro_assembler = compiler->macro_assembler(); |
| 4171 | macro_assembler->Bind(&alt_gen->possible_success); |
| 4172 | Trace out_of_line_trace(*trace); |
| 4173 | out_of_line_trace.set_characters_preloaded(preload_characters); |
| 4174 | out_of_line_trace.set_quick_check_performed(&alt_gen->quick_check_details); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4175 | if (not_at_start_) out_of_line_trace.set_at_start(Trace::FALSE_VALUE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4176 | ZoneList<Guard*>* guards = alternative.guards(); |
| 4177 | int guard_count = (guards == NULL) ? 0 : guards->length(); |
| 4178 | if (next_expects_preload) { |
| 4179 | Label reload_current_char; |
| 4180 | out_of_line_trace.set_backtrack(&reload_current_char); |
| 4181 | for (int j = 0; j < guard_count; j++) { |
| 4182 | GenerateGuard(macro_assembler, guards->at(j), &out_of_line_trace); |
| 4183 | } |
| 4184 | alternative.node()->Emit(compiler, &out_of_line_trace); |
| 4185 | macro_assembler->Bind(&reload_current_char); |
| 4186 | // Reload the current character, since the next quick check expects that. |
| 4187 | // We don't need to check bounds here because we only get into this |
| 4188 | // code through a quick check which already did the checked load. |
| 4189 | macro_assembler->LoadCurrentCharacter(trace->cp_offset(), |
| 4190 | NULL, |
| 4191 | false, |
| 4192 | preload_characters); |
| 4193 | macro_assembler->GoTo(&(alt_gen->after)); |
| 4194 | } else { |
| 4195 | out_of_line_trace.set_backtrack(&(alt_gen->after)); |
| 4196 | for (int j = 0; j < guard_count; j++) { |
| 4197 | GenerateGuard(macro_assembler, guards->at(j), &out_of_line_trace); |
| 4198 | } |
| 4199 | alternative.node()->Emit(compiler, &out_of_line_trace); |
| 4200 | } |
| 4201 | } |
| 4202 | |
| 4203 | |
| 4204 | void ActionNode::Emit(RegExpCompiler* compiler, Trace* trace) { |
| 4205 | RegExpMacroAssembler* assembler = compiler->macro_assembler(); |
| 4206 | LimitResult limit_result = LimitVersions(compiler, trace); |
| 4207 | if (limit_result == DONE) return; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4208 | DCHECK(limit_result == CONTINUE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4209 | |
| 4210 | RecursionCheck rc(compiler); |
| 4211 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4212 | switch (action_type_) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4213 | case STORE_POSITION: { |
| 4214 | Trace::DeferredCapture |
| 4215 | new_capture(data_.u_position_register.reg, |
| 4216 | data_.u_position_register.is_capture, |
| 4217 | trace); |
| 4218 | Trace new_trace = *trace; |
| 4219 | new_trace.add_action(&new_capture); |
| 4220 | on_success()->Emit(compiler, &new_trace); |
| 4221 | break; |
| 4222 | } |
| 4223 | case INCREMENT_REGISTER: { |
| 4224 | Trace::DeferredIncrementRegister |
| 4225 | new_increment(data_.u_increment_register.reg); |
| 4226 | Trace new_trace = *trace; |
| 4227 | new_trace.add_action(&new_increment); |
| 4228 | on_success()->Emit(compiler, &new_trace); |
| 4229 | break; |
| 4230 | } |
| 4231 | case SET_REGISTER: { |
| 4232 | Trace::DeferredSetRegister |
| 4233 | new_set(data_.u_store_register.reg, data_.u_store_register.value); |
| 4234 | Trace new_trace = *trace; |
| 4235 | new_trace.add_action(&new_set); |
| 4236 | on_success()->Emit(compiler, &new_trace); |
| 4237 | break; |
| 4238 | } |
| 4239 | case CLEAR_CAPTURES: { |
| 4240 | Trace::DeferredClearCaptures |
| 4241 | new_capture(Interval(data_.u_clear_captures.range_from, |
| 4242 | data_.u_clear_captures.range_to)); |
| 4243 | Trace new_trace = *trace; |
| 4244 | new_trace.add_action(&new_capture); |
| 4245 | on_success()->Emit(compiler, &new_trace); |
| 4246 | break; |
| 4247 | } |
| 4248 | case BEGIN_SUBMATCH: |
| 4249 | if (!trace->is_trivial()) { |
| 4250 | trace->Flush(compiler, this); |
| 4251 | } else { |
| 4252 | assembler->WriteCurrentPositionToRegister( |
| 4253 | data_.u_submatch.current_position_register, 0); |
| 4254 | assembler->WriteStackPointerToRegister( |
| 4255 | data_.u_submatch.stack_pointer_register); |
| 4256 | on_success()->Emit(compiler, trace); |
| 4257 | } |
| 4258 | break; |
| 4259 | case EMPTY_MATCH_CHECK: { |
| 4260 | int start_pos_reg = data_.u_empty_match_check.start_register; |
| 4261 | int stored_pos = 0; |
| 4262 | int rep_reg = data_.u_empty_match_check.repetition_register; |
| 4263 | bool has_minimum = (rep_reg != RegExpCompiler::kNoRegister); |
| 4264 | bool know_dist = trace->GetStoredPosition(start_pos_reg, &stored_pos); |
| 4265 | if (know_dist && !has_minimum && stored_pos == trace->cp_offset()) { |
| 4266 | // If we know we haven't advanced and there is no minimum we |
| 4267 | // can just backtrack immediately. |
| 4268 | assembler->GoTo(trace->backtrack()); |
| 4269 | } else if (know_dist && stored_pos < trace->cp_offset()) { |
| 4270 | // If we know we've advanced we can generate the continuation |
| 4271 | // immediately. |
| 4272 | on_success()->Emit(compiler, trace); |
| 4273 | } else if (!trace->is_trivial()) { |
| 4274 | trace->Flush(compiler, this); |
| 4275 | } else { |
| 4276 | Label skip_empty_check; |
| 4277 | // If we have a minimum number of repetitions we check the current |
| 4278 | // number first and skip the empty check if it's not enough. |
| 4279 | if (has_minimum) { |
| 4280 | int limit = data_.u_empty_match_check.repetition_limit; |
| 4281 | assembler->IfRegisterLT(rep_reg, limit, &skip_empty_check); |
| 4282 | } |
| 4283 | // If the match is empty we bail out, otherwise we fall through |
| 4284 | // to the on-success continuation. |
| 4285 | assembler->IfRegisterEqPos(data_.u_empty_match_check.start_register, |
| 4286 | trace->backtrack()); |
| 4287 | assembler->Bind(&skip_empty_check); |
| 4288 | on_success()->Emit(compiler, trace); |
| 4289 | } |
| 4290 | break; |
| 4291 | } |
| 4292 | case POSITIVE_SUBMATCH_SUCCESS: { |
| 4293 | if (!trace->is_trivial()) { |
| 4294 | trace->Flush(compiler, this); |
| 4295 | return; |
| 4296 | } |
| 4297 | assembler->ReadCurrentPositionFromRegister( |
| 4298 | data_.u_submatch.current_position_register); |
| 4299 | assembler->ReadStackPointerFromRegister( |
| 4300 | data_.u_submatch.stack_pointer_register); |
| 4301 | int clear_register_count = data_.u_submatch.clear_register_count; |
| 4302 | if (clear_register_count == 0) { |
| 4303 | on_success()->Emit(compiler, trace); |
| 4304 | return; |
| 4305 | } |
| 4306 | int clear_registers_from = data_.u_submatch.clear_register_from; |
| 4307 | Label clear_registers_backtrack; |
| 4308 | Trace new_trace = *trace; |
| 4309 | new_trace.set_backtrack(&clear_registers_backtrack); |
| 4310 | on_success()->Emit(compiler, &new_trace); |
| 4311 | |
| 4312 | assembler->Bind(&clear_registers_backtrack); |
| 4313 | int clear_registers_to = clear_registers_from + clear_register_count - 1; |
| 4314 | assembler->ClearRegisters(clear_registers_from, clear_registers_to); |
| 4315 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4316 | DCHECK(trace->backtrack() == NULL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4317 | assembler->Backtrack(); |
| 4318 | return; |
| 4319 | } |
| 4320 | default: |
| 4321 | UNREACHABLE(); |
| 4322 | } |
| 4323 | } |
| 4324 | |
| 4325 | |
| 4326 | void BackReferenceNode::Emit(RegExpCompiler* compiler, Trace* trace) { |
| 4327 | RegExpMacroAssembler* assembler = compiler->macro_assembler(); |
| 4328 | if (!trace->is_trivial()) { |
| 4329 | trace->Flush(compiler, this); |
| 4330 | return; |
| 4331 | } |
| 4332 | |
| 4333 | LimitResult limit_result = LimitVersions(compiler, trace); |
| 4334 | if (limit_result == DONE) return; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4335 | DCHECK(limit_result == CONTINUE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4336 | |
| 4337 | RecursionCheck rc(compiler); |
| 4338 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4339 | DCHECK_EQ(start_reg_ + 1, end_reg_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4340 | if (compiler->ignore_case()) { |
| 4341 | assembler->CheckNotBackReferenceIgnoreCase(start_reg_, |
| 4342 | trace->backtrack()); |
| 4343 | } else { |
| 4344 | assembler->CheckNotBackReference(start_reg_, trace->backtrack()); |
| 4345 | } |
| 4346 | on_success()->Emit(compiler, trace); |
| 4347 | } |
| 4348 | |
| 4349 | |
| 4350 | // ------------------------------------------------------------------- |
| 4351 | // Dot/dotty output |
| 4352 | |
| 4353 | |
| 4354 | #ifdef DEBUG |
| 4355 | |
| 4356 | |
| 4357 | class DotPrinter: public NodeVisitor { |
| 4358 | public: |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 4359 | DotPrinter(std::ostream& os, bool ignore_case) // NOLINT |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4360 | : os_(os), |
| 4361 | ignore_case_(ignore_case) {} |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4362 | void PrintNode(const char* label, RegExpNode* node); |
| 4363 | void Visit(RegExpNode* node); |
| 4364 | void PrintAttributes(RegExpNode* from); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4365 | void PrintOnFailure(RegExpNode* from, RegExpNode* to); |
| 4366 | #define DECLARE_VISIT(Type) \ |
| 4367 | virtual void Visit##Type(Type##Node* that); |
| 4368 | FOR_EACH_NODE_TYPE(DECLARE_VISIT) |
| 4369 | #undef DECLARE_VISIT |
| 4370 | private: |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 4371 | std::ostream& os_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4372 | bool ignore_case_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4373 | }; |
| 4374 | |
| 4375 | |
| 4376 | void DotPrinter::PrintNode(const char* label, RegExpNode* node) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4377 | os_ << "digraph G {\n graph [label=\""; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4378 | for (int i = 0; label[i]; i++) { |
| 4379 | switch (label[i]) { |
| 4380 | case '\\': |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4381 | os_ << "\\\\"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4382 | break; |
| 4383 | case '"': |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4384 | os_ << "\""; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4385 | break; |
| 4386 | default: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4387 | os_ << label[i]; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4388 | break; |
| 4389 | } |
| 4390 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4391 | os_ << "\"];\n"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4392 | Visit(node); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 4393 | os_ << "}" << std::endl; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4394 | } |
| 4395 | |
| 4396 | |
| 4397 | void DotPrinter::Visit(RegExpNode* node) { |
| 4398 | if (node->info()->visited) return; |
| 4399 | node->info()->visited = true; |
| 4400 | node->Accept(this); |
| 4401 | } |
| 4402 | |
| 4403 | |
| 4404 | void DotPrinter::PrintOnFailure(RegExpNode* from, RegExpNode* on_failure) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4405 | os_ << " n" << from << " -> n" << on_failure << " [style=dotted];\n"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4406 | Visit(on_failure); |
| 4407 | } |
| 4408 | |
| 4409 | |
| 4410 | class TableEntryBodyPrinter { |
| 4411 | public: |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 4412 | TableEntryBodyPrinter(std::ostream& os, ChoiceNode* choice) // NOLINT |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4413 | : os_(os), |
| 4414 | choice_(choice) {} |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4415 | void Call(uc16 from, DispatchTable::Entry entry) { |
| 4416 | OutSet* out_set = entry.out_set(); |
| 4417 | for (unsigned i = 0; i < OutSet::kFirstLimit; i++) { |
| 4418 | if (out_set->Get(i)) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4419 | os_ << " n" << choice() << ":s" << from << "o" << i << " -> n" |
| 4420 | << choice()->alternatives()->at(i).node() << ";\n"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4421 | } |
| 4422 | } |
| 4423 | } |
| 4424 | private: |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4425 | ChoiceNode* choice() { return choice_; } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 4426 | std::ostream& os_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4427 | ChoiceNode* choice_; |
| 4428 | }; |
| 4429 | |
| 4430 | |
| 4431 | class TableEntryHeaderPrinter { |
| 4432 | public: |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 4433 | explicit TableEntryHeaderPrinter(std::ostream& os) // NOLINT |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4434 | : first_(true), |
| 4435 | os_(os) {} |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4436 | void Call(uc16 from, DispatchTable::Entry entry) { |
| 4437 | if (first_) { |
| 4438 | first_ = false; |
| 4439 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4440 | os_ << "|"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4441 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4442 | os_ << "{\\" << AsUC16(from) << "-\\" << AsUC16(entry.to()) << "|{"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4443 | OutSet* out_set = entry.out_set(); |
| 4444 | int priority = 0; |
| 4445 | for (unsigned i = 0; i < OutSet::kFirstLimit; i++) { |
| 4446 | if (out_set->Get(i)) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4447 | if (priority > 0) os_ << "|"; |
| 4448 | os_ << "<s" << from << "o" << i << "> " << priority; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4449 | priority++; |
| 4450 | } |
| 4451 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4452 | os_ << "}}"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4453 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4454 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4455 | private: |
| 4456 | bool first_; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 4457 | std::ostream& os_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4458 | }; |
| 4459 | |
| 4460 | |
| 4461 | class AttributePrinter { |
| 4462 | public: |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 4463 | explicit AttributePrinter(std::ostream& os) // NOLINT |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4464 | : os_(os), |
| 4465 | first_(true) {} |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4466 | void PrintSeparator() { |
| 4467 | if (first_) { |
| 4468 | first_ = false; |
| 4469 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4470 | os_ << "|"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4471 | } |
| 4472 | } |
| 4473 | void PrintBit(const char* name, bool value) { |
| 4474 | if (!value) return; |
| 4475 | PrintSeparator(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4476 | os_ << "{" << name << "}"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4477 | } |
| 4478 | void PrintPositive(const char* name, int value) { |
| 4479 | if (value < 0) return; |
| 4480 | PrintSeparator(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4481 | os_ << "{" << name << "|" << value << "}"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4482 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4483 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4484 | private: |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 4485 | std::ostream& os_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4486 | bool first_; |
| 4487 | }; |
| 4488 | |
| 4489 | |
| 4490 | void DotPrinter::PrintAttributes(RegExpNode* that) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4491 | os_ << " a" << that << " [shape=Mrecord, color=grey, fontcolor=grey, " |
| 4492 | << "margin=0.1, fontsize=10, label=\"{"; |
| 4493 | AttributePrinter printer(os_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4494 | NodeInfo* info = that->info(); |
| 4495 | printer.PrintBit("NI", info->follows_newline_interest); |
| 4496 | printer.PrintBit("WI", info->follows_word_interest); |
| 4497 | printer.PrintBit("SI", info->follows_start_interest); |
| 4498 | Label* label = that->label(); |
| 4499 | if (label->is_bound()) |
| 4500 | printer.PrintPositive("@", label->pos()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4501 | os_ << "}\"];\n" |
| 4502 | << " a" << that << " -> n" << that |
| 4503 | << " [style=dashed, color=grey, arrowhead=none];\n"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4504 | } |
| 4505 | |
| 4506 | |
| 4507 | static const bool kPrintDispatchTable = false; |
| 4508 | void DotPrinter::VisitChoice(ChoiceNode* that) { |
| 4509 | if (kPrintDispatchTable) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4510 | os_ << " n" << that << " [shape=Mrecord, label=\""; |
| 4511 | TableEntryHeaderPrinter header_printer(os_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4512 | that->GetTable(ignore_case_)->ForEach(&header_printer); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4513 | os_ << "\"]\n"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4514 | PrintAttributes(that); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4515 | TableEntryBodyPrinter body_printer(os_, that); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4516 | that->GetTable(ignore_case_)->ForEach(&body_printer); |
| 4517 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4518 | os_ << " n" << that << " [shape=Mrecord, label=\"?\"];\n"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4519 | for (int i = 0; i < that->alternatives()->length(); i++) { |
| 4520 | GuardedAlternative alt = that->alternatives()->at(i); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4521 | os_ << " n" << that << " -> n" << alt.node(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4522 | } |
| 4523 | } |
| 4524 | for (int i = 0; i < that->alternatives()->length(); i++) { |
| 4525 | GuardedAlternative alt = that->alternatives()->at(i); |
| 4526 | alt.node()->Accept(this); |
| 4527 | } |
| 4528 | } |
| 4529 | |
| 4530 | |
| 4531 | void DotPrinter::VisitText(TextNode* that) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4532 | Zone* zone = that->zone(); |
| 4533 | os_ << " n" << that << " [label=\""; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4534 | for (int i = 0; i < that->elements()->length(); i++) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4535 | if (i > 0) os_ << " "; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4536 | TextElement elm = that->elements()->at(i); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4537 | switch (elm.text_type()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4538 | case TextElement::ATOM: { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4539 | Vector<const uc16> data = elm.atom()->data(); |
| 4540 | for (int i = 0; i < data.length(); i++) { |
| 4541 | os_ << static_cast<char>(data[i]); |
| 4542 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4543 | break; |
| 4544 | } |
| 4545 | case TextElement::CHAR_CLASS: { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4546 | RegExpCharacterClass* node = elm.char_class(); |
| 4547 | os_ << "["; |
| 4548 | if (node->is_negated()) os_ << "^"; |
| 4549 | for (int j = 0; j < node->ranges(zone)->length(); j++) { |
| 4550 | CharacterRange range = node->ranges(zone)->at(j); |
| 4551 | os_ << AsUC16(range.from()) << "-" << AsUC16(range.to()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4552 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4553 | os_ << "]"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4554 | break; |
| 4555 | } |
| 4556 | default: |
| 4557 | UNREACHABLE(); |
| 4558 | } |
| 4559 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4560 | os_ << "\", shape=box, peripheries=2];\n"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4561 | PrintAttributes(that); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4562 | os_ << " n" << that << " -> n" << that->on_success() << ";\n"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4563 | Visit(that->on_success()); |
| 4564 | } |
| 4565 | |
| 4566 | |
| 4567 | void DotPrinter::VisitBackReference(BackReferenceNode* that) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4568 | os_ << " n" << that << " [label=\"$" << that->start_register() << "..$" |
| 4569 | << that->end_register() << "\", shape=doubleoctagon];\n"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4570 | PrintAttributes(that); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4571 | os_ << " n" << that << " -> n" << that->on_success() << ";\n"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4572 | Visit(that->on_success()); |
| 4573 | } |
| 4574 | |
| 4575 | |
| 4576 | void DotPrinter::VisitEnd(EndNode* that) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4577 | os_ << " n" << that << " [style=bold, shape=point];\n"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4578 | PrintAttributes(that); |
| 4579 | } |
| 4580 | |
| 4581 | |
| 4582 | void DotPrinter::VisitAssertion(AssertionNode* that) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4583 | os_ << " n" << that << " ["; |
| 4584 | switch (that->assertion_type()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4585 | case AssertionNode::AT_END: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4586 | os_ << "label=\"$\", shape=septagon"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4587 | break; |
| 4588 | case AssertionNode::AT_START: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4589 | os_ << "label=\"^\", shape=septagon"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4590 | break; |
| 4591 | case AssertionNode::AT_BOUNDARY: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4592 | os_ << "label=\"\\b\", shape=septagon"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4593 | break; |
| 4594 | case AssertionNode::AT_NON_BOUNDARY: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4595 | os_ << "label=\"\\B\", shape=septagon"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4596 | break; |
| 4597 | case AssertionNode::AFTER_NEWLINE: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4598 | os_ << "label=\"(?<=\\n)\", shape=septagon"; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4599 | break; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4600 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4601 | os_ << "];\n"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4602 | PrintAttributes(that); |
| 4603 | RegExpNode* successor = that->on_success(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4604 | os_ << " n" << that << " -> n" << successor << ";\n"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4605 | Visit(successor); |
| 4606 | } |
| 4607 | |
| 4608 | |
| 4609 | void DotPrinter::VisitAction(ActionNode* that) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4610 | os_ << " n" << that << " ["; |
| 4611 | switch (that->action_type_) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4612 | case ActionNode::SET_REGISTER: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4613 | os_ << "label=\"$" << that->data_.u_store_register.reg |
| 4614 | << ":=" << that->data_.u_store_register.value << "\", shape=octagon"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4615 | break; |
| 4616 | case ActionNode::INCREMENT_REGISTER: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4617 | os_ << "label=\"$" << that->data_.u_increment_register.reg |
| 4618 | << "++\", shape=octagon"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4619 | break; |
| 4620 | case ActionNode::STORE_POSITION: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4621 | os_ << "label=\"$" << that->data_.u_position_register.reg |
| 4622 | << ":=$pos\", shape=octagon"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4623 | break; |
| 4624 | case ActionNode::BEGIN_SUBMATCH: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4625 | os_ << "label=\"$" << that->data_.u_submatch.current_position_register |
| 4626 | << ":=$pos,begin\", shape=septagon"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4627 | break; |
| 4628 | case ActionNode::POSITIVE_SUBMATCH_SUCCESS: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4629 | os_ << "label=\"escape\", shape=septagon"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4630 | break; |
| 4631 | case ActionNode::EMPTY_MATCH_CHECK: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4632 | os_ << "label=\"$" << that->data_.u_empty_match_check.start_register |
| 4633 | << "=$pos?,$" << that->data_.u_empty_match_check.repetition_register |
| 4634 | << "<" << that->data_.u_empty_match_check.repetition_limit |
| 4635 | << "?\", shape=septagon"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4636 | break; |
| 4637 | case ActionNode::CLEAR_CAPTURES: { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4638 | os_ << "label=\"clear $" << that->data_.u_clear_captures.range_from |
| 4639 | << " to $" << that->data_.u_clear_captures.range_to |
| 4640 | << "\", shape=septagon"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4641 | break; |
| 4642 | } |
| 4643 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4644 | os_ << "];\n"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4645 | PrintAttributes(that); |
| 4646 | RegExpNode* successor = that->on_success(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4647 | os_ << " n" << that << " -> n" << successor << ";\n"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4648 | Visit(successor); |
| 4649 | } |
| 4650 | |
| 4651 | |
| 4652 | class DispatchTableDumper { |
| 4653 | public: |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 4654 | explicit DispatchTableDumper(std::ostream& os) : os_(os) {} |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4655 | void Call(uc16 key, DispatchTable::Entry entry); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4656 | private: |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 4657 | std::ostream& os_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4658 | }; |
| 4659 | |
| 4660 | |
| 4661 | void DispatchTableDumper::Call(uc16 key, DispatchTable::Entry entry) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4662 | os_ << "[" << AsUC16(key) << "-" << AsUC16(entry.to()) << "]: {"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4663 | OutSet* set = entry.out_set(); |
| 4664 | bool first = true; |
| 4665 | for (unsigned i = 0; i < OutSet::kFirstLimit; i++) { |
| 4666 | if (set->Get(i)) { |
| 4667 | if (first) { |
| 4668 | first = false; |
| 4669 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4670 | os_ << ", "; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4671 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4672 | os_ << i; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4673 | } |
| 4674 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4675 | os_ << "}\n"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4676 | } |
| 4677 | |
| 4678 | |
| 4679 | void DispatchTable::Dump() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4680 | OFStream os(stderr); |
| 4681 | DispatchTableDumper dumper(os); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4682 | tree()->ForEach(&dumper); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4683 | } |
| 4684 | |
| 4685 | |
| 4686 | void RegExpEngine::DotPrint(const char* label, |
| 4687 | RegExpNode* node, |
| 4688 | bool ignore_case) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4689 | OFStream os(stdout); |
| 4690 | DotPrinter printer(os, ignore_case); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4691 | printer.PrintNode(label, node); |
| 4692 | } |
| 4693 | |
| 4694 | |
| 4695 | #endif // DEBUG |
| 4696 | |
| 4697 | |
| 4698 | // ------------------------------------------------------------------- |
| 4699 | // Tree to graph conversion |
| 4700 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4701 | RegExpNode* RegExpAtom::ToNode(RegExpCompiler* compiler, |
| 4702 | RegExpNode* on_success) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4703 | ZoneList<TextElement>* elms = |
| 4704 | new(compiler->zone()) ZoneList<TextElement>(1, compiler->zone()); |
| 4705 | elms->Add(TextElement::Atom(this), compiler->zone()); |
| 4706 | return new(compiler->zone()) TextNode(elms, on_success); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4707 | } |
| 4708 | |
| 4709 | |
| 4710 | RegExpNode* RegExpText::ToNode(RegExpCompiler* compiler, |
| 4711 | RegExpNode* on_success) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4712 | return new(compiler->zone()) TextNode(elements(), on_success); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4713 | } |
| 4714 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4715 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4716 | static bool CompareInverseRanges(ZoneList<CharacterRange>* ranges, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4717 | const int* special_class, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4718 | int length) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4719 | length--; // Remove final 0x10000. |
| 4720 | DCHECK(special_class[length] == 0x10000); |
| 4721 | DCHECK(ranges->length() != 0); |
| 4722 | DCHECK(length != 0); |
| 4723 | DCHECK(special_class[0] != 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4724 | if (ranges->length() != (length >> 1) + 1) { |
| 4725 | return false; |
| 4726 | } |
| 4727 | CharacterRange range = ranges->at(0); |
| 4728 | if (range.from() != 0) { |
| 4729 | return false; |
| 4730 | } |
| 4731 | for (int i = 0; i < length; i += 2) { |
| 4732 | if (special_class[i] != (range.to() + 1)) { |
| 4733 | return false; |
| 4734 | } |
| 4735 | range = ranges->at((i >> 1) + 1); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4736 | if (special_class[i+1] != range.from()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4737 | return false; |
| 4738 | } |
| 4739 | } |
| 4740 | if (range.to() != 0xffff) { |
| 4741 | return false; |
| 4742 | } |
| 4743 | return true; |
| 4744 | } |
| 4745 | |
| 4746 | |
| 4747 | static bool CompareRanges(ZoneList<CharacterRange>* ranges, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4748 | const int* special_class, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4749 | int length) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4750 | length--; // Remove final 0x10000. |
| 4751 | DCHECK(special_class[length] == 0x10000); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4752 | if (ranges->length() * 2 != length) { |
| 4753 | return false; |
| 4754 | } |
| 4755 | for (int i = 0; i < length; i += 2) { |
| 4756 | CharacterRange range = ranges->at(i >> 1); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4757 | if (range.from() != special_class[i] || |
| 4758 | range.to() != special_class[i + 1] - 1) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4759 | return false; |
| 4760 | } |
| 4761 | } |
| 4762 | return true; |
| 4763 | } |
| 4764 | |
| 4765 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4766 | bool RegExpCharacterClass::is_standard(Zone* zone) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4767 | // TODO(lrn): Remove need for this function, by not throwing away information |
| 4768 | // along the way. |
| 4769 | if (is_negated_) { |
| 4770 | return false; |
| 4771 | } |
| 4772 | if (set_.is_standard()) { |
| 4773 | return true; |
| 4774 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4775 | if (CompareRanges(set_.ranges(zone), kSpaceRanges, kSpaceRangeCount)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4776 | set_.set_standard_set_type('s'); |
| 4777 | return true; |
| 4778 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4779 | if (CompareInverseRanges(set_.ranges(zone), kSpaceRanges, kSpaceRangeCount)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4780 | set_.set_standard_set_type('S'); |
| 4781 | return true; |
| 4782 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4783 | if (CompareInverseRanges(set_.ranges(zone), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4784 | kLineTerminatorRanges, |
| 4785 | kLineTerminatorRangeCount)) { |
| 4786 | set_.set_standard_set_type('.'); |
| 4787 | return true; |
| 4788 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4789 | if (CompareRanges(set_.ranges(zone), |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4790 | kLineTerminatorRanges, |
| 4791 | kLineTerminatorRangeCount)) { |
| 4792 | set_.set_standard_set_type('n'); |
| 4793 | return true; |
| 4794 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4795 | if (CompareRanges(set_.ranges(zone), kWordRanges, kWordRangeCount)) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4796 | set_.set_standard_set_type('w'); |
| 4797 | return true; |
| 4798 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4799 | if (CompareInverseRanges(set_.ranges(zone), kWordRanges, kWordRangeCount)) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4800 | set_.set_standard_set_type('W'); |
| 4801 | return true; |
| 4802 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4803 | return false; |
| 4804 | } |
| 4805 | |
| 4806 | |
| 4807 | RegExpNode* RegExpCharacterClass::ToNode(RegExpCompiler* compiler, |
| 4808 | RegExpNode* on_success) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4809 | return new(compiler->zone()) TextNode(this, on_success); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4810 | } |
| 4811 | |
| 4812 | |
| 4813 | RegExpNode* RegExpDisjunction::ToNode(RegExpCompiler* compiler, |
| 4814 | RegExpNode* on_success) { |
| 4815 | ZoneList<RegExpTree*>* alternatives = this->alternatives(); |
| 4816 | int length = alternatives->length(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4817 | ChoiceNode* result = |
| 4818 | new(compiler->zone()) ChoiceNode(length, compiler->zone()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4819 | for (int i = 0; i < length; i++) { |
| 4820 | GuardedAlternative alternative(alternatives->at(i)->ToNode(compiler, |
| 4821 | on_success)); |
| 4822 | result->AddAlternative(alternative); |
| 4823 | } |
| 4824 | return result; |
| 4825 | } |
| 4826 | |
| 4827 | |
| 4828 | RegExpNode* RegExpQuantifier::ToNode(RegExpCompiler* compiler, |
| 4829 | RegExpNode* on_success) { |
| 4830 | return ToNode(min(), |
| 4831 | max(), |
| 4832 | is_greedy(), |
| 4833 | body(), |
| 4834 | compiler, |
| 4835 | on_success); |
| 4836 | } |
| 4837 | |
| 4838 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4839 | // Scoped object to keep track of how much we unroll quantifier loops in the |
| 4840 | // regexp graph generator. |
| 4841 | class RegExpExpansionLimiter { |
| 4842 | public: |
| 4843 | static const int kMaxExpansionFactor = 6; |
| 4844 | RegExpExpansionLimiter(RegExpCompiler* compiler, int factor) |
| 4845 | : compiler_(compiler), |
| 4846 | saved_expansion_factor_(compiler->current_expansion_factor()), |
| 4847 | ok_to_expand_(saved_expansion_factor_ <= kMaxExpansionFactor) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4848 | DCHECK(factor > 0); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4849 | if (ok_to_expand_) { |
| 4850 | if (factor > kMaxExpansionFactor) { |
| 4851 | // Avoid integer overflow of the current expansion factor. |
| 4852 | ok_to_expand_ = false; |
| 4853 | compiler->set_current_expansion_factor(kMaxExpansionFactor + 1); |
| 4854 | } else { |
| 4855 | int new_factor = saved_expansion_factor_ * factor; |
| 4856 | ok_to_expand_ = (new_factor <= kMaxExpansionFactor); |
| 4857 | compiler->set_current_expansion_factor(new_factor); |
| 4858 | } |
| 4859 | } |
| 4860 | } |
| 4861 | |
| 4862 | ~RegExpExpansionLimiter() { |
| 4863 | compiler_->set_current_expansion_factor(saved_expansion_factor_); |
| 4864 | } |
| 4865 | |
| 4866 | bool ok_to_expand() { return ok_to_expand_; } |
| 4867 | |
| 4868 | private: |
| 4869 | RegExpCompiler* compiler_; |
| 4870 | int saved_expansion_factor_; |
| 4871 | bool ok_to_expand_; |
| 4872 | |
| 4873 | DISALLOW_IMPLICIT_CONSTRUCTORS(RegExpExpansionLimiter); |
| 4874 | }; |
| 4875 | |
| 4876 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4877 | RegExpNode* RegExpQuantifier::ToNode(int min, |
| 4878 | int max, |
| 4879 | bool is_greedy, |
| 4880 | RegExpTree* body, |
| 4881 | RegExpCompiler* compiler, |
| 4882 | RegExpNode* on_success, |
| 4883 | bool not_at_start) { |
| 4884 | // x{f, t} becomes this: |
| 4885 | // |
| 4886 | // (r++)<-. |
| 4887 | // | ` |
| 4888 | // | (x) |
| 4889 | // v ^ |
| 4890 | // (r=0)-->(?)---/ [if r < t] |
| 4891 | // | |
| 4892 | // [if r >= f] \----> ... |
| 4893 | // |
| 4894 | |
| 4895 | // 15.10.2.5 RepeatMatcher algorithm. |
| 4896 | // The parser has already eliminated the case where max is 0. In the case |
| 4897 | // where max_match is zero the parser has removed the quantifier if min was |
| 4898 | // > 0 and removed the atom if min was 0. See AddQuantifierToAtom. |
| 4899 | |
| 4900 | // If we know that we cannot match zero length then things are a little |
| 4901 | // simpler since we don't need to make the special zero length match check |
| 4902 | // from step 2.1. If the min and max are small we can unroll a little in |
| 4903 | // this case. |
| 4904 | static const int kMaxUnrolledMinMatches = 3; // Unroll (foo)+ and (foo){3,} |
| 4905 | static const int kMaxUnrolledMaxMatches = 3; // Unroll (foo)? and (foo){x,3} |
| 4906 | if (max == 0) return on_success; // This can happen due to recursion. |
| 4907 | bool body_can_be_empty = (body->min_match() == 0); |
| 4908 | int body_start_reg = RegExpCompiler::kNoRegister; |
| 4909 | Interval capture_registers = body->CaptureRegisters(); |
| 4910 | bool needs_capture_clearing = !capture_registers.is_empty(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4911 | Zone* zone = compiler->zone(); |
| 4912 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4913 | if (body_can_be_empty) { |
| 4914 | body_start_reg = compiler->AllocateRegister(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 4915 | } else if (compiler->optimize() && !needs_capture_clearing) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4916 | // Only unroll if there are no captures and the body can't be |
| 4917 | // empty. |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4918 | { |
| 4919 | RegExpExpansionLimiter limiter( |
| 4920 | compiler, min + ((max != min) ? 1 : 0)); |
| 4921 | if (min > 0 && min <= kMaxUnrolledMinMatches && limiter.ok_to_expand()) { |
| 4922 | int new_max = (max == kInfinity) ? max : max - min; |
| 4923 | // Recurse once to get the loop or optional matches after the fixed |
| 4924 | // ones. |
| 4925 | RegExpNode* answer = ToNode( |
| 4926 | 0, new_max, is_greedy, body, compiler, on_success, true); |
| 4927 | // Unroll the forced matches from 0 to min. This can cause chains of |
| 4928 | // TextNodes (which the parser does not generate). These should be |
| 4929 | // combined if it turns out they hinder good code generation. |
| 4930 | for (int i = 0; i < min; i++) { |
| 4931 | answer = body->ToNode(compiler, answer); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4932 | } |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4933 | return answer; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4934 | } |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4935 | } |
| 4936 | if (max <= kMaxUnrolledMaxMatches && min == 0) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4937 | DCHECK(max > 0); // Due to the 'if' above. |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4938 | RegExpExpansionLimiter limiter(compiler, max); |
| 4939 | if (limiter.ok_to_expand()) { |
| 4940 | // Unroll the optional matches up to max. |
| 4941 | RegExpNode* answer = on_success; |
| 4942 | for (int i = 0; i < max; i++) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4943 | ChoiceNode* alternation = new(zone) ChoiceNode(2, zone); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4944 | if (is_greedy) { |
| 4945 | alternation->AddAlternative( |
| 4946 | GuardedAlternative(body->ToNode(compiler, answer))); |
| 4947 | alternation->AddAlternative(GuardedAlternative(on_success)); |
| 4948 | } else { |
| 4949 | alternation->AddAlternative(GuardedAlternative(on_success)); |
| 4950 | alternation->AddAlternative( |
| 4951 | GuardedAlternative(body->ToNode(compiler, answer))); |
| 4952 | } |
| 4953 | answer = alternation; |
| 4954 | if (not_at_start) alternation->set_not_at_start(); |
| 4955 | } |
| 4956 | return answer; |
| 4957 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4958 | } |
| 4959 | } |
| 4960 | bool has_min = min > 0; |
| 4961 | bool has_max = max < RegExpTree::kInfinity; |
| 4962 | bool needs_counter = has_min || has_max; |
| 4963 | int reg_ctr = needs_counter |
| 4964 | ? compiler->AllocateRegister() |
| 4965 | : RegExpCompiler::kNoRegister; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4966 | LoopChoiceNode* center = new(zone) LoopChoiceNode(body->min_match() == 0, |
| 4967 | zone); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4968 | if (not_at_start) center->set_not_at_start(); |
| 4969 | RegExpNode* loop_return = needs_counter |
| 4970 | ? static_cast<RegExpNode*>(ActionNode::IncrementRegister(reg_ctr, center)) |
| 4971 | : static_cast<RegExpNode*>(center); |
| 4972 | if (body_can_be_empty) { |
| 4973 | // If the body can be empty we need to check if it was and then |
| 4974 | // backtrack. |
| 4975 | loop_return = ActionNode::EmptyMatchCheck(body_start_reg, |
| 4976 | reg_ctr, |
| 4977 | min, |
| 4978 | loop_return); |
| 4979 | } |
| 4980 | RegExpNode* body_node = body->ToNode(compiler, loop_return); |
| 4981 | if (body_can_be_empty) { |
| 4982 | // If the body can be empty we need to store the start position |
| 4983 | // so we can bail out if it was empty. |
| 4984 | body_node = ActionNode::StorePosition(body_start_reg, false, body_node); |
| 4985 | } |
| 4986 | if (needs_capture_clearing) { |
| 4987 | // Before entering the body of this loop we need to clear captures. |
| 4988 | body_node = ActionNode::ClearCaptures(capture_registers, body_node); |
| 4989 | } |
| 4990 | GuardedAlternative body_alt(body_node); |
| 4991 | if (has_max) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4992 | Guard* body_guard = |
| 4993 | new(zone) Guard(reg_ctr, Guard::LT, max); |
| 4994 | body_alt.AddGuard(body_guard, zone); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4995 | } |
| 4996 | GuardedAlternative rest_alt(on_success); |
| 4997 | if (has_min) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4998 | Guard* rest_guard = new(compiler->zone()) Guard(reg_ctr, Guard::GEQ, min); |
| 4999 | rest_alt.AddGuard(rest_guard, zone); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5000 | } |
| 5001 | if (is_greedy) { |
| 5002 | center->AddLoopAlternative(body_alt); |
| 5003 | center->AddContinueAlternative(rest_alt); |
| 5004 | } else { |
| 5005 | center->AddContinueAlternative(rest_alt); |
| 5006 | center->AddLoopAlternative(body_alt); |
| 5007 | } |
| 5008 | if (needs_counter) { |
| 5009 | return ActionNode::SetRegister(reg_ctr, 0, center); |
| 5010 | } else { |
| 5011 | return center; |
| 5012 | } |
| 5013 | } |
| 5014 | |
| 5015 | |
| 5016 | RegExpNode* RegExpAssertion::ToNode(RegExpCompiler* compiler, |
| 5017 | RegExpNode* on_success) { |
| 5018 | NodeInfo info; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5019 | Zone* zone = compiler->zone(); |
| 5020 | |
| 5021 | switch (assertion_type()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5022 | case START_OF_LINE: |
| 5023 | return AssertionNode::AfterNewline(on_success); |
| 5024 | case START_OF_INPUT: |
| 5025 | return AssertionNode::AtStart(on_success); |
| 5026 | case BOUNDARY: |
| 5027 | return AssertionNode::AtBoundary(on_success); |
| 5028 | case NON_BOUNDARY: |
| 5029 | return AssertionNode::AtNonBoundary(on_success); |
| 5030 | case END_OF_INPUT: |
| 5031 | return AssertionNode::AtEnd(on_success); |
| 5032 | case END_OF_LINE: { |
| 5033 | // Compile $ in multiline regexps as an alternation with a positive |
| 5034 | // lookahead in one side and an end-of-input on the other side. |
| 5035 | // We need two registers for the lookahead. |
| 5036 | int stack_pointer_register = compiler->AllocateRegister(); |
| 5037 | int position_register = compiler->AllocateRegister(); |
| 5038 | // The ChoiceNode to distinguish between a newline and end-of-input. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5039 | ChoiceNode* result = new(zone) ChoiceNode(2, zone); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5040 | // Create a newline atom. |
| 5041 | ZoneList<CharacterRange>* newline_ranges = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5042 | new(zone) ZoneList<CharacterRange>(3, zone); |
| 5043 | CharacterRange::AddClassEscape('n', newline_ranges, zone); |
| 5044 | RegExpCharacterClass* newline_atom = new(zone) RegExpCharacterClass('n'); |
| 5045 | TextNode* newline_matcher = new(zone) TextNode( |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5046 | newline_atom, |
| 5047 | ActionNode::PositiveSubmatchSuccess(stack_pointer_register, |
| 5048 | position_register, |
| 5049 | 0, // No captures inside. |
| 5050 | -1, // Ignored if no captures. |
| 5051 | on_success)); |
| 5052 | // Create an end-of-input matcher. |
| 5053 | RegExpNode* end_of_line = ActionNode::BeginSubmatch( |
| 5054 | stack_pointer_register, |
| 5055 | position_register, |
| 5056 | newline_matcher); |
| 5057 | // Add the two alternatives to the ChoiceNode. |
| 5058 | GuardedAlternative eol_alternative(end_of_line); |
| 5059 | result->AddAlternative(eol_alternative); |
| 5060 | GuardedAlternative end_alternative(AssertionNode::AtEnd(on_success)); |
| 5061 | result->AddAlternative(end_alternative); |
| 5062 | return result; |
| 5063 | } |
| 5064 | default: |
| 5065 | UNREACHABLE(); |
| 5066 | } |
| 5067 | return on_success; |
| 5068 | } |
| 5069 | |
| 5070 | |
| 5071 | RegExpNode* RegExpBackReference::ToNode(RegExpCompiler* compiler, |
| 5072 | RegExpNode* on_success) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5073 | return new(compiler->zone()) |
| 5074 | BackReferenceNode(RegExpCapture::StartRegister(index()), |
| 5075 | RegExpCapture::EndRegister(index()), |
| 5076 | on_success); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5077 | } |
| 5078 | |
| 5079 | |
| 5080 | RegExpNode* RegExpEmpty::ToNode(RegExpCompiler* compiler, |
| 5081 | RegExpNode* on_success) { |
| 5082 | return on_success; |
| 5083 | } |
| 5084 | |
| 5085 | |
| 5086 | RegExpNode* RegExpLookahead::ToNode(RegExpCompiler* compiler, |
| 5087 | RegExpNode* on_success) { |
| 5088 | int stack_pointer_register = compiler->AllocateRegister(); |
| 5089 | int position_register = compiler->AllocateRegister(); |
| 5090 | |
| 5091 | const int registers_per_capture = 2; |
| 5092 | const int register_of_first_capture = 2; |
| 5093 | int register_count = capture_count_ * registers_per_capture; |
| 5094 | int register_start = |
| 5095 | register_of_first_capture + capture_from_ * registers_per_capture; |
| 5096 | |
| 5097 | RegExpNode* success; |
| 5098 | if (is_positive()) { |
| 5099 | RegExpNode* node = ActionNode::BeginSubmatch( |
| 5100 | stack_pointer_register, |
| 5101 | position_register, |
| 5102 | body()->ToNode( |
| 5103 | compiler, |
| 5104 | ActionNode::PositiveSubmatchSuccess(stack_pointer_register, |
| 5105 | position_register, |
| 5106 | register_count, |
| 5107 | register_start, |
| 5108 | on_success))); |
| 5109 | return node; |
| 5110 | } else { |
| 5111 | // We use a ChoiceNode for a negative lookahead because it has most of |
| 5112 | // the characteristics we need. It has the body of the lookahead as its |
| 5113 | // first alternative and the expression after the lookahead of the second |
| 5114 | // alternative. If the first alternative succeeds then the |
| 5115 | // NegativeSubmatchSuccess will unwind the stack including everything the |
| 5116 | // choice node set up and backtrack. If the first alternative fails then |
| 5117 | // the second alternative is tried, which is exactly the desired result |
| 5118 | // for a negative lookahead. The NegativeLookaheadChoiceNode is a special |
| 5119 | // ChoiceNode that knows to ignore the first exit when calculating quick |
| 5120 | // checks. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5121 | Zone* zone = compiler->zone(); |
| 5122 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5123 | GuardedAlternative body_alt( |
| 5124 | body()->ToNode( |
| 5125 | compiler, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5126 | success = new(zone) NegativeSubmatchSuccess(stack_pointer_register, |
| 5127 | position_register, |
| 5128 | register_count, |
| 5129 | register_start, |
| 5130 | zone))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5131 | ChoiceNode* choice_node = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5132 | new(zone) NegativeLookaheadChoiceNode(body_alt, |
| 5133 | GuardedAlternative(on_success), |
| 5134 | zone); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5135 | return ActionNode::BeginSubmatch(stack_pointer_register, |
| 5136 | position_register, |
| 5137 | choice_node); |
| 5138 | } |
| 5139 | } |
| 5140 | |
| 5141 | |
| 5142 | RegExpNode* RegExpCapture::ToNode(RegExpCompiler* compiler, |
| 5143 | RegExpNode* on_success) { |
| 5144 | return ToNode(body(), index(), compiler, on_success); |
| 5145 | } |
| 5146 | |
| 5147 | |
| 5148 | RegExpNode* RegExpCapture::ToNode(RegExpTree* body, |
| 5149 | int index, |
| 5150 | RegExpCompiler* compiler, |
| 5151 | RegExpNode* on_success) { |
| 5152 | int start_reg = RegExpCapture::StartRegister(index); |
| 5153 | int end_reg = RegExpCapture::EndRegister(index); |
| 5154 | RegExpNode* store_end = ActionNode::StorePosition(end_reg, true, on_success); |
| 5155 | RegExpNode* body_node = body->ToNode(compiler, store_end); |
| 5156 | return ActionNode::StorePosition(start_reg, true, body_node); |
| 5157 | } |
| 5158 | |
| 5159 | |
| 5160 | RegExpNode* RegExpAlternative::ToNode(RegExpCompiler* compiler, |
| 5161 | RegExpNode* on_success) { |
| 5162 | ZoneList<RegExpTree*>* children = nodes(); |
| 5163 | RegExpNode* current = on_success; |
| 5164 | for (int i = children->length() - 1; i >= 0; i--) { |
| 5165 | current = children->at(i)->ToNode(compiler, current); |
| 5166 | } |
| 5167 | return current; |
| 5168 | } |
| 5169 | |
| 5170 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5171 | static void AddClass(const int* elmv, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5172 | int elmc, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5173 | ZoneList<CharacterRange>* ranges, |
| 5174 | Zone* zone) { |
| 5175 | elmc--; |
| 5176 | DCHECK(elmv[elmc] == 0x10000); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5177 | for (int i = 0; i < elmc; i += 2) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5178 | DCHECK(elmv[i] < elmv[i + 1]); |
| 5179 | ranges->Add(CharacterRange(elmv[i], elmv[i + 1] - 1), zone); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5180 | } |
| 5181 | } |
| 5182 | |
| 5183 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5184 | static void AddClassNegated(const int *elmv, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5185 | int elmc, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5186 | ZoneList<CharacterRange>* ranges, |
| 5187 | Zone* zone) { |
| 5188 | elmc--; |
| 5189 | DCHECK(elmv[elmc] == 0x10000); |
| 5190 | DCHECK(elmv[0] != 0x0000); |
| 5191 | DCHECK(elmv[elmc-1] != String::kMaxUtf16CodeUnit); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5192 | uc16 last = 0x0000; |
| 5193 | for (int i = 0; i < elmc; i += 2) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5194 | DCHECK(last <= elmv[i] - 1); |
| 5195 | DCHECK(elmv[i] < elmv[i + 1]); |
| 5196 | ranges->Add(CharacterRange(last, elmv[i] - 1), zone); |
| 5197 | last = elmv[i + 1]; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5198 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5199 | ranges->Add(CharacterRange(last, String::kMaxUtf16CodeUnit), zone); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5200 | } |
| 5201 | |
| 5202 | |
| 5203 | void CharacterRange::AddClassEscape(uc16 type, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5204 | ZoneList<CharacterRange>* ranges, |
| 5205 | Zone* zone) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5206 | switch (type) { |
| 5207 | case 's': |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5208 | AddClass(kSpaceRanges, kSpaceRangeCount, ranges, zone); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5209 | break; |
| 5210 | case 'S': |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5211 | AddClassNegated(kSpaceRanges, kSpaceRangeCount, ranges, zone); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5212 | break; |
| 5213 | case 'w': |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5214 | AddClass(kWordRanges, kWordRangeCount, ranges, zone); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5215 | break; |
| 5216 | case 'W': |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5217 | AddClassNegated(kWordRanges, kWordRangeCount, ranges, zone); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5218 | break; |
| 5219 | case 'd': |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5220 | AddClass(kDigitRanges, kDigitRangeCount, ranges, zone); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5221 | break; |
| 5222 | case 'D': |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5223 | AddClassNegated(kDigitRanges, kDigitRangeCount, ranges, zone); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5224 | break; |
| 5225 | case '.': |
| 5226 | AddClassNegated(kLineTerminatorRanges, |
| 5227 | kLineTerminatorRangeCount, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5228 | ranges, |
| 5229 | zone); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5230 | break; |
| 5231 | // This is not a character range as defined by the spec but a |
| 5232 | // convenient shorthand for a character class that matches any |
| 5233 | // character. |
| 5234 | case '*': |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5235 | ranges->Add(CharacterRange::Everything(), zone); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5236 | break; |
| 5237 | // This is the set of characters matched by the $ and ^ symbols |
| 5238 | // in multiline mode. |
| 5239 | case 'n': |
| 5240 | AddClass(kLineTerminatorRanges, |
| 5241 | kLineTerminatorRangeCount, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5242 | ranges, |
| 5243 | zone); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5244 | break; |
| 5245 | default: |
| 5246 | UNREACHABLE(); |
| 5247 | } |
| 5248 | } |
| 5249 | |
| 5250 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5251 | Vector<const int> CharacterRange::GetWordBounds() { |
| 5252 | return Vector<const int>(kWordRanges, kWordRangeCount - 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5253 | } |
| 5254 | |
| 5255 | |
| 5256 | class CharacterRangeSplitter { |
| 5257 | public: |
| 5258 | CharacterRangeSplitter(ZoneList<CharacterRange>** included, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5259 | ZoneList<CharacterRange>** excluded, |
| 5260 | Zone* zone) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5261 | : included_(included), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5262 | excluded_(excluded), |
| 5263 | zone_(zone) { } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5264 | void Call(uc16 from, DispatchTable::Entry entry); |
| 5265 | |
| 5266 | static const int kInBase = 0; |
| 5267 | static const int kInOverlay = 1; |
| 5268 | |
| 5269 | private: |
| 5270 | ZoneList<CharacterRange>** included_; |
| 5271 | ZoneList<CharacterRange>** excluded_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5272 | Zone* zone_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5273 | }; |
| 5274 | |
| 5275 | |
| 5276 | void CharacterRangeSplitter::Call(uc16 from, DispatchTable::Entry entry) { |
| 5277 | if (!entry.out_set()->Get(kInBase)) return; |
| 5278 | ZoneList<CharacterRange>** target = entry.out_set()->Get(kInOverlay) |
| 5279 | ? included_ |
| 5280 | : excluded_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5281 | if (*target == NULL) *target = new(zone_) ZoneList<CharacterRange>(2, zone_); |
| 5282 | (*target)->Add(CharacterRange(entry.from(), entry.to()), zone_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5283 | } |
| 5284 | |
| 5285 | |
| 5286 | void CharacterRange::Split(ZoneList<CharacterRange>* base, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5287 | Vector<const int> overlay, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5288 | ZoneList<CharacterRange>** included, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5289 | ZoneList<CharacterRange>** excluded, |
| 5290 | Zone* zone) { |
| 5291 | DCHECK_EQ(NULL, *included); |
| 5292 | DCHECK_EQ(NULL, *excluded); |
| 5293 | DispatchTable table(zone); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5294 | for (int i = 0; i < base->length(); i++) |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5295 | table.AddRange(base->at(i), CharacterRangeSplitter::kInBase, zone); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5296 | for (int i = 0; i < overlay.length(); i += 2) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5297 | table.AddRange(CharacterRange(overlay[i], overlay[i + 1] - 1), |
| 5298 | CharacterRangeSplitter::kInOverlay, zone); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5299 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5300 | CharacterRangeSplitter callback(included, excluded, zone); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5301 | table.ForEach(&callback); |
| 5302 | } |
| 5303 | |
| 5304 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5305 | void CharacterRange::AddCaseEquivalents(ZoneList<CharacterRange>* ranges, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5306 | bool is_one_byte, Zone* zone) { |
| 5307 | Isolate* isolate = zone->isolate(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5308 | uc16 bottom = from(); |
| 5309 | uc16 top = to(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5310 | if (is_one_byte && !RangeContainsLatin1Equivalents(*this)) { |
| 5311 | if (bottom > String::kMaxOneByteCharCode) return; |
| 5312 | if (top > String::kMaxOneByteCharCode) top = String::kMaxOneByteCharCode; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5313 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5314 | unibrow::uchar chars[unibrow::Ecma262UnCanonicalize::kMaxWidth]; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5315 | if (top == bottom) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5316 | // If this is a singleton we just expand the one character. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5317 | int length = isolate->jsregexp_uncanonicalize()->get(bottom, '\0', chars); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5318 | for (int i = 0; i < length; i++) { |
| 5319 | uc32 chr = chars[i]; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5320 | if (chr != bottom) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5321 | ranges->Add(CharacterRange::Singleton(chars[i]), zone); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5322 | } |
| 5323 | } |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 5324 | } else { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5325 | // If this is a range we expand the characters block by block, |
| 5326 | // expanding contiguous subranges (blocks) one at a time. |
| 5327 | // The approach is as follows. For a given start character we |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 5328 | // look up the remainder of the block that contains it (represented |
| 5329 | // by the end point), for instance we find 'z' if the character |
| 5330 | // is 'c'. A block is characterized by the property |
| 5331 | // that all characters uncanonicalize in the same way, except that |
| 5332 | // each entry in the result is incremented by the distance from the first |
| 5333 | // element. So a-z is a block because 'a' uncanonicalizes to ['a', 'A'] and |
| 5334 | // the k'th letter uncanonicalizes to ['a' + k, 'A' + k]. |
| 5335 | // Once we've found the end point we look up its uncanonicalization |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5336 | // and produce a range for each element. For instance for [c-f] |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 5337 | // we look up ['z', 'Z'] and produce [c-f] and [C-F]. We then only |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5338 | // add a range if it is not already contained in the input, so [c-f] |
| 5339 | // will be skipped but [C-F] will be added. If this range is not |
| 5340 | // completely contained in a block we do this for all the blocks |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 5341 | // covered by the range (handling characters that is not in a block |
| 5342 | // as a "singleton block"). |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5343 | unibrow::uchar range[unibrow::Ecma262UnCanonicalize::kMaxWidth]; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5344 | int pos = bottom; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5345 | while (pos <= top) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5346 | int length = isolate->jsregexp_canonrange()->get(pos, '\0', range); |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 5347 | uc16 block_end; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5348 | if (length == 0) { |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 5349 | block_end = pos; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5350 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5351 | DCHECK_EQ(1, length); |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 5352 | block_end = range[0]; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5353 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5354 | int end = (block_end > top) ? top : block_end; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5355 | length = isolate->jsregexp_uncanonicalize()->get(block_end, '\0', range); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5356 | for (int i = 0; i < length; i++) { |
| 5357 | uc32 c = range[i]; |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 5358 | uc16 range_from = c - (block_end - pos); |
| 5359 | uc16 range_to = c - (block_end - end); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5360 | if (!(bottom <= range_from && range_to <= top)) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5361 | ranges->Add(CharacterRange(range_from, range_to), zone); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5362 | } |
| 5363 | } |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 5364 | pos = end + 1; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5365 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5366 | } |
| 5367 | } |
| 5368 | |
| 5369 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5370 | bool CharacterRange::IsCanonical(ZoneList<CharacterRange>* ranges) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5371 | DCHECK_NOT_NULL(ranges); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5372 | int n = ranges->length(); |
| 5373 | if (n <= 1) return true; |
| 5374 | int max = ranges->at(0).to(); |
| 5375 | for (int i = 1; i < n; i++) { |
| 5376 | CharacterRange next_range = ranges->at(i); |
| 5377 | if (next_range.from() <= max + 1) return false; |
| 5378 | max = next_range.to(); |
| 5379 | } |
| 5380 | return true; |
| 5381 | } |
| 5382 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5383 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5384 | ZoneList<CharacterRange>* CharacterSet::ranges(Zone* zone) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5385 | if (ranges_ == NULL) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5386 | ranges_ = new(zone) ZoneList<CharacterRange>(2, zone); |
| 5387 | CharacterRange::AddClassEscape(standard_set_type_, ranges_, zone); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5388 | } |
| 5389 | return ranges_; |
| 5390 | } |
| 5391 | |
| 5392 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5393 | // Move a number of elements in a zonelist to another position |
| 5394 | // in the same list. Handles overlapping source and target areas. |
| 5395 | static void MoveRanges(ZoneList<CharacterRange>* list, |
| 5396 | int from, |
| 5397 | int to, |
| 5398 | int count) { |
| 5399 | // Ranges are potentially overlapping. |
| 5400 | if (from < to) { |
| 5401 | for (int i = count - 1; i >= 0; i--) { |
| 5402 | list->at(to + i) = list->at(from + i); |
| 5403 | } |
| 5404 | } else { |
| 5405 | for (int i = 0; i < count; i++) { |
| 5406 | list->at(to + i) = list->at(from + i); |
| 5407 | } |
| 5408 | } |
| 5409 | } |
| 5410 | |
| 5411 | |
| 5412 | static int InsertRangeInCanonicalList(ZoneList<CharacterRange>* list, |
| 5413 | int count, |
| 5414 | CharacterRange insert) { |
| 5415 | // Inserts a range into list[0..count[, which must be sorted |
| 5416 | // by from value and non-overlapping and non-adjacent, using at most |
| 5417 | // list[0..count] for the result. Returns the number of resulting |
| 5418 | // canonicalized ranges. Inserting a range may collapse existing ranges into |
| 5419 | // fewer ranges, so the return value can be anything in the range 1..count+1. |
| 5420 | uc16 from = insert.from(); |
| 5421 | uc16 to = insert.to(); |
| 5422 | int start_pos = 0; |
| 5423 | int end_pos = count; |
| 5424 | for (int i = count - 1; i >= 0; i--) { |
| 5425 | CharacterRange current = list->at(i); |
| 5426 | if (current.from() > to + 1) { |
| 5427 | end_pos = i; |
| 5428 | } else if (current.to() + 1 < from) { |
| 5429 | start_pos = i + 1; |
| 5430 | break; |
| 5431 | } |
| 5432 | } |
| 5433 | |
| 5434 | // Inserted range overlaps, or is adjacent to, ranges at positions |
| 5435 | // [start_pos..end_pos[. Ranges before start_pos or at or after end_pos are |
| 5436 | // not affected by the insertion. |
| 5437 | // If start_pos == end_pos, the range must be inserted before start_pos. |
| 5438 | // if start_pos < end_pos, the entire range from start_pos to end_pos |
| 5439 | // must be merged with the insert range. |
| 5440 | |
| 5441 | if (start_pos == end_pos) { |
| 5442 | // Insert between existing ranges at position start_pos. |
| 5443 | if (start_pos < count) { |
| 5444 | MoveRanges(list, start_pos, start_pos + 1, count - start_pos); |
| 5445 | } |
| 5446 | list->at(start_pos) = insert; |
| 5447 | return count + 1; |
| 5448 | } |
| 5449 | if (start_pos + 1 == end_pos) { |
| 5450 | // Replace single existing range at position start_pos. |
| 5451 | CharacterRange to_replace = list->at(start_pos); |
| 5452 | int new_from = Min(to_replace.from(), from); |
| 5453 | int new_to = Max(to_replace.to(), to); |
| 5454 | list->at(start_pos) = CharacterRange(new_from, new_to); |
| 5455 | return count; |
| 5456 | } |
| 5457 | // Replace a number of existing ranges from start_pos to end_pos - 1. |
| 5458 | // Move the remaining ranges down. |
| 5459 | |
| 5460 | int new_from = Min(list->at(start_pos).from(), from); |
| 5461 | int new_to = Max(list->at(end_pos - 1).to(), to); |
| 5462 | if (end_pos < count) { |
| 5463 | MoveRanges(list, end_pos, start_pos + 1, count - end_pos); |
| 5464 | } |
| 5465 | list->at(start_pos) = CharacterRange(new_from, new_to); |
| 5466 | return count - (end_pos - start_pos) + 1; |
| 5467 | } |
| 5468 | |
| 5469 | |
| 5470 | void CharacterSet::Canonicalize() { |
| 5471 | // Special/default classes are always considered canonical. The result |
| 5472 | // of calling ranges() will be sorted. |
| 5473 | if (ranges_ == NULL) return; |
| 5474 | CharacterRange::Canonicalize(ranges_); |
| 5475 | } |
| 5476 | |
| 5477 | |
| 5478 | void CharacterRange::Canonicalize(ZoneList<CharacterRange>* character_ranges) { |
| 5479 | if (character_ranges->length() <= 1) return; |
| 5480 | // Check whether ranges are already canonical (increasing, non-overlapping, |
| 5481 | // non-adjacent). |
| 5482 | int n = character_ranges->length(); |
| 5483 | int max = character_ranges->at(0).to(); |
| 5484 | int i = 1; |
| 5485 | while (i < n) { |
| 5486 | CharacterRange current = character_ranges->at(i); |
| 5487 | if (current.from() <= max + 1) { |
| 5488 | break; |
| 5489 | } |
| 5490 | max = current.to(); |
| 5491 | i++; |
| 5492 | } |
| 5493 | // Canonical until the i'th range. If that's all of them, we are done. |
| 5494 | if (i == n) return; |
| 5495 | |
| 5496 | // The ranges at index i and forward are not canonicalized. Make them so by |
| 5497 | // doing the equivalent of insertion sort (inserting each into the previous |
| 5498 | // list, in order). |
| 5499 | // Notice that inserting a range can reduce the number of ranges in the |
| 5500 | // result due to combining of adjacent and overlapping ranges. |
| 5501 | int read = i; // Range to insert. |
| 5502 | int num_canonical = i; // Length of canonicalized part of list. |
| 5503 | do { |
| 5504 | num_canonical = InsertRangeInCanonicalList(character_ranges, |
| 5505 | num_canonical, |
| 5506 | character_ranges->at(read)); |
| 5507 | read++; |
| 5508 | } while (read < n); |
| 5509 | character_ranges->Rewind(num_canonical); |
| 5510 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5511 | DCHECK(CharacterRange::IsCanonical(character_ranges)); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5512 | } |
| 5513 | |
| 5514 | |
| 5515 | void CharacterRange::Negate(ZoneList<CharacterRange>* ranges, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5516 | ZoneList<CharacterRange>* negated_ranges, |
| 5517 | Zone* zone) { |
| 5518 | DCHECK(CharacterRange::IsCanonical(ranges)); |
| 5519 | DCHECK_EQ(0, negated_ranges->length()); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5520 | int range_count = ranges->length(); |
| 5521 | uc16 from = 0; |
| 5522 | int i = 0; |
| 5523 | if (range_count > 0 && ranges->at(0).from() == 0) { |
| 5524 | from = ranges->at(0).to(); |
| 5525 | i = 1; |
| 5526 | } |
| 5527 | while (i < range_count) { |
| 5528 | CharacterRange range = ranges->at(i); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5529 | negated_ranges->Add(CharacterRange(from + 1, range.from() - 1), zone); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5530 | from = range.to(); |
| 5531 | i++; |
| 5532 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5533 | if (from < String::kMaxUtf16CodeUnit) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5534 | negated_ranges->Add(CharacterRange(from + 1, String::kMaxUtf16CodeUnit), |
| 5535 | zone); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5536 | } |
| 5537 | } |
| 5538 | |
| 5539 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5540 | // ------------------------------------------------------------------- |
| 5541 | // Splay tree |
| 5542 | |
| 5543 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5544 | OutSet* OutSet::Extend(unsigned value, Zone* zone) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5545 | if (Get(value)) |
| 5546 | return this; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5547 | if (successors(zone) != NULL) { |
| 5548 | for (int i = 0; i < successors(zone)->length(); i++) { |
| 5549 | OutSet* successor = successors(zone)->at(i); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5550 | if (successor->Get(value)) |
| 5551 | return successor; |
| 5552 | } |
| 5553 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5554 | successors_ = new(zone) ZoneList<OutSet*>(2, zone); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5555 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5556 | OutSet* result = new(zone) OutSet(first_, remaining_); |
| 5557 | result->Set(value, zone); |
| 5558 | successors(zone)->Add(result, zone); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5559 | return result; |
| 5560 | } |
| 5561 | |
| 5562 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5563 | void OutSet::Set(unsigned value, Zone *zone) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5564 | if (value < kFirstLimit) { |
| 5565 | first_ |= (1 << value); |
| 5566 | } else { |
| 5567 | if (remaining_ == NULL) |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5568 | remaining_ = new(zone) ZoneList<unsigned>(1, zone); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5569 | if (remaining_->is_empty() || !remaining_->Contains(value)) |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5570 | remaining_->Add(value, zone); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5571 | } |
| 5572 | } |
| 5573 | |
| 5574 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5575 | bool OutSet::Get(unsigned value) const { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5576 | if (value < kFirstLimit) { |
| 5577 | return (first_ & (1 << value)) != 0; |
| 5578 | } else if (remaining_ == NULL) { |
| 5579 | return false; |
| 5580 | } else { |
| 5581 | return remaining_->Contains(value); |
| 5582 | } |
| 5583 | } |
| 5584 | |
| 5585 | |
| 5586 | const uc16 DispatchTable::Config::kNoKey = unibrow::Utf8::kBadChar; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5587 | |
| 5588 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5589 | void DispatchTable::AddRange(CharacterRange full_range, int value, |
| 5590 | Zone* zone) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5591 | CharacterRange current = full_range; |
| 5592 | if (tree()->is_empty()) { |
| 5593 | // If this is the first range we just insert into the table. |
| 5594 | ZoneSplayTree<Config>::Locator loc; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5595 | DCHECK_RESULT(tree()->Insert(current.from(), &loc)); |
| 5596 | loc.set_value(Entry(current.from(), current.to(), |
| 5597 | empty()->Extend(value, zone))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5598 | return; |
| 5599 | } |
| 5600 | // First see if there is a range to the left of this one that |
| 5601 | // overlaps. |
| 5602 | ZoneSplayTree<Config>::Locator loc; |
| 5603 | if (tree()->FindGreatestLessThan(current.from(), &loc)) { |
| 5604 | Entry* entry = &loc.value(); |
| 5605 | // If we've found a range that overlaps with this one, and it |
| 5606 | // starts strictly to the left of this one, we have to fix it |
| 5607 | // because the following code only handles ranges that start on |
| 5608 | // or after the start point of the range we're adding. |
| 5609 | if (entry->from() < current.from() && entry->to() >= current.from()) { |
| 5610 | // Snap the overlapping range in half around the start point of |
| 5611 | // the range we're adding. |
| 5612 | CharacterRange left(entry->from(), current.from() - 1); |
| 5613 | CharacterRange right(current.from(), entry->to()); |
| 5614 | // The left part of the overlapping range doesn't overlap. |
| 5615 | // Truncate the whole entry to be just the left part. |
| 5616 | entry->set_to(left.to()); |
| 5617 | // The right part is the one that overlaps. We add this part |
| 5618 | // to the map and let the next step deal with merging it with |
| 5619 | // the range we're adding. |
| 5620 | ZoneSplayTree<Config>::Locator loc; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5621 | DCHECK_RESULT(tree()->Insert(right.from(), &loc)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5622 | loc.set_value(Entry(right.from(), |
| 5623 | right.to(), |
| 5624 | entry->out_set())); |
| 5625 | } |
| 5626 | } |
| 5627 | while (current.is_valid()) { |
| 5628 | if (tree()->FindLeastGreaterThan(current.from(), &loc) && |
| 5629 | (loc.value().from() <= current.to()) && |
| 5630 | (loc.value().to() >= current.from())) { |
| 5631 | Entry* entry = &loc.value(); |
| 5632 | // We have overlap. If there is space between the start point of |
| 5633 | // the range we're adding and where the overlapping range starts |
| 5634 | // then we have to add a range covering just that space. |
| 5635 | if (current.from() < entry->from()) { |
| 5636 | ZoneSplayTree<Config>::Locator ins; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5637 | DCHECK_RESULT(tree()->Insert(current.from(), &ins)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5638 | ins.set_value(Entry(current.from(), |
| 5639 | entry->from() - 1, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5640 | empty()->Extend(value, zone))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5641 | current.set_from(entry->from()); |
| 5642 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5643 | DCHECK_EQ(current.from(), entry->from()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5644 | // If the overlapping range extends beyond the one we want to add |
| 5645 | // we have to snap the right part off and add it separately. |
| 5646 | if (entry->to() > current.to()) { |
| 5647 | ZoneSplayTree<Config>::Locator ins; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5648 | DCHECK_RESULT(tree()->Insert(current.to() + 1, &ins)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5649 | ins.set_value(Entry(current.to() + 1, |
| 5650 | entry->to(), |
| 5651 | entry->out_set())); |
| 5652 | entry->set_to(current.to()); |
| 5653 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5654 | DCHECK(entry->to() <= current.to()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5655 | // The overlapping range is now completely contained by the range |
| 5656 | // we're adding so we can just update it and move the start point |
| 5657 | // of the range we're adding just past it. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5658 | entry->AddValue(value, zone); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5659 | // Bail out if the last interval ended at 0xFFFF since otherwise |
| 5660 | // adding 1 will wrap around to 0. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5661 | if (entry->to() == String::kMaxUtf16CodeUnit) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5662 | break; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5663 | DCHECK(entry->to() + 1 > current.from()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5664 | current.set_from(entry->to() + 1); |
| 5665 | } else { |
| 5666 | // There is no overlap so we can just add the range |
| 5667 | ZoneSplayTree<Config>::Locator ins; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5668 | DCHECK_RESULT(tree()->Insert(current.from(), &ins)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5669 | ins.set_value(Entry(current.from(), |
| 5670 | current.to(), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5671 | empty()->Extend(value, zone))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5672 | break; |
| 5673 | } |
| 5674 | } |
| 5675 | } |
| 5676 | |
| 5677 | |
| 5678 | OutSet* DispatchTable::Get(uc16 value) { |
| 5679 | ZoneSplayTree<Config>::Locator loc; |
| 5680 | if (!tree()->FindGreatestLessThan(value, &loc)) |
| 5681 | return empty(); |
| 5682 | Entry* entry = &loc.value(); |
| 5683 | if (value <= entry->to()) |
| 5684 | return entry->out_set(); |
| 5685 | else |
| 5686 | return empty(); |
| 5687 | } |
| 5688 | |
| 5689 | |
| 5690 | // ------------------------------------------------------------------- |
| 5691 | // Analysis |
| 5692 | |
| 5693 | |
| 5694 | void Analysis::EnsureAnalyzed(RegExpNode* that) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5695 | StackLimitCheck check(that->zone()->isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5696 | if (check.HasOverflowed()) { |
| 5697 | fail("Stack overflow"); |
| 5698 | return; |
| 5699 | } |
| 5700 | if (that->info()->been_analyzed || that->info()->being_analyzed) |
| 5701 | return; |
| 5702 | that->info()->being_analyzed = true; |
| 5703 | that->Accept(this); |
| 5704 | that->info()->being_analyzed = false; |
| 5705 | that->info()->been_analyzed = true; |
| 5706 | } |
| 5707 | |
| 5708 | |
| 5709 | void Analysis::VisitEnd(EndNode* that) { |
| 5710 | // nothing to do |
| 5711 | } |
| 5712 | |
| 5713 | |
| 5714 | void TextNode::CalculateOffsets() { |
| 5715 | int element_count = elements()->length(); |
| 5716 | // Set up the offsets of the elements relative to the start. This is a fixed |
| 5717 | // quantity since a TextNode can only contain fixed-width things. |
| 5718 | int cp_offset = 0; |
| 5719 | for (int i = 0; i < element_count; i++) { |
| 5720 | TextElement& elm = elements()->at(i); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5721 | elm.set_cp_offset(cp_offset); |
| 5722 | cp_offset += elm.length(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5723 | } |
| 5724 | } |
| 5725 | |
| 5726 | |
| 5727 | void Analysis::VisitText(TextNode* that) { |
| 5728 | if (ignore_case_) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5729 | that->MakeCaseIndependent(is_one_byte_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5730 | } |
| 5731 | EnsureAnalyzed(that->on_success()); |
| 5732 | if (!has_failed()) { |
| 5733 | that->CalculateOffsets(); |
| 5734 | } |
| 5735 | } |
| 5736 | |
| 5737 | |
| 5738 | void Analysis::VisitAction(ActionNode* that) { |
| 5739 | RegExpNode* target = that->on_success(); |
| 5740 | EnsureAnalyzed(target); |
| 5741 | if (!has_failed()) { |
| 5742 | // If the next node is interested in what it follows then this node |
| 5743 | // has to be interested too so it can pass the information on. |
| 5744 | that->info()->AddFromFollowing(target->info()); |
| 5745 | } |
| 5746 | } |
| 5747 | |
| 5748 | |
| 5749 | void Analysis::VisitChoice(ChoiceNode* that) { |
| 5750 | NodeInfo* info = that->info(); |
| 5751 | for (int i = 0; i < that->alternatives()->length(); i++) { |
| 5752 | RegExpNode* node = that->alternatives()->at(i).node(); |
| 5753 | EnsureAnalyzed(node); |
| 5754 | if (has_failed()) return; |
| 5755 | // Anything the following nodes need to know has to be known by |
| 5756 | // this node also, so it can pass it on. |
| 5757 | info->AddFromFollowing(node->info()); |
| 5758 | } |
| 5759 | } |
| 5760 | |
| 5761 | |
| 5762 | void Analysis::VisitLoopChoice(LoopChoiceNode* that) { |
| 5763 | NodeInfo* info = that->info(); |
| 5764 | for (int i = 0; i < that->alternatives()->length(); i++) { |
| 5765 | RegExpNode* node = that->alternatives()->at(i).node(); |
| 5766 | if (node != that->loop_node()) { |
| 5767 | EnsureAnalyzed(node); |
| 5768 | if (has_failed()) return; |
| 5769 | info->AddFromFollowing(node->info()); |
| 5770 | } |
| 5771 | } |
| 5772 | // Check the loop last since it may need the value of this node |
| 5773 | // to get a correct result. |
| 5774 | EnsureAnalyzed(that->loop_node()); |
| 5775 | if (!has_failed()) { |
| 5776 | info->AddFromFollowing(that->loop_node()->info()); |
| 5777 | } |
| 5778 | } |
| 5779 | |
| 5780 | |
| 5781 | void Analysis::VisitBackReference(BackReferenceNode* that) { |
| 5782 | EnsureAnalyzed(that->on_success()); |
| 5783 | } |
| 5784 | |
| 5785 | |
| 5786 | void Analysis::VisitAssertion(AssertionNode* that) { |
| 5787 | EnsureAnalyzed(that->on_success()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5788 | } |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5789 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5790 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5791 | void BackReferenceNode::FillInBMInfo(int offset, |
| 5792 | int budget, |
| 5793 | BoyerMooreLookahead* bm, |
| 5794 | bool not_at_start) { |
| 5795 | // Working out the set of characters that a backreference can match is too |
| 5796 | // hard, so we just say that any character can match. |
| 5797 | bm->SetRest(offset); |
| 5798 | SaveBMInfo(bm, not_at_start, offset); |
| 5799 | } |
| 5800 | |
| 5801 | |
| 5802 | STATIC_ASSERT(BoyerMoorePositionInfo::kMapSize == |
| 5803 | RegExpMacroAssembler::kTableSize); |
| 5804 | |
| 5805 | |
| 5806 | void ChoiceNode::FillInBMInfo(int offset, |
| 5807 | int budget, |
| 5808 | BoyerMooreLookahead* bm, |
| 5809 | bool not_at_start) { |
| 5810 | ZoneList<GuardedAlternative>* alts = alternatives(); |
| 5811 | budget = (budget - 1) / alts->length(); |
| 5812 | for (int i = 0; i < alts->length(); i++) { |
| 5813 | GuardedAlternative& alt = alts->at(i); |
| 5814 | if (alt.guards() != NULL && alt.guards()->length() != 0) { |
| 5815 | bm->SetRest(offset); // Give up trying to fill in info. |
| 5816 | SaveBMInfo(bm, not_at_start, offset); |
| 5817 | return; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5818 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5819 | alt.node()->FillInBMInfo(offset, budget, bm, not_at_start); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5820 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5821 | SaveBMInfo(bm, not_at_start, offset); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5822 | } |
| 5823 | |
| 5824 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5825 | void TextNode::FillInBMInfo(int initial_offset, |
| 5826 | int budget, |
| 5827 | BoyerMooreLookahead* bm, |
| 5828 | bool not_at_start) { |
| 5829 | if (initial_offset >= bm->length()) return; |
| 5830 | int offset = initial_offset; |
| 5831 | int max_char = bm->max_char(); |
| 5832 | for (int i = 0; i < elements()->length(); i++) { |
| 5833 | if (offset >= bm->length()) { |
| 5834 | if (initial_offset == 0) set_bm_info(not_at_start, bm); |
| 5835 | return; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5836 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5837 | TextElement text = elements()->at(i); |
| 5838 | if (text.text_type() == TextElement::ATOM) { |
| 5839 | RegExpAtom* atom = text.atom(); |
| 5840 | for (int j = 0; j < atom->length(); j++, offset++) { |
| 5841 | if (offset >= bm->length()) { |
| 5842 | if (initial_offset == 0) set_bm_info(not_at_start, bm); |
| 5843 | return; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5844 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5845 | uc16 character = atom->data()[j]; |
| 5846 | if (bm->compiler()->ignore_case()) { |
| 5847 | unibrow::uchar chars[unibrow::Ecma262UnCanonicalize::kMaxWidth]; |
| 5848 | int length = GetCaseIndependentLetters( |
| 5849 | Isolate::Current(), |
| 5850 | character, |
| 5851 | bm->max_char() == String::kMaxOneByteCharCode, |
| 5852 | chars); |
| 5853 | for (int j = 0; j < length; j++) { |
| 5854 | bm->Set(offset, chars[j]); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5855 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5856 | } else { |
| 5857 | if (character <= max_char) bm->Set(offset, character); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5858 | } |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5859 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5860 | } else { |
| 5861 | DCHECK_EQ(TextElement::CHAR_CLASS, text.text_type()); |
| 5862 | RegExpCharacterClass* char_class = text.char_class(); |
| 5863 | ZoneList<CharacterRange>* ranges = char_class->ranges(zone()); |
| 5864 | if (char_class->is_negated()) { |
| 5865 | bm->SetAll(offset); |
| 5866 | } else { |
| 5867 | for (int k = 0; k < ranges->length(); k++) { |
| 5868 | CharacterRange& range = ranges->at(k); |
| 5869 | if (range.from() > max_char) continue; |
| 5870 | int to = Min(max_char, static_cast<int>(range.to())); |
| 5871 | bm->SetInterval(offset, Interval(range.from(), to)); |
| 5872 | } |
| 5873 | } |
| 5874 | offset++; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5875 | } |
| 5876 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5877 | if (offset >= bm->length()) { |
| 5878 | if (initial_offset == 0) set_bm_info(not_at_start, bm); |
| 5879 | return; |
| 5880 | } |
| 5881 | on_success()->FillInBMInfo(offset, |
| 5882 | budget - 1, |
| 5883 | bm, |
| 5884 | true); // Not at start after a text node. |
| 5885 | if (initial_offset == 0) set_bm_info(not_at_start, bm); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5886 | } |
| 5887 | |
| 5888 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5889 | // ------------------------------------------------------------------- |
| 5890 | // Dispatch table construction |
| 5891 | |
| 5892 | |
| 5893 | void DispatchTableConstructor::VisitEnd(EndNode* that) { |
| 5894 | AddRange(CharacterRange::Everything()); |
| 5895 | } |
| 5896 | |
| 5897 | |
| 5898 | void DispatchTableConstructor::BuildTable(ChoiceNode* node) { |
| 5899 | node->set_being_calculated(true); |
| 5900 | ZoneList<GuardedAlternative>* alternatives = node->alternatives(); |
| 5901 | for (int i = 0; i < alternatives->length(); i++) { |
| 5902 | set_choice_index(i); |
| 5903 | alternatives->at(i).node()->Accept(this); |
| 5904 | } |
| 5905 | node->set_being_calculated(false); |
| 5906 | } |
| 5907 | |
| 5908 | |
| 5909 | class AddDispatchRange { |
| 5910 | public: |
| 5911 | explicit AddDispatchRange(DispatchTableConstructor* constructor) |
| 5912 | : constructor_(constructor) { } |
| 5913 | void Call(uc32 from, DispatchTable::Entry entry); |
| 5914 | private: |
| 5915 | DispatchTableConstructor* constructor_; |
| 5916 | }; |
| 5917 | |
| 5918 | |
| 5919 | void AddDispatchRange::Call(uc32 from, DispatchTable::Entry entry) { |
| 5920 | CharacterRange range(from, entry.to()); |
| 5921 | constructor_->AddRange(range); |
| 5922 | } |
| 5923 | |
| 5924 | |
| 5925 | void DispatchTableConstructor::VisitChoice(ChoiceNode* node) { |
| 5926 | if (node->being_calculated()) |
| 5927 | return; |
| 5928 | DispatchTable* table = node->GetTable(ignore_case_); |
| 5929 | AddDispatchRange adder(this); |
| 5930 | table->ForEach(&adder); |
| 5931 | } |
| 5932 | |
| 5933 | |
| 5934 | void DispatchTableConstructor::VisitBackReference(BackReferenceNode* that) { |
| 5935 | // TODO(160): Find the node that we refer back to and propagate its start |
| 5936 | // set back to here. For now we just accept anything. |
| 5937 | AddRange(CharacterRange::Everything()); |
| 5938 | } |
| 5939 | |
| 5940 | |
| 5941 | void DispatchTableConstructor::VisitAssertion(AssertionNode* that) { |
| 5942 | RegExpNode* target = that->on_success(); |
| 5943 | target->Accept(this); |
| 5944 | } |
| 5945 | |
| 5946 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5947 | static int CompareRangeByFrom(const CharacterRange* a, |
| 5948 | const CharacterRange* b) { |
| 5949 | return Compare<uc16>(a->from(), b->from()); |
| 5950 | } |
| 5951 | |
| 5952 | |
| 5953 | void DispatchTableConstructor::AddInverse(ZoneList<CharacterRange>* ranges) { |
| 5954 | ranges->Sort(CompareRangeByFrom); |
| 5955 | uc16 last = 0; |
| 5956 | for (int i = 0; i < ranges->length(); i++) { |
| 5957 | CharacterRange range = ranges->at(i); |
| 5958 | if (last < range.from()) |
| 5959 | AddRange(CharacterRange(last, range.from() - 1)); |
| 5960 | if (range.to() >= last) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5961 | if (range.to() == String::kMaxUtf16CodeUnit) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5962 | return; |
| 5963 | } else { |
| 5964 | last = range.to() + 1; |
| 5965 | } |
| 5966 | } |
| 5967 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5968 | AddRange(CharacterRange(last, String::kMaxUtf16CodeUnit)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5969 | } |
| 5970 | |
| 5971 | |
| 5972 | void DispatchTableConstructor::VisitText(TextNode* that) { |
| 5973 | TextElement elm = that->elements()->at(0); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5974 | switch (elm.text_type()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5975 | case TextElement::ATOM: { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5976 | uc16 c = elm.atom()->data()[0]; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5977 | AddRange(CharacterRange(c, c)); |
| 5978 | break; |
| 5979 | } |
| 5980 | case TextElement::CHAR_CLASS: { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5981 | RegExpCharacterClass* tree = elm.char_class(); |
| 5982 | ZoneList<CharacterRange>* ranges = tree->ranges(that->zone()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5983 | if (tree->is_negated()) { |
| 5984 | AddInverse(ranges); |
| 5985 | } else { |
| 5986 | for (int i = 0; i < ranges->length(); i++) |
| 5987 | AddRange(ranges->at(i)); |
| 5988 | } |
| 5989 | break; |
| 5990 | } |
| 5991 | default: { |
| 5992 | UNIMPLEMENTED(); |
| 5993 | } |
| 5994 | } |
| 5995 | } |
| 5996 | |
| 5997 | |
| 5998 | void DispatchTableConstructor::VisitAction(ActionNode* that) { |
| 5999 | RegExpNode* target = that->on_success(); |
| 6000 | target->Accept(this); |
| 6001 | } |
| 6002 | |
| 6003 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6004 | RegExpEngine::CompilationResult RegExpEngine::Compile( |
| 6005 | RegExpCompileData* data, bool ignore_case, bool is_global, |
| 6006 | bool is_multiline, bool is_sticky, Handle<String> pattern, |
| 6007 | Handle<String> sample_subject, bool is_one_byte, Zone* zone) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6008 | if ((data->capture_count + 1) * 2 - 1 > RegExpMacroAssembler::kMaxRegister) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6009 | return IrregexpRegExpTooBig(zone->isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6010 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6011 | RegExpCompiler compiler(data->capture_count, ignore_case, is_one_byte, zone); |
| 6012 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 6013 | compiler.set_optimize(!TooMuchRegExpCode(pattern)); |
| 6014 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6015 | // Sample some characters from the middle of the string. |
| 6016 | static const int kSampleSize = 128; |
| 6017 | |
| 6018 | sample_subject = String::Flatten(sample_subject); |
| 6019 | int chars_sampled = 0; |
| 6020 | int half_way = (sample_subject->length() - kSampleSize) / 2; |
| 6021 | for (int i = Max(0, half_way); |
| 6022 | i < sample_subject->length() && chars_sampled < kSampleSize; |
| 6023 | i++, chars_sampled++) { |
| 6024 | compiler.frequency_collator()->CountCharacter(sample_subject->Get(i)); |
| 6025 | } |
| 6026 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6027 | // Wrap the body of the regexp in capture #0. |
| 6028 | RegExpNode* captured_body = RegExpCapture::ToNode(data->tree, |
| 6029 | 0, |
| 6030 | &compiler, |
| 6031 | compiler.accept()); |
| 6032 | RegExpNode* node = captured_body; |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 6033 | bool is_end_anchored = data->tree->IsAnchoredAtEnd(); |
| 6034 | bool is_start_anchored = data->tree->IsAnchoredAtStart(); |
| 6035 | int max_length = data->tree->max_match(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6036 | if (!is_start_anchored && !is_sticky) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6037 | // Add a .*? at the beginning, outside the body capture, unless |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6038 | // this expression is anchored at the beginning or sticky. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6039 | RegExpNode* loop_node = |
| 6040 | RegExpQuantifier::ToNode(0, |
| 6041 | RegExpTree::kInfinity, |
| 6042 | false, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6043 | new(zone) RegExpCharacterClass('*'), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6044 | &compiler, |
| 6045 | captured_body, |
| 6046 | data->contains_anchor); |
| 6047 | |
| 6048 | if (data->contains_anchor) { |
| 6049 | // Unroll loop once, to take care of the case that might start |
| 6050 | // at the start of input. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6051 | ChoiceNode* first_step_node = new(zone) ChoiceNode(2, zone); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6052 | first_step_node->AddAlternative(GuardedAlternative(captured_body)); |
| 6053 | first_step_node->AddAlternative(GuardedAlternative( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6054 | new(zone) TextNode(new(zone) RegExpCharacterClass('*'), loop_node))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6055 | node = first_step_node; |
| 6056 | } else { |
| 6057 | node = loop_node; |
| 6058 | } |
| 6059 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6060 | if (is_one_byte) { |
| 6061 | node = node->FilterOneByte(RegExpCompiler::kMaxRecursion, ignore_case); |
| 6062 | // Do it again to propagate the new nodes to places where they were not |
| 6063 | // put because they had not been calculated yet. |
| 6064 | if (node != NULL) { |
| 6065 | node = node->FilterOneByte(RegExpCompiler::kMaxRecursion, ignore_case); |
| 6066 | } |
| 6067 | } |
| 6068 | |
| 6069 | if (node == NULL) node = new(zone) EndNode(EndNode::BACKTRACK, zone); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6070 | data->node = node; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6071 | Analysis analysis(ignore_case, is_one_byte); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6072 | analysis.EnsureAnalyzed(node); |
| 6073 | if (analysis.has_failed()) { |
| 6074 | const char* error_message = analysis.error_message(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6075 | return CompilationResult(zone->isolate(), error_message); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6076 | } |
| 6077 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6078 | // Create the correct assembler for the architecture. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6079 | #ifndef V8_INTERPRETED_REGEXP |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6080 | // Native regexp implementation. |
| 6081 | |
| 6082 | NativeRegExpMacroAssembler::Mode mode = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6083 | is_one_byte ? NativeRegExpMacroAssembler::LATIN1 |
| 6084 | : NativeRegExpMacroAssembler::UC16; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6085 | |
| 6086 | #if V8_TARGET_ARCH_IA32 |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6087 | RegExpMacroAssemblerIA32 macro_assembler(mode, (data->capture_count + 1) * 2, |
| 6088 | zone); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6089 | #elif V8_TARGET_ARCH_X64 |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6090 | RegExpMacroAssemblerX64 macro_assembler(mode, (data->capture_count + 1) * 2, |
| 6091 | zone); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6092 | #elif V8_TARGET_ARCH_ARM |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6093 | RegExpMacroAssemblerARM macro_assembler(mode, (data->capture_count + 1) * 2, |
| 6094 | zone); |
| 6095 | #elif V8_TARGET_ARCH_ARM64 |
| 6096 | RegExpMacroAssemblerARM64 macro_assembler(mode, (data->capture_count + 1) * 2, |
| 6097 | zone); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6098 | #elif V8_TARGET_ARCH_MIPS |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6099 | RegExpMacroAssemblerMIPS macro_assembler(mode, (data->capture_count + 1) * 2, |
| 6100 | zone); |
| 6101 | #elif V8_TARGET_ARCH_MIPS64 |
| 6102 | RegExpMacroAssemblerMIPS macro_assembler(mode, (data->capture_count + 1) * 2, |
| 6103 | zone); |
| 6104 | #elif V8_TARGET_ARCH_X87 |
| 6105 | RegExpMacroAssemblerX87 macro_assembler(mode, (data->capture_count + 1) * 2, |
| 6106 | zone); |
| 6107 | #else |
| 6108 | #error "Unsupported architecture" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6109 | #endif |
| 6110 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6111 | #else // V8_INTERPRETED_REGEXP |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6112 | // Interpreted regexp implementation. |
| 6113 | EmbeddedVector<byte, 1024> codes; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6114 | RegExpMacroAssemblerIrregexp macro_assembler(codes, zone); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6115 | #endif // V8_INTERPRETED_REGEXP |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6116 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 6117 | macro_assembler.set_slow_safe(TooMuchRegExpCode(pattern)); |
| 6118 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 6119 | // Inserted here, instead of in Assembler, because it depends on information |
| 6120 | // in the AST that isn't replicated in the Node structure. |
| 6121 | static const int kMaxBacksearchLimit = 1024; |
| 6122 | if (is_end_anchored && |
| 6123 | !is_start_anchored && |
| 6124 | max_length < kMaxBacksearchLimit) { |
| 6125 | macro_assembler.SetCurrentPositionFromEnd(max_length); |
| 6126 | } |
| 6127 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6128 | if (is_global) { |
| 6129 | macro_assembler.set_global_mode( |
| 6130 | (data->tree->min_match() > 0) |
| 6131 | ? RegExpMacroAssembler::GLOBAL_NO_ZERO_LENGTH_CHECK |
| 6132 | : RegExpMacroAssembler::GLOBAL); |
| 6133 | } |
| 6134 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6135 | return compiler.Assemble(¯o_assembler, |
| 6136 | node, |
| 6137 | data->capture_count, |
| 6138 | pattern); |
| 6139 | } |
| 6140 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 6141 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 6142 | bool RegExpEngine::TooMuchRegExpCode(Handle<String> pattern) { |
| 6143 | Heap* heap = pattern->GetHeap(); |
| 6144 | bool too_much = pattern->length() > RegExpImpl::kRegExpTooLargeToOptimize; |
| 6145 | if (heap->total_regexp_code_generated() > RegExpImpl::kRegExpCompiledLimit && |
| 6146 | heap->isolate()->memory_allocator()->SizeExecutable() > |
| 6147 | RegExpImpl::kRegExpExecutableMemoryLimit) { |
| 6148 | too_much = true; |
| 6149 | } |
| 6150 | return too_much; |
| 6151 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6152 | }} // namespace v8::internal |