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