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