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