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