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