yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 1 | // Copyright 2012 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 | #ifndef V8_JSREGEXP_H_ |
| 29 | #define V8_JSREGEXP_H_ |
| 30 | |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 31 | #include "allocation.h" |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 32 | #include "assembler.h" |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 33 | #include "zone-inl.h" |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 34 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 35 | namespace v8 { |
| 36 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 37 | |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 38 | class NodeVisitor; |
| 39 | class RegExpCompiler; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 40 | class RegExpMacroAssembler; |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 41 | class RegExpNode; |
| 42 | class RegExpTree; |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 43 | class BoyerMooreLookahead; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 44 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 45 | class RegExpImpl { |
| 46 | public: |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 47 | // Whether V8 is compiled with native regexp support or not. |
| 48 | static bool UsesNativeRegExp() { |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 49 | #ifdef V8_INTERPRETED_REGEXP |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 50 | return false; |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 51 | #else |
| 52 | return true; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 53 | #endif |
| 54 | } |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 55 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 56 | // Creates a regular expression literal in the old space. |
| 57 | // This function calls the garbage collector if necessary. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 58 | static Handle<Object> CreateRegExpLiteral(Handle<JSFunction> constructor, |
| 59 | Handle<String> pattern, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 60 | Handle<String> flags, |
| 61 | bool* has_pending_exception); |
| 62 | |
| 63 | // Returns a string representation of a regular expression. |
| 64 | // Implements RegExp.prototype.toString, see ECMA-262 section 15.10.6.4. |
| 65 | // This function calls the garbage collector if necessary. |
| 66 | static Handle<String> ToString(Handle<Object> value); |
| 67 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 68 | // Parses the RegExp pattern and prepares the JSRegExp object with |
| 69 | // generic data and choice of implementation - as well as what |
| 70 | // the implementation wants to store in the data field. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 71 | // Returns false if compilation fails. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 72 | static Handle<Object> Compile(Handle<JSRegExp> re, |
| 73 | Handle<String> pattern, |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame^] | 74 | Handle<String> flags, |
| 75 | Zone* zone); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 76 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 77 | // See ECMA-262 section 15.10.6.2. |
| 78 | // This function calls the garbage collector if necessary. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 79 | static Handle<Object> Exec(Handle<JSRegExp> regexp, |
| 80 | Handle<String> subject, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 81 | int index, |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame^] | 82 | Handle<JSArray> lastMatchInfo); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 83 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 84 | // Prepares a JSRegExp object with Irregexp-specific data. |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 85 | static void IrregexpInitialize(Handle<JSRegExp> re, |
| 86 | Handle<String> pattern, |
| 87 | JSRegExp::Flags flags, |
| 88 | int capture_register_count); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 89 | |
| 90 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 91 | static void AtomCompile(Handle<JSRegExp> re, |
| 92 | Handle<String> pattern, |
| 93 | JSRegExp::Flags flags, |
| 94 | Handle<String> match_pattern); |
| 95 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 96 | static Handle<Object> AtomExec(Handle<JSRegExp> regexp, |
| 97 | Handle<String> subject, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 98 | int index, |
| 99 | Handle<JSArray> lastMatchInfo); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 100 | |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 101 | enum IrregexpResult { RE_FAILURE = 0, RE_SUCCESS = 1, RE_EXCEPTION = -1 }; |
| 102 | |
| 103 | // Prepare a RegExp for being executed one or more times (using |
| 104 | // IrregexpExecOnce) on the subject. |
| 105 | // This ensures that the regexp is compiled for the subject, and that |
| 106 | // the subject is flat. |
| 107 | // Returns the number of integer spaces required by IrregexpExecOnce |
| 108 | // as its "registers" argument. If the regexp cannot be compiled, |
| 109 | // an exception is set as pending, and this function returns negative. |
| 110 | static int IrregexpPrepare(Handle<JSRegExp> regexp, |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame^] | 111 | Handle<String> subject); |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 112 | |
mstarzinger@chromium.org | 15613d0 | 2012-05-23 12:04:37 +0000 | [diff] [blame] | 113 | // Calculate the size of offsets vector for the case of global regexp |
| 114 | // and the number of matches this vector is able to store. |
| 115 | static int GlobalOffsetsVectorSize(Handle<JSRegExp> regexp, |
| 116 | int registers_per_match, |
| 117 | int* max_matches); |
| 118 | |
| 119 | // Execute a regular expression on the subject, starting from index. |
| 120 | // If matching succeeds, return the number of matches. This can be larger |
| 121 | // than one in the case of global regular expressions. |
| 122 | // The captures and subcaptures are stored into the registers vector. |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 123 | // If matching fails, returns RE_FAILURE. |
| 124 | // If execution fails, sets a pending exception and returns RE_EXCEPTION. |
mstarzinger@chromium.org | 15613d0 | 2012-05-23 12:04:37 +0000 | [diff] [blame] | 125 | static int IrregexpExecRaw(Handle<JSRegExp> regexp, |
| 126 | Handle<String> subject, |
| 127 | int index, |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame^] | 128 | Vector<int> registers); |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 129 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 130 | // Execute an Irregexp bytecode pattern. |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 131 | // On a successful match, the result is a JSArray containing |
| 132 | // captured positions. On a failure, the result is the null value. |
| 133 | // Returns an empty handle in case of an exception. |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 134 | static Handle<Object> IrregexpExec(Handle<JSRegExp> regexp, |
| 135 | Handle<String> subject, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 136 | int index, |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame^] | 137 | Handle<JSArray> lastMatchInfo); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 138 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 139 | // Array index in the lastMatchInfo array. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 140 | static const int kLastCaptureCount = 0; |
| 141 | static const int kLastSubject = 1; |
| 142 | static const int kLastInput = 2; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 143 | static const int kFirstCapture = 3; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 144 | static const int kLastMatchOverhead = 3; |
| 145 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 146 | // Direct offset into the lastMatchInfo array. |
| 147 | static const int kLastCaptureCountOffset = |
| 148 | FixedArray::kHeaderSize + kLastCaptureCount * kPointerSize; |
| 149 | static const int kLastSubjectOffset = |
| 150 | FixedArray::kHeaderSize + kLastSubject * kPointerSize; |
| 151 | static const int kLastInputOffset = |
| 152 | FixedArray::kHeaderSize + kLastInput * kPointerSize; |
| 153 | static const int kFirstCaptureOffset = |
| 154 | FixedArray::kHeaderSize + kFirstCapture * kPointerSize; |
| 155 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 156 | // Used to access the lastMatchInfo array. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 157 | static int GetCapture(FixedArray* array, int index) { |
| 158 | return Smi::cast(array->get(index + kFirstCapture))->value(); |
| 159 | } |
| 160 | |
| 161 | static void SetLastCaptureCount(FixedArray* array, int to) { |
| 162 | array->set(kLastCaptureCount, Smi::FromInt(to)); |
| 163 | } |
| 164 | |
| 165 | static void SetLastSubject(FixedArray* array, String* to) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 166 | array->set(kLastSubject, to); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | static void SetLastInput(FixedArray* array, String* to) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 170 | array->set(kLastInput, to); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | static void SetCapture(FixedArray* array, int index, int to) { |
| 174 | array->set(index + kFirstCapture, Smi::FromInt(to)); |
| 175 | } |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 176 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 177 | static int GetLastCaptureCount(FixedArray* array) { |
| 178 | return Smi::cast(array->get(kLastCaptureCount))->value(); |
| 179 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 180 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 181 | // For acting on the JSRegExp data FixedArray. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 182 | static int IrregexpMaxRegisterCount(FixedArray* re); |
| 183 | static void SetIrregexpMaxRegisterCount(FixedArray* re, int value); |
| 184 | static int IrregexpNumberOfCaptures(FixedArray* re); |
| 185 | static int IrregexpNumberOfRegisters(FixedArray* re); |
| 186 | static ByteArray* IrregexpByteCode(FixedArray* re, bool is_ascii); |
| 187 | static Code* IrregexpNativeCode(FixedArray* re, bool is_ascii); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 188 | |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 189 | // Limit the space regexps take up on the heap. In order to limit this we |
| 190 | // would like to keep track of the amount of regexp code on the heap. This |
| 191 | // is not tracked, however. As a conservative approximation we track the |
| 192 | // total regexp code compiled including code that has subsequently been freed |
| 193 | // and the total executable memory at any point. |
| 194 | static const int kRegExpExecutableMemoryLimit = 16 * MB; |
| 195 | static const int kRegWxpCompiledLimit = 1 * MB; |
| 196 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 197 | private: |
| 198 | static String* last_ascii_string_; |
| 199 | static String* two_byte_cached_string_; |
| 200 | |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 201 | static bool CompileIrregexp( |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame^] | 202 | Handle<JSRegExp> re, Handle<String> sample_subject, bool is_ascii); |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 203 | static inline bool EnsureCompiledIrregexp( |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame^] | 204 | Handle<JSRegExp> re, Handle<String> sample_subject, bool is_ascii); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 205 | |
| 206 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 207 | // Set the subject cache. The previous string buffer is not deleted, so the |
| 208 | // caller should ensure that it doesn't leak. |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 209 | static void SetSubjectCache(String* subject, |
| 210 | char* utf8_subject, |
| 211 | int uft8_length, |
| 212 | int character_position, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 213 | int utf8_position); |
| 214 | |
| 215 | // A one element cache of the last utf8_subject string and its length. The |
| 216 | // subject JS String object is cached in the heap. We also cache a |
| 217 | // translation between position and utf8 position. |
| 218 | static char* utf8_subject_cache_; |
| 219 | static int utf8_length_cache_; |
| 220 | static int utf8_position_; |
| 221 | static int character_position_; |
| 222 | }; |
| 223 | |
| 224 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 225 | // Represents the location of one element relative to the intersection of |
| 226 | // two sets. Corresponds to the four areas of a Venn diagram. |
| 227 | enum ElementInSetsRelation { |
| 228 | kInsideNone = 0, |
| 229 | kInsideFirst = 1, |
| 230 | kInsideSecond = 2, |
| 231 | kInsideBoth = 3 |
| 232 | }; |
| 233 | |
| 234 | |
danno@chromium.org | 1044a4d | 2012-04-30 12:34:39 +0000 | [diff] [blame] | 235 | // Represents code units in the range from from_ to to_, both ends are |
| 236 | // inclusive. |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 237 | class CharacterRange { |
| 238 | public: |
| 239 | CharacterRange() : from_(0), to_(0) { } |
| 240 | // For compatibility with the CHECK_OK macro |
| 241 | CharacterRange(void* null) { ASSERT_EQ(NULL, null); } //NOLINT |
| 242 | CharacterRange(uc16 from, uc16 to) : from_(from), to_(to) { } |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 243 | static void AddClassEscape(uc16 type, ZoneList<CharacterRange>* ranges, |
| 244 | Zone* zone); |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 245 | static Vector<const int> GetWordBounds(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 246 | static inline CharacterRange Singleton(uc16 value) { |
| 247 | return CharacterRange(value, value); |
| 248 | } |
| 249 | static inline CharacterRange Range(uc16 from, uc16 to) { |
| 250 | ASSERT(from <= to); |
| 251 | return CharacterRange(from, to); |
| 252 | } |
| 253 | static inline CharacterRange Everything() { |
| 254 | return CharacterRange(0, 0xFFFF); |
| 255 | } |
| 256 | bool Contains(uc16 i) { return from_ <= i && i <= to_; } |
| 257 | uc16 from() const { return from_; } |
| 258 | void set_from(uc16 value) { from_ = value; } |
| 259 | uc16 to() const { return to_; } |
| 260 | void set_to(uc16 value) { to_ = value; } |
| 261 | bool is_valid() { return from_ <= to_; } |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 262 | bool IsEverything(uc16 max) { return from_ == 0 && to_ >= max; } |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 263 | bool IsSingleton() { return (from_ == to_); } |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 264 | void AddCaseEquivalents(ZoneList<CharacterRange>* ranges, bool is_ascii, |
| 265 | Zone* zone); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 266 | static void Split(ZoneList<CharacterRange>* base, |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 267 | Vector<const int> overlay, |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 268 | ZoneList<CharacterRange>** included, |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 269 | ZoneList<CharacterRange>** excluded, |
| 270 | Zone* zone); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 271 | // Whether a range list is in canonical form: Ranges ordered by from value, |
| 272 | // and ranges non-overlapping and non-adjacent. |
| 273 | static bool IsCanonical(ZoneList<CharacterRange>* ranges); |
| 274 | // Convert range list to canonical form. The characters covered by the ranges |
| 275 | // will still be the same, but no character is in more than one range, and |
| 276 | // adjacent ranges are merged. The resulting list may be shorter than the |
| 277 | // original, but cannot be longer. |
| 278 | static void Canonicalize(ZoneList<CharacterRange>* ranges); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 279 | // Negate the contents of a character range in canonical form. |
| 280 | static void Negate(ZoneList<CharacterRange>* src, |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 281 | ZoneList<CharacterRange>* dst, |
| 282 | Zone* zone); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 283 | static const int kStartMarker = (1 << 24); |
| 284 | static const int kPayloadMask = (1 << 24) - 1; |
| 285 | |
| 286 | private: |
| 287 | uc16 from_; |
| 288 | uc16 to_; |
| 289 | }; |
| 290 | |
| 291 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 292 | // A set of unsigned integers that behaves especially well on small |
| 293 | // integers (< 32). May do zone-allocation. |
| 294 | class OutSet: public ZoneObject { |
| 295 | public: |
| 296 | OutSet() : first_(0), remaining_(NULL), successors_(NULL) { } |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 297 | OutSet* Extend(unsigned value, Zone* zone); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 298 | bool Get(unsigned value); |
| 299 | static const unsigned kFirstLimit = 32; |
| 300 | |
| 301 | private: |
| 302 | // Destructively set a value in this set. In most cases you want |
| 303 | // to use Extend instead to ensure that only one instance exists |
| 304 | // that contains the same values. |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 305 | void Set(unsigned value, Zone* zone); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 306 | |
| 307 | // The successors are a list of sets that contain the same values |
| 308 | // as this set and the one more value that is not present in this |
| 309 | // set. |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 310 | ZoneList<OutSet*>* successors(Zone* zone) { return successors_; } |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 311 | |
| 312 | OutSet(uint32_t first, ZoneList<unsigned>* remaining) |
| 313 | : first_(first), remaining_(remaining), successors_(NULL) { } |
| 314 | uint32_t first_; |
| 315 | ZoneList<unsigned>* remaining_; |
| 316 | ZoneList<OutSet*>* successors_; |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 317 | friend class Trace; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 318 | }; |
| 319 | |
| 320 | |
| 321 | // A mapping from integers, specified as ranges, to a set of integers. |
| 322 | // Used for mapping character ranges to choices. |
| 323 | class DispatchTable : public ZoneObject { |
| 324 | public: |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 325 | explicit DispatchTable(Zone* zone) : tree_(zone) { } |
| 326 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 327 | class Entry { |
| 328 | public: |
| 329 | Entry() : from_(0), to_(0), out_set_(NULL) { } |
| 330 | Entry(uc16 from, uc16 to, OutSet* out_set) |
| 331 | : from_(from), to_(to), out_set_(out_set) { } |
| 332 | uc16 from() { return from_; } |
| 333 | uc16 to() { return to_; } |
| 334 | void set_to(uc16 value) { to_ = value; } |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 335 | void AddValue(int value, Zone* zone) { |
| 336 | out_set_ = out_set_->Extend(value, zone); |
| 337 | } |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 338 | OutSet* out_set() { return out_set_; } |
| 339 | private: |
| 340 | uc16 from_; |
| 341 | uc16 to_; |
| 342 | OutSet* out_set_; |
| 343 | }; |
| 344 | |
| 345 | class Config { |
| 346 | public: |
| 347 | typedef uc16 Key; |
| 348 | typedef Entry Value; |
| 349 | static const uc16 kNoKey; |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 350 | static const Entry NoValue() { return Value(); } |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 351 | static inline int Compare(uc16 a, uc16 b) { |
| 352 | if (a == b) |
| 353 | return 0; |
| 354 | else if (a < b) |
| 355 | return -1; |
| 356 | else |
| 357 | return 1; |
| 358 | } |
| 359 | }; |
| 360 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 361 | void AddRange(CharacterRange range, int value, Zone* zone); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 362 | OutSet* Get(uc16 value); |
| 363 | void Dump(); |
| 364 | |
| 365 | template <typename Callback> |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 366 | void ForEach(Callback* callback) { |
| 367 | return tree()->ForEach(callback); |
| 368 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 369 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 370 | private: |
| 371 | // There can't be a static empty set since it allocates its |
| 372 | // successors in a zone and caches them. |
| 373 | OutSet* empty() { return &empty_; } |
| 374 | OutSet empty_; |
| 375 | ZoneSplayTree<Config>* tree() { return &tree_; } |
| 376 | ZoneSplayTree<Config> tree_; |
| 377 | }; |
| 378 | |
| 379 | |
| 380 | #define FOR_EACH_NODE_TYPE(VISIT) \ |
| 381 | VISIT(End) \ |
| 382 | VISIT(Action) \ |
| 383 | VISIT(Choice) \ |
| 384 | VISIT(BackReference) \ |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 385 | VISIT(Assertion) \ |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 386 | VISIT(Text) |
| 387 | |
| 388 | |
| 389 | #define FOR_EACH_REG_EXP_TREE_TYPE(VISIT) \ |
| 390 | VISIT(Disjunction) \ |
| 391 | VISIT(Alternative) \ |
| 392 | VISIT(Assertion) \ |
| 393 | VISIT(CharacterClass) \ |
| 394 | VISIT(Atom) \ |
| 395 | VISIT(Quantifier) \ |
| 396 | VISIT(Capture) \ |
| 397 | VISIT(Lookahead) \ |
| 398 | VISIT(BackReference) \ |
| 399 | VISIT(Empty) \ |
| 400 | VISIT(Text) |
| 401 | |
| 402 | |
| 403 | #define FORWARD_DECLARE(Name) class RegExp##Name; |
| 404 | FOR_EACH_REG_EXP_TREE_TYPE(FORWARD_DECLARE) |
| 405 | #undef FORWARD_DECLARE |
| 406 | |
| 407 | |
| 408 | class TextElement { |
| 409 | public: |
| 410 | enum Type {UNINITIALIZED, ATOM, CHAR_CLASS}; |
| 411 | TextElement() : type(UNINITIALIZED) { } |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 412 | explicit TextElement(Type t) : type(t), cp_offset(-1) { } |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 413 | static TextElement Atom(RegExpAtom* atom); |
| 414 | static TextElement CharClass(RegExpCharacterClass* char_class); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 415 | int length(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 416 | Type type; |
| 417 | union { |
| 418 | RegExpAtom* u_atom; |
| 419 | RegExpCharacterClass* u_char_class; |
| 420 | } data; |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 421 | int cp_offset; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 422 | }; |
| 423 | |
| 424 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 425 | class Trace; |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 426 | |
| 427 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 428 | struct NodeInfo { |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 429 | NodeInfo() |
| 430 | : being_analyzed(false), |
| 431 | been_analyzed(false), |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 432 | follows_word_interest(false), |
| 433 | follows_newline_interest(false), |
| 434 | follows_start_interest(false), |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 435 | at_end(false), |
danno@chromium.org | 1044a4d | 2012-04-30 12:34:39 +0000 | [diff] [blame] | 436 | visited(false), |
| 437 | replacement_calculated(false) { } |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 438 | |
| 439 | // Returns true if the interests and assumptions of this node |
| 440 | // matches the given one. |
| 441 | bool Matches(NodeInfo* that) { |
| 442 | return (at_end == that->at_end) && |
| 443 | (follows_word_interest == that->follows_word_interest) && |
| 444 | (follows_newline_interest == that->follows_newline_interest) && |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 445 | (follows_start_interest == that->follows_start_interest); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | // Updates the interests of this node given the interests of the |
| 449 | // node preceding it. |
| 450 | void AddFromPreceding(NodeInfo* that) { |
| 451 | at_end |= that->at_end; |
| 452 | follows_word_interest |= that->follows_word_interest; |
| 453 | follows_newline_interest |= that->follows_newline_interest; |
| 454 | follows_start_interest |= that->follows_start_interest; |
| 455 | } |
| 456 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 457 | bool HasLookbehind() { |
| 458 | return follows_word_interest || |
| 459 | follows_newline_interest || |
| 460 | follows_start_interest; |
| 461 | } |
| 462 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 463 | // Sets the interests of this node to include the interests of the |
| 464 | // following node. |
| 465 | void AddFromFollowing(NodeInfo* that) { |
| 466 | follows_word_interest |= that->follows_word_interest; |
| 467 | follows_newline_interest |= that->follows_newline_interest; |
| 468 | follows_start_interest |= that->follows_start_interest; |
| 469 | } |
| 470 | |
| 471 | void ResetCompilationState() { |
| 472 | being_analyzed = false; |
| 473 | been_analyzed = false; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | bool being_analyzed: 1; |
| 477 | bool been_analyzed: 1; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 478 | |
| 479 | // These bits are set of this node has to know what the preceding |
| 480 | // character was. |
| 481 | bool follows_word_interest: 1; |
| 482 | bool follows_newline_interest: 1; |
| 483 | bool follows_start_interest: 1; |
| 484 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 485 | bool at_end: 1; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 486 | bool visited: 1; |
danno@chromium.org | 1044a4d | 2012-04-30 12:34:39 +0000 | [diff] [blame] | 487 | bool replacement_calculated: 1; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 488 | }; |
| 489 | |
| 490 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 491 | // Details of a quick mask-compare check that can look ahead in the |
| 492 | // input stream. |
| 493 | class QuickCheckDetails { |
| 494 | public: |
| 495 | QuickCheckDetails() |
| 496 | : characters_(0), |
| 497 | mask_(0), |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 498 | value_(0), |
| 499 | cannot_match_(false) { } |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 500 | explicit QuickCheckDetails(int characters) |
| 501 | : characters_(characters), |
| 502 | mask_(0), |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 503 | value_(0), |
| 504 | cannot_match_(false) { } |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 505 | bool Rationalize(bool ascii); |
| 506 | // Merge in the information from another branch of an alternation. |
| 507 | void Merge(QuickCheckDetails* other, int from_index); |
| 508 | // Advance the current position by some amount. |
| 509 | void Advance(int by, bool ascii); |
| 510 | void Clear(); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 511 | bool cannot_match() { return cannot_match_; } |
| 512 | void set_cannot_match() { cannot_match_ = true; } |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 513 | struct Position { |
| 514 | Position() : mask(0), value(0), determines_perfectly(false) { } |
| 515 | uc16 mask; |
| 516 | uc16 value; |
| 517 | bool determines_perfectly; |
| 518 | }; |
| 519 | int characters() { return characters_; } |
| 520 | void set_characters(int characters) { characters_ = characters; } |
| 521 | Position* positions(int index) { |
| 522 | ASSERT(index >= 0); |
| 523 | ASSERT(index < characters_); |
| 524 | return positions_ + index; |
| 525 | } |
| 526 | uint32_t mask() { return mask_; } |
| 527 | uint32_t value() { return value_; } |
| 528 | |
| 529 | private: |
| 530 | // How many characters do we have quick check information from. This is |
| 531 | // the same for all branches of a choice node. |
| 532 | int characters_; |
| 533 | Position positions_[4]; |
| 534 | // These values are the condensate of the above array after Rationalize(). |
| 535 | uint32_t mask_; |
| 536 | uint32_t value_; |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 537 | // If set to true, there is no way this quick check can match at all. |
| 538 | // E.g., if it requires to be at the start of the input, and isn't. |
| 539 | bool cannot_match_; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 540 | }; |
| 541 | |
| 542 | |
danno@chromium.org | 1044a4d | 2012-04-30 12:34:39 +0000 | [diff] [blame] | 543 | extern int kUninitializedRegExpNodePlaceHolder; |
| 544 | |
| 545 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 546 | class RegExpNode: public ZoneObject { |
| 547 | public: |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 548 | explicit RegExpNode(Zone* zone) |
| 549 | : replacement_(NULL), trace_count_(0), zone_(zone) { |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 550 | bm_info_[0] = bm_info_[1] = NULL; |
| 551 | } |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 552 | virtual ~RegExpNode(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 553 | virtual void Accept(NodeVisitor* visitor) = 0; |
| 554 | // Generates a goto to this node or actually generates the code at this point. |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 555 | virtual void Emit(RegExpCompiler* compiler, Trace* trace) = 0; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 556 | // How many characters must this node consume at a minimum in order to |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 557 | // succeed. If we have found at least 'still_to_find' characters that |
| 558 | // must be consumed there is no need to ask any following nodes whether |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 559 | // they are sure to eat any more characters. The not_at_start argument is |
| 560 | // used to indicate that we know we are not at the start of the input. In |
| 561 | // this case anchored branches will always fail and can be ignored when |
| 562 | // determining how many characters are consumed on success. |
| 563 | virtual int EatsAtLeast(int still_to_find, |
| 564 | int recursion_depth, |
| 565 | bool not_at_start) = 0; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 566 | // Emits some quick code that checks whether the preloaded characters match. |
| 567 | // Falls through on certain failure, jumps to the label on possible success. |
| 568 | // If the node cannot make a quick check it does nothing and returns false. |
| 569 | bool EmitQuickCheck(RegExpCompiler* compiler, |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 570 | Trace* trace, |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 571 | bool preload_has_checked_bounds, |
| 572 | Label* on_possible_success, |
| 573 | QuickCheckDetails* details_return, |
| 574 | bool fall_through_on_failure); |
| 575 | // For a given number of characters this returns a mask and a value. The |
| 576 | // next n characters are anded with the mask and compared with the value. |
| 577 | // A comparison failure indicates the node cannot match the next n characters. |
| 578 | // A comparison success indicates the node may match. |
| 579 | virtual void GetQuickCheckDetails(QuickCheckDetails* details, |
| 580 | RegExpCompiler* compiler, |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 581 | int characters_filled_in, |
| 582 | bool not_at_start) = 0; |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 583 | static const int kNodeIsTooComplexForGreedyLoops = -1; |
| 584 | virtual int GreedyLoopTextLength() { return kNodeIsTooComplexForGreedyLoops; } |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 585 | // Only returns the successor for a text node of length 1 that matches any |
| 586 | // character and that has no guards on it. |
| 587 | virtual RegExpNode* GetSuccessorOfOmnivorousTextNode( |
| 588 | RegExpCompiler* compiler) { |
| 589 | return NULL; |
| 590 | } |
| 591 | |
| 592 | // Collects information on the possible code units (mod 128) that can match if |
| 593 | // we look forward. This is used for a Boyer-Moore-like string searching |
| 594 | // implementation. TODO(erikcorry): This should share more code with |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 595 | // EatsAtLeast, GetQuickCheckDetails. The budget argument is used to limit |
| 596 | // the number of nodes we are willing to look at in order to create this data. |
| 597 | static const int kFillInBMBudget = 200; |
verwaest@chromium.org | 3714139 | 2012-05-31 13:27:02 +0000 | [diff] [blame] | 598 | virtual void FillInBMInfo(int offset, |
| 599 | int recursion_depth, |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 600 | int budget, |
verwaest@chromium.org | 3714139 | 2012-05-31 13:27:02 +0000 | [diff] [blame] | 601 | BoyerMooreLookahead* bm, |
| 602 | bool not_at_start) { |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 603 | UNREACHABLE(); |
| 604 | } |
danno@chromium.org | 1044a4d | 2012-04-30 12:34:39 +0000 | [diff] [blame] | 605 | |
| 606 | // If we know that the input is ASCII then there are some nodes that can |
| 607 | // never match. This method returns a node that can be substituted for |
| 608 | // itself, or NULL if the node can never match. |
| 609 | virtual RegExpNode* FilterASCII(int depth) { return this; } |
| 610 | // Helper for FilterASCII. |
| 611 | RegExpNode* replacement() { |
| 612 | ASSERT(info()->replacement_calculated); |
| 613 | return replacement_; |
| 614 | } |
| 615 | RegExpNode* set_replacement(RegExpNode* replacement) { |
| 616 | info()->replacement_calculated = true; |
| 617 | replacement_ = replacement; |
| 618 | return replacement; // For convenience. |
| 619 | } |
| 620 | |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 621 | // We want to avoid recalculating the lookahead info, so we store it on the |
| 622 | // node. Only info that is for this node is stored. We can tell that the |
| 623 | // info is for this node when offset == 0, so the information is calculated |
| 624 | // relative to this node. |
| 625 | void SaveBMInfo(BoyerMooreLookahead* bm, bool not_at_start, int offset) { |
| 626 | if (offset == 0) set_bm_info(not_at_start, bm); |
| 627 | } |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 628 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 629 | Label* label() { return &label_; } |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 630 | // If non-generic code is generated for a node (i.e. the node is not at the |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 631 | // start of the trace) then it cannot be reused. This variable sets a limit |
| 632 | // on how often we allow that to happen before we insist on starting a new |
| 633 | // trace and generating generic code for a node that can be reused by flushing |
| 634 | // the deferred actions in the current trace and generating a goto. |
| 635 | static const int kMaxCopiesCodeGenerated = 10; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 636 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 637 | NodeInfo* info() { return &info_; } |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 638 | |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 639 | BoyerMooreLookahead* bm_info(bool not_at_start) { |
| 640 | return bm_info_[not_at_start ? 1 : 0]; |
| 641 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 642 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 643 | Zone* zone() const { return zone_; } |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 644 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 645 | protected: |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 646 | enum LimitResult { DONE, CONTINUE }; |
danno@chromium.org | 1044a4d | 2012-04-30 12:34:39 +0000 | [diff] [blame] | 647 | RegExpNode* replacement_; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 648 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 649 | LimitResult LimitVersions(RegExpCompiler* compiler, Trace* trace); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 650 | |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 651 | void set_bm_info(bool not_at_start, BoyerMooreLookahead* bm) { |
| 652 | bm_info_[not_at_start ? 1 : 0] = bm; |
| 653 | } |
| 654 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 655 | private: |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 656 | static const int kFirstCharBudget = 10; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 657 | Label label_; |
| 658 | NodeInfo info_; |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 659 | // This variable keeps track of how many times code has been generated for |
| 660 | // this node (in different traces). We don't keep track of where the |
| 661 | // generated code is located unless the code is generated at the start of |
| 662 | // a trace, in which case it is generic and can be reused by flushing the |
| 663 | // deferred operations in the current trace and generating a goto. |
| 664 | int trace_count_; |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 665 | BoyerMooreLookahead* bm_info_[2]; |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 666 | |
| 667 | Zone* zone_; |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 668 | }; |
| 669 | |
| 670 | |
| 671 | // A simple closed interval. |
| 672 | class Interval { |
| 673 | public: |
| 674 | Interval() : from_(kNone), to_(kNone) { } |
| 675 | Interval(int from, int to) : from_(from), to_(to) { } |
| 676 | Interval Union(Interval that) { |
| 677 | if (that.from_ == kNone) |
| 678 | return *this; |
| 679 | else if (from_ == kNone) |
| 680 | return that; |
| 681 | else |
| 682 | return Interval(Min(from_, that.from_), Max(to_, that.to_)); |
| 683 | } |
| 684 | bool Contains(int value) { |
| 685 | return (from_ <= value) && (value <= to_); |
| 686 | } |
| 687 | bool is_empty() { return from_ == kNone; } |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 688 | int from() const { return from_; } |
| 689 | int to() const { return to_; } |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 690 | static Interval Empty() { return Interval(); } |
| 691 | static const int kNone = -1; |
| 692 | private: |
| 693 | int from_; |
| 694 | int to_; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 695 | }; |
| 696 | |
| 697 | |
| 698 | class SeqRegExpNode: public RegExpNode { |
| 699 | public: |
| 700 | explicit SeqRegExpNode(RegExpNode* on_success) |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 701 | : RegExpNode(on_success->zone()), on_success_(on_success) { } |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 702 | RegExpNode* on_success() { return on_success_; } |
| 703 | void set_on_success(RegExpNode* node) { on_success_ = node; } |
danno@chromium.org | 1044a4d | 2012-04-30 12:34:39 +0000 | [diff] [blame] | 704 | virtual RegExpNode* FilterASCII(int depth); |
verwaest@chromium.org | 3714139 | 2012-05-31 13:27:02 +0000 | [diff] [blame] | 705 | virtual void FillInBMInfo(int offset, |
| 706 | int recursion_depth, |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 707 | int budget, |
verwaest@chromium.org | 3714139 | 2012-05-31 13:27:02 +0000 | [diff] [blame] | 708 | BoyerMooreLookahead* bm, |
| 709 | bool not_at_start) { |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 710 | on_success_->FillInBMInfo( |
| 711 | offset, recursion_depth + 1, budget - 1, bm, not_at_start); |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 712 | if (offset == 0) set_bm_info(not_at_start, bm); |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 713 | } |
danno@chromium.org | 1044a4d | 2012-04-30 12:34:39 +0000 | [diff] [blame] | 714 | |
| 715 | protected: |
| 716 | RegExpNode* FilterSuccessor(int depth); |
| 717 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 718 | private: |
| 719 | RegExpNode* on_success_; |
| 720 | }; |
| 721 | |
| 722 | |
| 723 | class ActionNode: public SeqRegExpNode { |
| 724 | public: |
| 725 | enum Type { |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 726 | SET_REGISTER, |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 727 | INCREMENT_REGISTER, |
| 728 | STORE_POSITION, |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 729 | BEGIN_SUBMATCH, |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 730 | POSITIVE_SUBMATCH_SUCCESS, |
| 731 | EMPTY_MATCH_CHECK, |
| 732 | CLEAR_CAPTURES |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 733 | }; |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 734 | static ActionNode* SetRegister(int reg, int val, RegExpNode* on_success); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 735 | static ActionNode* IncrementRegister(int reg, RegExpNode* on_success); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 736 | static ActionNode* StorePosition(int reg, |
| 737 | bool is_capture, |
| 738 | RegExpNode* on_success); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 739 | static ActionNode* ClearCaptures(Interval range, RegExpNode* on_success); |
| 740 | static ActionNode* BeginSubmatch(int stack_pointer_reg, |
| 741 | int position_reg, |
| 742 | RegExpNode* on_success); |
| 743 | static ActionNode* PositiveSubmatchSuccess(int stack_pointer_reg, |
| 744 | int restore_reg, |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 745 | int clear_capture_count, |
| 746 | int clear_capture_from, |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 747 | RegExpNode* on_success); |
| 748 | static ActionNode* EmptyMatchCheck(int start_register, |
| 749 | int repetition_register, |
| 750 | int repetition_limit, |
| 751 | RegExpNode* on_success); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 752 | virtual void Accept(NodeVisitor* visitor); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 753 | virtual void Emit(RegExpCompiler* compiler, Trace* trace); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 754 | virtual int EatsAtLeast(int still_to_find, |
| 755 | int recursion_depth, |
| 756 | bool not_at_start); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 757 | virtual void GetQuickCheckDetails(QuickCheckDetails* details, |
| 758 | RegExpCompiler* compiler, |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 759 | int filled_in, |
| 760 | bool not_at_start) { |
| 761 | return on_success()->GetQuickCheckDetails( |
| 762 | details, compiler, filled_in, not_at_start); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 763 | } |
verwaest@chromium.org | 3714139 | 2012-05-31 13:27:02 +0000 | [diff] [blame] | 764 | virtual void FillInBMInfo(int offset, |
| 765 | int recursion_depth, |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 766 | int budget, |
verwaest@chromium.org | 3714139 | 2012-05-31 13:27:02 +0000 | [diff] [blame] | 767 | BoyerMooreLookahead* bm, |
| 768 | bool not_at_start); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 769 | Type type() { return type_; } |
| 770 | // TODO(erikcorry): We should allow some action nodes in greedy loops. |
| 771 | virtual int GreedyLoopTextLength() { return kNodeIsTooComplexForGreedyLoops; } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 772 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 773 | private: |
| 774 | union { |
| 775 | struct { |
| 776 | int reg; |
| 777 | int value; |
| 778 | } u_store_register; |
| 779 | struct { |
| 780 | int reg; |
| 781 | } u_increment_register; |
| 782 | struct { |
| 783 | int reg; |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 784 | bool is_capture; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 785 | } u_position_register; |
| 786 | struct { |
| 787 | int stack_pointer_register; |
| 788 | int current_position_register; |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 789 | int clear_register_count; |
| 790 | int clear_register_from; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 791 | } u_submatch; |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 792 | struct { |
| 793 | int start_register; |
| 794 | int repetition_register; |
| 795 | int repetition_limit; |
| 796 | } u_empty_match_check; |
| 797 | struct { |
| 798 | int range_from; |
| 799 | int range_to; |
| 800 | } u_clear_captures; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 801 | } data_; |
| 802 | ActionNode(Type type, RegExpNode* on_success) |
| 803 | : SeqRegExpNode(on_success), |
| 804 | type_(type) { } |
| 805 | Type type_; |
| 806 | friend class DotPrinter; |
| 807 | }; |
| 808 | |
| 809 | |
| 810 | class TextNode: public SeqRegExpNode { |
| 811 | public: |
| 812 | TextNode(ZoneList<TextElement>* elms, |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 813 | RegExpNode* on_success) |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 814 | : SeqRegExpNode(on_success), |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 815 | elms_(elms) { } |
| 816 | TextNode(RegExpCharacterClass* that, |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 817 | RegExpNode* on_success) |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 818 | : SeqRegExpNode(on_success), |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 819 | elms_(new(zone()) ZoneList<TextElement>(1, zone())) { |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 820 | elms_->Add(TextElement::CharClass(that), zone()); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 821 | } |
| 822 | virtual void Accept(NodeVisitor* visitor); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 823 | virtual void Emit(RegExpCompiler* compiler, Trace* trace); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 824 | virtual int EatsAtLeast(int still_to_find, |
| 825 | int recursion_depth, |
| 826 | bool not_at_start); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 827 | virtual void GetQuickCheckDetails(QuickCheckDetails* details, |
| 828 | RegExpCompiler* compiler, |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 829 | int characters_filled_in, |
| 830 | bool not_at_start); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 831 | ZoneList<TextElement>* elements() { return elms_; } |
ager@chromium.org | 38e4c71 | 2009-11-11 09:11:58 +0000 | [diff] [blame] | 832 | void MakeCaseIndependent(bool is_ascii); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 833 | virtual int GreedyLoopTextLength(); |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 834 | virtual RegExpNode* GetSuccessorOfOmnivorousTextNode( |
| 835 | RegExpCompiler* compiler); |
verwaest@chromium.org | 3714139 | 2012-05-31 13:27:02 +0000 | [diff] [blame] | 836 | virtual void FillInBMInfo(int offset, |
| 837 | int recursion_depth, |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 838 | int budget, |
verwaest@chromium.org | 3714139 | 2012-05-31 13:27:02 +0000 | [diff] [blame] | 839 | BoyerMooreLookahead* bm, |
| 840 | bool not_at_start); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 841 | void CalculateOffsets(); |
danno@chromium.org | 1044a4d | 2012-04-30 12:34:39 +0000 | [diff] [blame] | 842 | virtual RegExpNode* FilterASCII(int depth); |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 843 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 844 | private: |
| 845 | enum TextEmitPassType { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 846 | NON_ASCII_MATCH, // Check for characters that can't match. |
| 847 | SIMPLE_CHARACTER_MATCH, // Case-dependent single character check. |
| 848 | NON_LETTER_CHARACTER_MATCH, // Check characters that have no case equivs. |
| 849 | CASE_CHARACTER_MATCH, // Case-independent single character check. |
| 850 | CHARACTER_CLASS_MATCH // Character class. |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 851 | }; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 852 | static bool SkipPass(int pass, bool ignore_case); |
| 853 | static const int kFirstRealPass = SIMPLE_CHARACTER_MATCH; |
| 854 | static const int kLastPass = CHARACTER_CLASS_MATCH; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 855 | void TextEmitPass(RegExpCompiler* compiler, |
| 856 | TextEmitPassType pass, |
| 857 | bool preloaded, |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 858 | Trace* trace, |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 859 | bool first_element_checked, |
| 860 | int* checked_up_to); |
| 861 | int Length(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 862 | ZoneList<TextElement>* elms_; |
| 863 | }; |
| 864 | |
| 865 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 866 | class AssertionNode: public SeqRegExpNode { |
| 867 | public: |
| 868 | enum AssertionNodeType { |
| 869 | AT_END, |
| 870 | AT_START, |
| 871 | AT_BOUNDARY, |
| 872 | AT_NON_BOUNDARY, |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 873 | AFTER_NEWLINE |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 874 | }; |
| 875 | static AssertionNode* AtEnd(RegExpNode* on_success) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 876 | return new(on_success->zone()) AssertionNode(AT_END, on_success); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 877 | } |
| 878 | static AssertionNode* AtStart(RegExpNode* on_success) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 879 | return new(on_success->zone()) AssertionNode(AT_START, on_success); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 880 | } |
| 881 | static AssertionNode* AtBoundary(RegExpNode* on_success) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 882 | return new(on_success->zone()) AssertionNode(AT_BOUNDARY, on_success); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 883 | } |
| 884 | static AssertionNode* AtNonBoundary(RegExpNode* on_success) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 885 | return new(on_success->zone()) AssertionNode(AT_NON_BOUNDARY, on_success); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 886 | } |
| 887 | static AssertionNode* AfterNewline(RegExpNode* on_success) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 888 | return new(on_success->zone()) AssertionNode(AFTER_NEWLINE, on_success); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 889 | } |
| 890 | virtual void Accept(NodeVisitor* visitor); |
| 891 | virtual void Emit(RegExpCompiler* compiler, Trace* trace); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 892 | virtual int EatsAtLeast(int still_to_find, |
| 893 | int recursion_depth, |
| 894 | bool not_at_start); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 895 | virtual void GetQuickCheckDetails(QuickCheckDetails* details, |
| 896 | RegExpCompiler* compiler, |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 897 | int filled_in, |
| 898 | bool not_at_start); |
verwaest@chromium.org | 3714139 | 2012-05-31 13:27:02 +0000 | [diff] [blame] | 899 | virtual void FillInBMInfo(int offset, |
| 900 | int recursion_depth, |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 901 | int budget, |
verwaest@chromium.org | 3714139 | 2012-05-31 13:27:02 +0000 | [diff] [blame] | 902 | BoyerMooreLookahead* bm, |
| 903 | bool not_at_start); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 904 | AssertionNodeType type() { return type_; } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 905 | void set_type(AssertionNodeType type) { type_ = type; } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 906 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 907 | private: |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 908 | void EmitBoundaryCheck(RegExpCompiler* compiler, Trace* trace); |
| 909 | enum IfPrevious { kIsNonWord, kIsWord }; |
| 910 | void BacktrackIfPrevious(RegExpCompiler* compiler, |
| 911 | Trace* trace, |
| 912 | IfPrevious backtrack_if_previous); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 913 | AssertionNode(AssertionNodeType t, RegExpNode* on_success) |
| 914 | : SeqRegExpNode(on_success), type_(t) { } |
| 915 | AssertionNodeType type_; |
| 916 | }; |
| 917 | |
| 918 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 919 | class BackReferenceNode: public SeqRegExpNode { |
| 920 | public: |
| 921 | BackReferenceNode(int start_reg, |
| 922 | int end_reg, |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 923 | RegExpNode* on_success) |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 924 | : SeqRegExpNode(on_success), |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 925 | start_reg_(start_reg), |
| 926 | end_reg_(end_reg) { } |
| 927 | virtual void Accept(NodeVisitor* visitor); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 928 | int start_register() { return start_reg_; } |
| 929 | int end_register() { return end_reg_; } |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 930 | virtual void Emit(RegExpCompiler* compiler, Trace* trace); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 931 | virtual int EatsAtLeast(int still_to_find, |
| 932 | int recursion_depth, |
| 933 | bool not_at_start); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 934 | virtual void GetQuickCheckDetails(QuickCheckDetails* details, |
| 935 | RegExpCompiler* compiler, |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 936 | int characters_filled_in, |
| 937 | bool not_at_start) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 938 | return; |
| 939 | } |
verwaest@chromium.org | 3714139 | 2012-05-31 13:27:02 +0000 | [diff] [blame] | 940 | virtual void FillInBMInfo(int offset, |
| 941 | int recursion_depth, |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 942 | int budget, |
verwaest@chromium.org | 3714139 | 2012-05-31 13:27:02 +0000 | [diff] [blame] | 943 | BoyerMooreLookahead* bm, |
| 944 | bool not_at_start); |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 945 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 946 | private: |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 947 | int start_reg_; |
| 948 | int end_reg_; |
| 949 | }; |
| 950 | |
| 951 | |
| 952 | class EndNode: public RegExpNode { |
| 953 | public: |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 954 | enum Action { ACCEPT, BACKTRACK, NEGATIVE_SUBMATCH_SUCCESS }; |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 955 | explicit EndNode(Action action, Zone* zone) |
| 956 | : RegExpNode(zone), action_(action) { } |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 957 | virtual void Accept(NodeVisitor* visitor); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 958 | virtual void Emit(RegExpCompiler* compiler, Trace* trace); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 959 | virtual int EatsAtLeast(int still_to_find, |
| 960 | int recursion_depth, |
| 961 | bool not_at_start) { return 0; } |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 962 | virtual void GetQuickCheckDetails(QuickCheckDetails* details, |
| 963 | RegExpCompiler* compiler, |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 964 | int characters_filled_in, |
| 965 | bool not_at_start) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 966 | // Returning 0 from EatsAtLeast should ensure we never get here. |
| 967 | UNREACHABLE(); |
| 968 | } |
verwaest@chromium.org | 3714139 | 2012-05-31 13:27:02 +0000 | [diff] [blame] | 969 | virtual void FillInBMInfo(int offset, |
| 970 | int recursion_depth, |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 971 | int budget, |
verwaest@chromium.org | 3714139 | 2012-05-31 13:27:02 +0000 | [diff] [blame] | 972 | BoyerMooreLookahead* bm, |
| 973 | bool not_at_start) { |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 974 | // Returning 0 from EatsAtLeast should ensure we never get here. |
| 975 | UNREACHABLE(); |
| 976 | } |
danno@chromium.org | 1044a4d | 2012-04-30 12:34:39 +0000 | [diff] [blame] | 977 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 978 | private: |
| 979 | Action action_; |
| 980 | }; |
| 981 | |
| 982 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 983 | class NegativeSubmatchSuccess: public EndNode { |
| 984 | public: |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 985 | NegativeSubmatchSuccess(int stack_pointer_reg, |
| 986 | int position_reg, |
| 987 | int clear_capture_count, |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 988 | int clear_capture_start, |
| 989 | Zone* zone) |
| 990 | : EndNode(NEGATIVE_SUBMATCH_SUCCESS, zone), |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 991 | stack_pointer_register_(stack_pointer_reg), |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 992 | current_position_register_(position_reg), |
| 993 | clear_capture_count_(clear_capture_count), |
| 994 | clear_capture_start_(clear_capture_start) { } |
| 995 | virtual void Emit(RegExpCompiler* compiler, Trace* trace); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 996 | |
| 997 | private: |
| 998 | int stack_pointer_register_; |
| 999 | int current_position_register_; |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1000 | int clear_capture_count_; |
| 1001 | int clear_capture_start_; |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1002 | }; |
| 1003 | |
| 1004 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1005 | class Guard: public ZoneObject { |
| 1006 | public: |
| 1007 | enum Relation { LT, GEQ }; |
| 1008 | Guard(int reg, Relation op, int value) |
| 1009 | : reg_(reg), |
| 1010 | op_(op), |
| 1011 | value_(value) { } |
| 1012 | int reg() { return reg_; } |
| 1013 | Relation op() { return op_; } |
| 1014 | int value() { return value_; } |
| 1015 | |
| 1016 | private: |
| 1017 | int reg_; |
| 1018 | Relation op_; |
| 1019 | int value_; |
| 1020 | }; |
| 1021 | |
| 1022 | |
| 1023 | class GuardedAlternative { |
| 1024 | public: |
| 1025 | explicit GuardedAlternative(RegExpNode* node) : node_(node), guards_(NULL) { } |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1026 | void AddGuard(Guard* guard, Zone* zone); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1027 | RegExpNode* node() { return node_; } |
| 1028 | void set_node(RegExpNode* node) { node_ = node; } |
| 1029 | ZoneList<Guard*>* guards() { return guards_; } |
| 1030 | |
| 1031 | private: |
| 1032 | RegExpNode* node_; |
| 1033 | ZoneList<Guard*>* guards_; |
| 1034 | }; |
| 1035 | |
| 1036 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1037 | class AlternativeGeneration; |
| 1038 | |
| 1039 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1040 | class ChoiceNode: public RegExpNode { |
| 1041 | public: |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 1042 | explicit ChoiceNode(int expected_size, Zone* zone) |
| 1043 | : RegExpNode(zone), |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1044 | alternatives_(new(zone) |
| 1045 | ZoneList<GuardedAlternative>(expected_size, zone)), |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1046 | table_(NULL), |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1047 | not_at_start_(false), |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1048 | being_calculated_(false) { } |
| 1049 | virtual void Accept(NodeVisitor* visitor); |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 1050 | void AddAlternative(GuardedAlternative node) { |
| 1051 | alternatives()->Add(node, zone()); |
| 1052 | } |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1053 | ZoneList<GuardedAlternative>* alternatives() { return alternatives_; } |
| 1054 | DispatchTable* GetTable(bool ignore_case); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1055 | virtual void Emit(RegExpCompiler* compiler, Trace* trace); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1056 | virtual int EatsAtLeast(int still_to_find, |
| 1057 | int recursion_depth, |
| 1058 | bool not_at_start); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1059 | int EatsAtLeastHelper(int still_to_find, |
| 1060 | int recursion_depth, |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1061 | RegExpNode* ignore_this_node, |
| 1062 | bool not_at_start); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1063 | virtual void GetQuickCheckDetails(QuickCheckDetails* details, |
| 1064 | RegExpCompiler* compiler, |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1065 | int characters_filled_in, |
| 1066 | bool not_at_start); |
verwaest@chromium.org | 3714139 | 2012-05-31 13:27:02 +0000 | [diff] [blame] | 1067 | virtual void FillInBMInfo(int offset, |
| 1068 | int recursion_depth, |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 1069 | int budget, |
verwaest@chromium.org | 3714139 | 2012-05-31 13:27:02 +0000 | [diff] [blame] | 1070 | BoyerMooreLookahead* bm, |
| 1071 | bool not_at_start); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1072 | |
| 1073 | bool being_calculated() { return being_calculated_; } |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1074 | bool not_at_start() { return not_at_start_; } |
| 1075 | void set_not_at_start() { not_at_start_ = true; } |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1076 | void set_being_calculated(bool b) { being_calculated_ = b; } |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1077 | virtual bool try_to_emit_quick_check_for_alternative(int i) { return true; } |
danno@chromium.org | 1044a4d | 2012-04-30 12:34:39 +0000 | [diff] [blame] | 1078 | virtual RegExpNode* FilterASCII(int depth); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1079 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1080 | protected: |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 1081 | int GreedyLoopTextLengthForAlternative(GuardedAlternative* alternative); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1082 | ZoneList<GuardedAlternative>* alternatives_; |
| 1083 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1084 | private: |
| 1085 | friend class DispatchTableConstructor; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1086 | friend class Analysis; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1087 | void GenerateGuard(RegExpMacroAssembler* macro_assembler, |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 1088 | Guard* guard, |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1089 | Trace* trace); |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 1090 | int CalculatePreloadCharacters(RegExpCompiler* compiler, int eats_at_least); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1091 | void EmitOutOfLineContinuation(RegExpCompiler* compiler, |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1092 | Trace* trace, |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1093 | GuardedAlternative alternative, |
| 1094 | AlternativeGeneration* alt_gen, |
| 1095 | int preload_characters, |
| 1096 | bool next_expects_preload); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1097 | DispatchTable* table_; |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1098 | // If true, this node is never checked at the start of the input. |
| 1099 | // Allows a new trace to start with at_start() set to false. |
| 1100 | bool not_at_start_; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1101 | bool being_calculated_; |
| 1102 | }; |
| 1103 | |
| 1104 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1105 | class NegativeLookaheadChoiceNode: public ChoiceNode { |
| 1106 | public: |
| 1107 | explicit NegativeLookaheadChoiceNode(GuardedAlternative this_must_fail, |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 1108 | GuardedAlternative then_do_this, |
| 1109 | Zone* zone) |
| 1110 | : ChoiceNode(2, zone) { |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1111 | AddAlternative(this_must_fail); |
| 1112 | AddAlternative(then_do_this); |
| 1113 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1114 | virtual int EatsAtLeast(int still_to_find, |
| 1115 | int recursion_depth, |
| 1116 | bool not_at_start); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1117 | virtual void GetQuickCheckDetails(QuickCheckDetails* details, |
| 1118 | RegExpCompiler* compiler, |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1119 | int characters_filled_in, |
| 1120 | bool not_at_start); |
verwaest@chromium.org | 3714139 | 2012-05-31 13:27:02 +0000 | [diff] [blame] | 1121 | virtual void FillInBMInfo(int offset, |
| 1122 | int recursion_depth, |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 1123 | int budget, |
verwaest@chromium.org | 3714139 | 2012-05-31 13:27:02 +0000 | [diff] [blame] | 1124 | BoyerMooreLookahead* bm, |
| 1125 | bool not_at_start) { |
| 1126 | alternatives_->at(1).node()->FillInBMInfo( |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 1127 | offset, recursion_depth + 1, budget - 1, bm, not_at_start); |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 1128 | if (offset == 0) set_bm_info(not_at_start, bm); |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 1129 | } |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1130 | // For a negative lookahead we don't emit the quick check for the |
| 1131 | // alternative that is expected to fail. This is because quick check code |
| 1132 | // starts by loading enough characters for the alternative that takes fewest |
| 1133 | // characters, but on a negative lookahead the negative branch did not take |
| 1134 | // part in that calculation (EatsAtLeast) so the assumptions don't hold. |
| 1135 | virtual bool try_to_emit_quick_check_for_alternative(int i) { return i != 0; } |
danno@chromium.org | 1044a4d | 2012-04-30 12:34:39 +0000 | [diff] [blame] | 1136 | virtual RegExpNode* FilterASCII(int depth); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1137 | }; |
| 1138 | |
| 1139 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1140 | class LoopChoiceNode: public ChoiceNode { |
| 1141 | public: |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 1142 | explicit LoopChoiceNode(bool body_can_be_zero_length, Zone* zone) |
| 1143 | : ChoiceNode(2, zone), |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1144 | loop_node_(NULL), |
| 1145 | continue_node_(NULL), |
| 1146 | body_can_be_zero_length_(body_can_be_zero_length) { } |
| 1147 | void AddLoopAlternative(GuardedAlternative alt); |
| 1148 | void AddContinueAlternative(GuardedAlternative alt); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1149 | virtual void Emit(RegExpCompiler* compiler, Trace* trace); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1150 | virtual int EatsAtLeast(int still_to_find, |
| 1151 | int recursion_depth, |
| 1152 | bool not_at_start); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1153 | virtual void GetQuickCheckDetails(QuickCheckDetails* details, |
| 1154 | RegExpCompiler* compiler, |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1155 | int characters_filled_in, |
| 1156 | bool not_at_start); |
verwaest@chromium.org | 3714139 | 2012-05-31 13:27:02 +0000 | [diff] [blame] | 1157 | virtual void FillInBMInfo(int offset, |
| 1158 | int recursion_depth, |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 1159 | int budget, |
verwaest@chromium.org | 3714139 | 2012-05-31 13:27:02 +0000 | [diff] [blame] | 1160 | BoyerMooreLookahead* bm, |
| 1161 | bool not_at_start); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1162 | RegExpNode* loop_node() { return loop_node_; } |
| 1163 | RegExpNode* continue_node() { return continue_node_; } |
| 1164 | bool body_can_be_zero_length() { return body_can_be_zero_length_; } |
| 1165 | virtual void Accept(NodeVisitor* visitor); |
danno@chromium.org | 1044a4d | 2012-04-30 12:34:39 +0000 | [diff] [blame] | 1166 | virtual RegExpNode* FilterASCII(int depth); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1167 | |
| 1168 | private: |
| 1169 | // AddAlternative is made private for loop nodes because alternatives |
| 1170 | // should not be added freely, we need to keep track of which node |
| 1171 | // goes back to the node itself. |
| 1172 | void AddAlternative(GuardedAlternative node) { |
| 1173 | ChoiceNode::AddAlternative(node); |
| 1174 | } |
| 1175 | |
| 1176 | RegExpNode* loop_node_; |
| 1177 | RegExpNode* continue_node_; |
| 1178 | bool body_can_be_zero_length_; |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1179 | }; |
| 1180 | |
| 1181 | |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 1182 | // Improve the speed that we scan for an initial point where a non-anchored |
| 1183 | // regexp can match by using a Boyer-Moore-like table. This is done by |
| 1184 | // identifying non-greedy non-capturing loops in the nodes that eat any |
| 1185 | // character one at a time. For example in the middle of the regexp |
| 1186 | // /foo[\s\S]*?bar/ we find such a loop. There is also such a loop implicitly |
| 1187 | // inserted at the start of any non-anchored regexp. |
| 1188 | // |
| 1189 | // When we have found such a loop we look ahead in the nodes to find the set of |
| 1190 | // characters that can come at given distances. For example for the regexp |
| 1191 | // /.?foo/ we know that there are at least 3 characters ahead of us, and the |
| 1192 | // sets of characters that can occur are [any, [f, o], [o]]. We find a range in |
| 1193 | // the lookahead info where the set of characters is reasonably constrained. In |
| 1194 | // our example this is from index 1 to 2 (0 is not constrained). We can now |
| 1195 | // look 3 characters ahead and if we don't find one of [f, o] (the union of |
| 1196 | // [f, o] and [o]) then we can skip forwards by the range size (in this case 2). |
| 1197 | // |
| 1198 | // For Unicode input strings we do the same, but modulo 128. |
| 1199 | // |
| 1200 | // We also look at the first string fed to the regexp and use that to get a hint |
| 1201 | // of the character frequencies in the inputs. This affects the assessment of |
| 1202 | // whether the set of characters is 'reasonably constrained'. |
| 1203 | // |
| 1204 | // We also have another lookahead mechanism (called quick check in the code), |
| 1205 | // which uses a wide load of multiple characters followed by a mask and compare |
| 1206 | // to determine whether a match is possible at this point. |
| 1207 | enum ContainedInLattice { |
| 1208 | kNotYet = 0, |
| 1209 | kLatticeIn = 1, |
| 1210 | kLatticeOut = 2, |
| 1211 | kLatticeUnknown = 3 // Can also mean both in and out. |
| 1212 | }; |
| 1213 | |
| 1214 | |
| 1215 | inline ContainedInLattice Combine(ContainedInLattice a, ContainedInLattice b) { |
| 1216 | return static_cast<ContainedInLattice>(a | b); |
| 1217 | } |
| 1218 | |
| 1219 | |
| 1220 | ContainedInLattice AddRange(ContainedInLattice a, |
| 1221 | const int* ranges, |
| 1222 | int ranges_size, |
| 1223 | Interval new_range); |
| 1224 | |
| 1225 | |
| 1226 | class BoyerMoorePositionInfo : public ZoneObject { |
| 1227 | public: |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 1228 | explicit BoyerMoorePositionInfo(Zone* zone) |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1229 | : map_(new(zone) ZoneList<bool>(kMapSize, zone)), |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 1230 | map_count_(0), |
| 1231 | w_(kNotYet), |
| 1232 | s_(kNotYet), |
| 1233 | d_(kNotYet), |
| 1234 | surrogate_(kNotYet) { |
| 1235 | for (int i = 0; i < kMapSize; i++) { |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 1236 | map_->Add(false, zone); |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 1237 | } |
| 1238 | } |
| 1239 | |
| 1240 | bool& at(int i) { return map_->at(i); } |
| 1241 | |
| 1242 | static const int kMapSize = 128; |
| 1243 | static const int kMask = kMapSize - 1; |
| 1244 | |
| 1245 | int map_count() const { return map_count_; } |
| 1246 | |
| 1247 | void Set(int character); |
| 1248 | void SetInterval(const Interval& interval); |
| 1249 | void SetAll(); |
| 1250 | bool is_non_word() { return w_ == kLatticeOut; } |
| 1251 | bool is_word() { return w_ == kLatticeIn; } |
| 1252 | |
| 1253 | private: |
| 1254 | ZoneList<bool>* map_; |
| 1255 | int map_count_; // Number of set bits in the map. |
| 1256 | ContainedInLattice w_; // The \w character class. |
| 1257 | ContainedInLattice s_; // The \s character class. |
| 1258 | ContainedInLattice d_; // The \d character class. |
| 1259 | ContainedInLattice surrogate_; // Surrogate UTF-16 code units. |
| 1260 | }; |
| 1261 | |
| 1262 | |
| 1263 | class BoyerMooreLookahead : public ZoneObject { |
| 1264 | public: |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 1265 | BoyerMooreLookahead(int length, RegExpCompiler* compiler, Zone* zone); |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 1266 | |
| 1267 | int length() { return length_; } |
| 1268 | int max_char() { return max_char_; } |
| 1269 | RegExpCompiler* compiler() { return compiler_; } |
| 1270 | |
| 1271 | int Count(int map_number) { |
| 1272 | return bitmaps_->at(map_number)->map_count(); |
| 1273 | } |
| 1274 | |
| 1275 | BoyerMoorePositionInfo* at(int i) { return bitmaps_->at(i); } |
| 1276 | |
| 1277 | void Set(int map_number, int character) { |
| 1278 | if (character > max_char_) return; |
| 1279 | BoyerMoorePositionInfo* info = bitmaps_->at(map_number); |
| 1280 | info->Set(character); |
| 1281 | } |
| 1282 | |
| 1283 | void SetInterval(int map_number, const Interval& interval) { |
| 1284 | if (interval.from() > max_char_) return; |
| 1285 | BoyerMoorePositionInfo* info = bitmaps_->at(map_number); |
| 1286 | if (interval.to() > max_char_) { |
| 1287 | info->SetInterval(Interval(interval.from(), max_char_)); |
| 1288 | } else { |
| 1289 | info->SetInterval(interval); |
| 1290 | } |
| 1291 | } |
| 1292 | |
| 1293 | void SetAll(int map_number) { |
| 1294 | bitmaps_->at(map_number)->SetAll(); |
| 1295 | } |
| 1296 | |
| 1297 | void SetRest(int from_map) { |
| 1298 | for (int i = from_map; i < length_; i++) SetAll(i); |
| 1299 | } |
| 1300 | bool EmitSkipInstructions(RegExpMacroAssembler* masm); |
| 1301 | |
| 1302 | private: |
| 1303 | // This is the value obtained by EatsAtLeast. If we do not have at least this |
| 1304 | // many characters left in the sample string then the match is bound to fail. |
| 1305 | // Therefore it is OK to read a character this far ahead of the current match |
| 1306 | // point. |
| 1307 | int length_; |
| 1308 | RegExpCompiler* compiler_; |
| 1309 | // 0x7f for ASCII, 0xffff for UTF-16. |
| 1310 | int max_char_; |
| 1311 | ZoneList<BoyerMoorePositionInfo*>* bitmaps_; |
| 1312 | |
| 1313 | int GetSkipTable(int min_lookahead, |
| 1314 | int max_lookahead, |
| 1315 | Handle<ByteArray> boolean_skip_table); |
| 1316 | bool FindWorthwhileInterval(int* from, int* to); |
| 1317 | int FindBestInterval( |
| 1318 | int max_number_of_chars, int old_biggest_points, int* from, int* to); |
| 1319 | }; |
| 1320 | |
| 1321 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1322 | // There are many ways to generate code for a node. This class encapsulates |
| 1323 | // the current way we should be generating. In other words it encapsulates |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1324 | // the current state of the code generator. The effect of this is that we |
| 1325 | // generate code for paths that the matcher can take through the regular |
| 1326 | // expression. A given node in the regexp can be code-generated several times |
| 1327 | // as it can be part of several traces. For example for the regexp: |
| 1328 | // /foo(bar|ip)baz/ the code to match baz will be generated twice, once as part |
| 1329 | // of the foo-bar-baz trace and once as part of the foo-ip-baz trace. The code |
| 1330 | // to match foo is generated only once (the traces have a common prefix). The |
| 1331 | // code to store the capture is deferred and generated (twice) after the places |
| 1332 | // where baz has been matched. |
| 1333 | class Trace { |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1334 | public: |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1335 | // A value for a property that is either known to be true, know to be false, |
| 1336 | // or not known. |
| 1337 | enum TriBool { |
| 1338 | UNKNOWN = -1, FALSE = 0, TRUE = 1 |
| 1339 | }; |
| 1340 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1341 | class DeferredAction { |
| 1342 | public: |
| 1343 | DeferredAction(ActionNode::Type type, int reg) |
| 1344 | : type_(type), reg_(reg), next_(NULL) { } |
| 1345 | DeferredAction* next() { return next_; } |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1346 | bool Mentions(int reg); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1347 | int reg() { return reg_; } |
| 1348 | ActionNode::Type type() { return type_; } |
| 1349 | private: |
| 1350 | ActionNode::Type type_; |
| 1351 | int reg_; |
| 1352 | DeferredAction* next_; |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1353 | friend class Trace; |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1354 | }; |
| 1355 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1356 | class DeferredCapture : public DeferredAction { |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1357 | public: |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1358 | DeferredCapture(int reg, bool is_capture, Trace* trace) |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1359 | : DeferredAction(ActionNode::STORE_POSITION, reg), |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1360 | cp_offset_(trace->cp_offset()), |
| 1361 | is_capture_(is_capture) { } |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1362 | int cp_offset() { return cp_offset_; } |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1363 | bool is_capture() { return is_capture_; } |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1364 | private: |
| 1365 | int cp_offset_; |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1366 | bool is_capture_; |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1367 | void set_cp_offset(int cp_offset) { cp_offset_ = cp_offset; } |
| 1368 | }; |
| 1369 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1370 | class DeferredSetRegister : public DeferredAction { |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1371 | public: |
| 1372 | DeferredSetRegister(int reg, int value) |
| 1373 | : DeferredAction(ActionNode::SET_REGISTER, reg), |
| 1374 | value_(value) { } |
| 1375 | int value() { return value_; } |
| 1376 | private: |
| 1377 | int value_; |
| 1378 | }; |
| 1379 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1380 | class DeferredClearCaptures : public DeferredAction { |
| 1381 | public: |
| 1382 | explicit DeferredClearCaptures(Interval range) |
| 1383 | : DeferredAction(ActionNode::CLEAR_CAPTURES, -1), |
| 1384 | range_(range) { } |
| 1385 | Interval range() { return range_; } |
| 1386 | private: |
| 1387 | Interval range_; |
| 1388 | }; |
| 1389 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1390 | class DeferredIncrementRegister : public DeferredAction { |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1391 | public: |
| 1392 | explicit DeferredIncrementRegister(int reg) |
| 1393 | : DeferredAction(ActionNode::INCREMENT_REGISTER, reg) { } |
| 1394 | }; |
| 1395 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1396 | Trace() |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1397 | : cp_offset_(0), |
| 1398 | actions_(NULL), |
| 1399 | backtrack_(NULL), |
| 1400 | stop_node_(NULL), |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1401 | loop_label_(NULL), |
| 1402 | characters_preloaded_(0), |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1403 | bound_checked_up_to_(0), |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1404 | flush_budget_(100), |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1405 | at_start_(UNKNOWN) { } |
| 1406 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1407 | // End the trace. This involves flushing the deferred actions in the trace |
| 1408 | // and pushing a backtrack location onto the backtrack stack. Once this is |
| 1409 | // done we can start a new trace or go to one that has already been |
| 1410 | // generated. |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1411 | void Flush(RegExpCompiler* compiler, RegExpNode* successor); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1412 | int cp_offset() { return cp_offset_; } |
| 1413 | DeferredAction* actions() { return actions_; } |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1414 | // A trivial trace is one that has no deferred actions or other state that |
| 1415 | // affects the assumptions used when generating code. There is no recorded |
| 1416 | // backtrack location in a trivial trace, so with a trivial trace we will |
| 1417 | // generate code that, on a failure to match, gets the backtrack location |
| 1418 | // from the backtrack stack rather than using a direct jump instruction. We |
| 1419 | // always start code generation with a trivial trace and non-trivial traces |
| 1420 | // are created as we emit code for nodes or add to the list of deferred |
| 1421 | // actions in the trace. The location of the code generated for a node using |
| 1422 | // a trivial trace is recorded in a label in the node so that gotos can be |
| 1423 | // generated to that code. |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1424 | bool is_trivial() { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1425 | return backtrack_ == NULL && |
| 1426 | actions_ == NULL && |
| 1427 | cp_offset_ == 0 && |
| 1428 | characters_preloaded_ == 0 && |
| 1429 | bound_checked_up_to_ == 0 && |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1430 | quick_check_performed_.characters() == 0 && |
| 1431 | at_start_ == UNKNOWN; |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1432 | } |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1433 | TriBool at_start() { return at_start_; } |
| 1434 | void set_at_start(bool at_start) { at_start_ = at_start ? TRUE : FALSE; } |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1435 | Label* backtrack() { return backtrack_; } |
| 1436 | Label* loop_label() { return loop_label_; } |
| 1437 | RegExpNode* stop_node() { return stop_node_; } |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1438 | int characters_preloaded() { return characters_preloaded_; } |
| 1439 | int bound_checked_up_to() { return bound_checked_up_to_; } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1440 | int flush_budget() { return flush_budget_; } |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1441 | QuickCheckDetails* quick_check_performed() { return &quick_check_performed_; } |
| 1442 | bool mentions_reg(int reg); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1443 | // Returns true if a deferred position store exists to the specified |
| 1444 | // register and stores the offset in the out-parameter. Otherwise |
| 1445 | // returns false. |
| 1446 | bool GetStoredPosition(int reg, int* cp_offset); |
| 1447 | // These set methods and AdvanceCurrentPositionInTrace should be used only on |
| 1448 | // new traces - the intention is that traces are immutable after creation. |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1449 | void add_action(DeferredAction* new_action) { |
| 1450 | ASSERT(new_action->next_ == NULL); |
| 1451 | new_action->next_ = actions_; |
| 1452 | actions_ = new_action; |
| 1453 | } |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1454 | void set_backtrack(Label* backtrack) { backtrack_ = backtrack; } |
| 1455 | void set_stop_node(RegExpNode* node) { stop_node_ = node; } |
| 1456 | void set_loop_label(Label* label) { loop_label_ = label; } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 1457 | void set_characters_preloaded(int count) { characters_preloaded_ = count; } |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1458 | void set_bound_checked_up_to(int to) { bound_checked_up_to_ = to; } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1459 | void set_flush_budget(int to) { flush_budget_ = to; } |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1460 | void set_quick_check_performed(QuickCheckDetails* d) { |
| 1461 | quick_check_performed_ = *d; |
| 1462 | } |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1463 | void InvalidateCurrentCharacter(); |
| 1464 | void AdvanceCurrentPositionInTrace(int by, RegExpCompiler* compiler); |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 1465 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1466 | private: |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1467 | int FindAffectedRegisters(OutSet* affected_registers, Zone* zone); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1468 | void PerformDeferredActions(RegExpMacroAssembler* macro, |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1469 | int max_register, |
| 1470 | OutSet& affected_registers, |
| 1471 | OutSet* registers_to_pop, |
| 1472 | OutSet* registers_to_clear, |
| 1473 | Zone* zone); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1474 | void RestoreAffectedRegisters(RegExpMacroAssembler* macro, |
| 1475 | int max_register, |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1476 | OutSet& registers_to_pop, |
| 1477 | OutSet& registers_to_clear); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1478 | int cp_offset_; |
| 1479 | DeferredAction* actions_; |
| 1480 | Label* backtrack_; |
| 1481 | RegExpNode* stop_node_; |
| 1482 | Label* loop_label_; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1483 | int characters_preloaded_; |
| 1484 | int bound_checked_up_to_; |
| 1485 | QuickCheckDetails quick_check_performed_; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1486 | int flush_budget_; |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1487 | TriBool at_start_; |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1488 | }; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1489 | |
| 1490 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1491 | class NodeVisitor { |
| 1492 | public: |
| 1493 | virtual ~NodeVisitor() { } |
| 1494 | #define DECLARE_VISIT(Type) \ |
| 1495 | virtual void Visit##Type(Type##Node* that) = 0; |
| 1496 | FOR_EACH_NODE_TYPE(DECLARE_VISIT) |
| 1497 | #undef DECLARE_VISIT |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1498 | virtual void VisitLoopChoice(LoopChoiceNode* that) { VisitChoice(that); } |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1499 | }; |
| 1500 | |
| 1501 | |
| 1502 | // Node visitor used to add the start set of the alternatives to the |
| 1503 | // dispatch table of a choice node. |
| 1504 | class DispatchTableConstructor: public NodeVisitor { |
| 1505 | public: |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1506 | DispatchTableConstructor(DispatchTable* table, bool ignore_case, |
| 1507 | Zone* zone) |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1508 | : table_(table), |
| 1509 | choice_index_(-1), |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1510 | ignore_case_(ignore_case), |
| 1511 | zone_(zone) { } |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1512 | |
| 1513 | void BuildTable(ChoiceNode* node); |
| 1514 | |
| 1515 | void AddRange(CharacterRange range) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1516 | table()->AddRange(range, choice_index_, zone_); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1517 | } |
| 1518 | |
| 1519 | void AddInverse(ZoneList<CharacterRange>* ranges); |
| 1520 | |
| 1521 | #define DECLARE_VISIT(Type) \ |
| 1522 | virtual void Visit##Type(Type##Node* that); |
| 1523 | FOR_EACH_NODE_TYPE(DECLARE_VISIT) |
| 1524 | #undef DECLARE_VISIT |
| 1525 | |
| 1526 | DispatchTable* table() { return table_; } |
| 1527 | void set_choice_index(int value) { choice_index_ = value; } |
| 1528 | |
| 1529 | protected: |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 1530 | DispatchTable* table_; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1531 | int choice_index_; |
| 1532 | bool ignore_case_; |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1533 | Zone* zone_; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1534 | }; |
| 1535 | |
| 1536 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1537 | // Assertion propagation moves information about assertions such as |
| 1538 | // \b to the affected nodes. For instance, in /.\b./ information must |
| 1539 | // be propagated to the first '.' that whatever follows needs to know |
| 1540 | // if it matched a word or a non-word, and to the second '.' that it |
| 1541 | // has to check if it succeeds a word or non-word. In this case the |
| 1542 | // result will be something like: |
| 1543 | // |
| 1544 | // +-------+ +------------+ |
| 1545 | // | . | | . | |
| 1546 | // +-------+ ---> +------------+ |
| 1547 | // | word? | | check word | |
| 1548 | // +-------+ +------------+ |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1549 | class Analysis: public NodeVisitor { |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1550 | public: |
ager@chromium.org | 38e4c71 | 2009-11-11 09:11:58 +0000 | [diff] [blame] | 1551 | Analysis(bool ignore_case, bool is_ascii) |
| 1552 | : ignore_case_(ignore_case), |
| 1553 | is_ascii_(is_ascii), |
| 1554 | error_message_(NULL) { } |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1555 | void EnsureAnalyzed(RegExpNode* node); |
| 1556 | |
| 1557 | #define DECLARE_VISIT(Type) \ |
| 1558 | virtual void Visit##Type(Type##Node* that); |
| 1559 | FOR_EACH_NODE_TYPE(DECLARE_VISIT) |
| 1560 | #undef DECLARE_VISIT |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1561 | virtual void VisitLoopChoice(LoopChoiceNode* that); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1562 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 1563 | bool has_failed() { return error_message_ != NULL; } |
| 1564 | const char* error_message() { |
| 1565 | ASSERT(error_message_ != NULL); |
| 1566 | return error_message_; |
| 1567 | } |
| 1568 | void fail(const char* error_message) { |
| 1569 | error_message_ = error_message; |
| 1570 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 1571 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1572 | private: |
| 1573 | bool ignore_case_; |
ager@chromium.org | 38e4c71 | 2009-11-11 09:11:58 +0000 | [diff] [blame] | 1574 | bool is_ascii_; |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 1575 | const char* error_message_; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1576 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1577 | DISALLOW_IMPLICIT_CONSTRUCTORS(Analysis); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1578 | }; |
| 1579 | |
| 1580 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1581 | struct RegExpCompileData { |
| 1582 | RegExpCompileData() |
| 1583 | : tree(NULL), |
| 1584 | node(NULL), |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1585 | simple(true), |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1586 | contains_anchor(false), |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1587 | capture_count(0) { } |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1588 | RegExpTree* tree; |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1589 | RegExpNode* node; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1590 | bool simple; |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1591 | bool contains_anchor; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1592 | Handle<String> error; |
| 1593 | int capture_count; |
| 1594 | }; |
| 1595 | |
| 1596 | |
| 1597 | class RegExpEngine: public AllStatic { |
| 1598 | public: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1599 | struct CompilationResult { |
| 1600 | explicit CompilationResult(const char* error_message) |
| 1601 | : error_message(error_message), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1602 | code(HEAP->the_hole_value()), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1603 | num_registers(0) {} |
| 1604 | CompilationResult(Object* code, int registers) |
| 1605 | : error_message(NULL), |
| 1606 | code(code), |
| 1607 | num_registers(registers) {} |
| 1608 | const char* error_message; |
| 1609 | Object* code; |
| 1610 | int num_registers; |
| 1611 | }; |
| 1612 | |
| 1613 | static CompilationResult Compile(RegExpCompileData* input, |
| 1614 | bool ignore_case, |
mstarzinger@chromium.org | 15613d0 | 2012-05-23 12:04:37 +0000 | [diff] [blame] | 1615 | bool global, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1616 | bool multiline, |
| 1617 | Handle<String> pattern, |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 1618 | Handle<String> sample_subject, |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 1619 | bool is_ascii, Zone* zone); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1620 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1621 | static void DotPrint(const char* label, RegExpNode* node, bool ignore_case); |
| 1622 | }; |
| 1623 | |
| 1624 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 1625 | class OffsetsVector { |
| 1626 | public: |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1627 | inline OffsetsVector(int num_registers, Isolate* isolate) |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 1628 | : offsets_vector_length_(num_registers) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1629 | if (offsets_vector_length_ > Isolate::kJSRegexpStaticOffsetsVectorSize) { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 1630 | vector_ = NewArray<int>(offsets_vector_length_); |
| 1631 | } else { |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1632 | vector_ = isolate->jsregexp_static_offsets_vector(); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 1633 | } |
| 1634 | } |
| 1635 | inline ~OffsetsVector() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1636 | if (offsets_vector_length_ > Isolate::kJSRegexpStaticOffsetsVectorSize) { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 1637 | DeleteArray(vector_); |
| 1638 | vector_ = NULL; |
| 1639 | } |
| 1640 | } |
| 1641 | inline int* vector() { return vector_; } |
| 1642 | inline int length() { return offsets_vector_length_; } |
| 1643 | |
mstarzinger@chromium.org | 15613d0 | 2012-05-23 12:04:37 +0000 | [diff] [blame] | 1644 | static const int kStaticOffsetsVectorSize = |
| 1645 | Isolate::kJSRegexpStaticOffsetsVectorSize; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 1646 | |
| 1647 | private: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1648 | static Address static_offsets_vector_address(Isolate* isolate) { |
| 1649 | return reinterpret_cast<Address>(isolate->jsregexp_static_offsets_vector()); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 1650 | } |
| 1651 | |
| 1652 | int* vector_; |
| 1653 | int offsets_vector_length_; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 1654 | |
| 1655 | friend class ExternalReference; |
| 1656 | }; |
| 1657 | |
| 1658 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1659 | } } // namespace v8::internal |
| 1660 | |
| 1661 | #endif // V8_JSREGEXP_H_ |