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