blob: fa4ef5d09f44e4dd0d0492d45a72d5c70143d5e1 [file] [log] [blame]
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001// Copyright 2012 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// 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.org1c092762011-05-09 09:42:16 +000031#include "allocation.h"
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000032#include "assembler.h"
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000033#include "zone-inl.h"
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +000034
kasperl@chromium.org71affb52009-05-26 05:44:31 +000035namespace v8 {
36namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000037
yangguo@chromium.org659ceec2012-01-26 07:37:54 +000038class NodeVisitor;
39class RegExpCompiler;
ager@chromium.orga74f0da2008-12-03 16:05:52 +000040class RegExpMacroAssembler;
yangguo@chromium.org659ceec2012-01-26 07:37:54 +000041class RegExpNode;
42class RegExpTree;
erik.corry@gmail.comed49e962012-04-17 11:57:53 +000043class BoyerMooreLookahead;
ager@chromium.orga74f0da2008-12-03 16:05:52 +000044
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000045class RegExpImpl {
46 public:
kasperl@chromium.org68ac0092009-07-09 06:00:35 +000047 // Whether V8 is compiled with native regexp support or not.
48 static bool UsesNativeRegExp() {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000049#ifdef V8_INTERPRETED_REGEXP
kasperl@chromium.org68ac0092009-07-09 06:00:35 +000050 return false;
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000051#else
52 return true;
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000053#endif
54 }
kasperl@chromium.org68ac0092009-07-09 06:00:35 +000055
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000056 // Creates a regular expression literal in the old space.
57 // This function calls the garbage collector if necessary.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000058 static Handle<Object> CreateRegExpLiteral(Handle<JSFunction> constructor,
59 Handle<String> pattern,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000060 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.org8bb60582008-12-11 12:02:20 +000068 // 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.org7be3c992009-03-12 07:19:55 +000071 // Returns false if compilation fails.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000072 static Handle<Object> Compile(Handle<JSRegExp> re,
73 Handle<String> pattern,
74 Handle<String> flags);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000075
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000076 // See ECMA-262 section 15.10.6.2.
77 // This function calls the garbage collector if necessary.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000078 static Handle<Object> Exec(Handle<JSRegExp> regexp,
79 Handle<String> subject,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000080 int index,
rossberg@chromium.org400388e2012-06-06 09:29:22 +000081 Handle<JSArray> lastMatchInfo,
82 Zone* zone);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000083
ager@chromium.org8bb60582008-12-11 12:02:20 +000084 // Prepares a JSRegExp object with Irregexp-specific data.
whesse@chromium.orgcec079d2010-03-22 14:44:04 +000085 static void IrregexpInitialize(Handle<JSRegExp> re,
86 Handle<String> pattern,
87 JSRegExp::Flags flags,
88 int capture_register_count);
ager@chromium.orga74f0da2008-12-03 16:05:52 +000089
90
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000091 static void AtomCompile(Handle<JSRegExp> re,
92 Handle<String> pattern,
93 JSRegExp::Flags flags,
94 Handle<String> match_pattern);
95
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000096 static Handle<Object> AtomExec(Handle<JSRegExp> regexp,
97 Handle<String> subject,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000098 int index,
99 Handle<JSArray> lastMatchInfo);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000100
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000101 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,
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000111 Handle<String> subject,
112 Zone* zone);
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000113
mstarzinger@chromium.org15613d02012-05-23 12:04:37 +0000114 // Calculate the size of offsets vector for the case of global regexp
115 // and the number of matches this vector is able to store.
116 static int GlobalOffsetsVectorSize(Handle<JSRegExp> regexp,
117 int registers_per_match,
118 int* max_matches);
119
120 // Execute a regular expression on the subject, starting from index.
121 // If matching succeeds, return the number of matches. This can be larger
122 // than one in the case of global regular expressions.
123 // The captures and subcaptures are stored into the registers vector.
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000124 // If matching fails, returns RE_FAILURE.
125 // If execution fails, sets a pending exception and returns RE_EXCEPTION.
mstarzinger@chromium.org15613d02012-05-23 12:04:37 +0000126 static int IrregexpExecRaw(Handle<JSRegExp> regexp,
127 Handle<String> subject,
128 int index,
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000129 Vector<int> registers,
130 Zone* zone);
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000131
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000132 // Execute an Irregexp bytecode pattern.
ager@chromium.org41826e72009-03-30 13:30:57 +0000133 // On a successful match, the result is a JSArray containing
134 // captured positions. On a failure, the result is the null value.
135 // Returns an empty handle in case of an exception.
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000136 static Handle<Object> IrregexpExec(Handle<JSRegExp> regexp,
137 Handle<String> subject,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000138 int index,
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000139 Handle<JSArray> lastMatchInfo,
140 Zone* zone);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000141
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000142 // Array index in the lastMatchInfo array.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000143 static const int kLastCaptureCount = 0;
144 static const int kLastSubject = 1;
145 static const int kLastInput = 2;
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000146 static const int kFirstCapture = 3;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000147 static const int kLastMatchOverhead = 3;
148
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000149 // Direct offset into the lastMatchInfo array.
150 static const int kLastCaptureCountOffset =
151 FixedArray::kHeaderSize + kLastCaptureCount * kPointerSize;
152 static const int kLastSubjectOffset =
153 FixedArray::kHeaderSize + kLastSubject * kPointerSize;
154 static const int kLastInputOffset =
155 FixedArray::kHeaderSize + kLastInput * kPointerSize;
156 static const int kFirstCaptureOffset =
157 FixedArray::kHeaderSize + kFirstCapture * kPointerSize;
158
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000159 // Used to access the lastMatchInfo array.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000160 static int GetCapture(FixedArray* array, int index) {
161 return Smi::cast(array->get(index + kFirstCapture))->value();
162 }
163
164 static void SetLastCaptureCount(FixedArray* array, int to) {
165 array->set(kLastCaptureCount, Smi::FromInt(to));
166 }
167
168 static void SetLastSubject(FixedArray* array, String* to) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000169 array->set(kLastSubject, to);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000170 }
171
172 static void SetLastInput(FixedArray* array, String* to) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000173 array->set(kLastInput, to);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000174 }
175
176 static void SetCapture(FixedArray* array, int index, int to) {
177 array->set(index + kFirstCapture, Smi::FromInt(to));
178 }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000179
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000180 static int GetLastCaptureCount(FixedArray* array) {
181 return Smi::cast(array->get(kLastCaptureCount))->value();
182 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000183
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000184 // For acting on the JSRegExp data FixedArray.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000185 static int IrregexpMaxRegisterCount(FixedArray* re);
186 static void SetIrregexpMaxRegisterCount(FixedArray* re, int value);
187 static int IrregexpNumberOfCaptures(FixedArray* re);
188 static int IrregexpNumberOfRegisters(FixedArray* re);
189 static ByteArray* IrregexpByteCode(FixedArray* re, bool is_ascii);
190 static Code* IrregexpNativeCode(FixedArray* re, bool is_ascii);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000191
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000192 // Limit the space regexps take up on the heap. In order to limit this we
193 // would like to keep track of the amount of regexp code on the heap. This
194 // is not tracked, however. As a conservative approximation we track the
195 // total regexp code compiled including code that has subsequently been freed
196 // and the total executable memory at any point.
197 static const int kRegExpExecutableMemoryLimit = 16 * MB;
198 static const int kRegWxpCompiledLimit = 1 * MB;
199
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000200 private:
201 static String* last_ascii_string_;
202 static String* two_byte_cached_string_;
203
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000204 static bool CompileIrregexp(
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000205 Handle<JSRegExp> re, Handle<String> sample_subject, bool is_ascii,
206 Zone* zone);
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000207 static inline bool EnsureCompiledIrregexp(
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000208 Handle<JSRegExp> re, Handle<String> sample_subject, bool is_ascii,
209 Zone* zone);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000210
211
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000212 // Set the subject cache. The previous string buffer is not deleted, so the
213 // caller should ensure that it doesn't leak.
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000214 static void SetSubjectCache(String* subject,
215 char* utf8_subject,
216 int uft8_length,
217 int character_position,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000218 int utf8_position);
219
220 // A one element cache of the last utf8_subject string and its length. The
221 // subject JS String object is cached in the heap. We also cache a
222 // translation between position and utf8 position.
223 static char* utf8_subject_cache_;
224 static int utf8_length_cache_;
225 static int utf8_position_;
226 static int character_position_;
227};
228
229
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000230// Represents the location of one element relative to the intersection of
231// two sets. Corresponds to the four areas of a Venn diagram.
232enum ElementInSetsRelation {
233 kInsideNone = 0,
234 kInsideFirst = 1,
235 kInsideSecond = 2,
236 kInsideBoth = 3
237};
238
239
danno@chromium.org1044a4d2012-04-30 12:34:39 +0000240// Represents code units in the range from from_ to to_, both ends are
241// inclusive.
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000242class CharacterRange {
243 public:
244 CharacterRange() : from_(0), to_(0) { }
245 // For compatibility with the CHECK_OK macro
246 CharacterRange(void* null) { ASSERT_EQ(NULL, null); } //NOLINT
247 CharacterRange(uc16 from, uc16 to) : from_(from), to_(to) { }
248 static void AddClassEscape(uc16 type, ZoneList<CharacterRange>* ranges);
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000249 static Vector<const int> GetWordBounds();
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000250 static inline CharacterRange Singleton(uc16 value) {
251 return CharacterRange(value, value);
252 }
253 static inline CharacterRange Range(uc16 from, uc16 to) {
254 ASSERT(from <= to);
255 return CharacterRange(from, to);
256 }
257 static inline CharacterRange Everything() {
258 return CharacterRange(0, 0xFFFF);
259 }
260 bool Contains(uc16 i) { return from_ <= i && i <= to_; }
261 uc16 from() const { return from_; }
262 void set_from(uc16 value) { from_ = value; }
263 uc16 to() const { return to_; }
264 void set_to(uc16 value) { to_ = value; }
265 bool is_valid() { return from_ <= to_; }
ager@chromium.org8bb60582008-12-11 12:02:20 +0000266 bool IsEverything(uc16 max) { return from_ == 0 && to_ >= max; }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000267 bool IsSingleton() { return (from_ == to_); }
ager@chromium.org38e4c712009-11-11 09:11:58 +0000268 void AddCaseEquivalents(ZoneList<CharacterRange>* ranges, bool is_ascii);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000269 static void Split(ZoneList<CharacterRange>* base,
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000270 Vector<const int> overlay,
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000271 ZoneList<CharacterRange>** included,
272 ZoneList<CharacterRange>** excluded);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000273 // Whether a range list is in canonical form: Ranges ordered by from value,
274 // and ranges non-overlapping and non-adjacent.
275 static bool IsCanonical(ZoneList<CharacterRange>* ranges);
276 // Convert range list to canonical form. The characters covered by the ranges
277 // will still be the same, but no character is in more than one range, and
278 // adjacent ranges are merged. The resulting list may be shorter than the
279 // original, but cannot be longer.
280 static void Canonicalize(ZoneList<CharacterRange>* ranges);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000281 // Negate the contents of a character range in canonical form.
282 static void Negate(ZoneList<CharacterRange>* src,
283 ZoneList<CharacterRange>* dst);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000284 static const int kStartMarker = (1 << 24);
285 static const int kPayloadMask = (1 << 24) - 1;
286
287 private:
288 uc16 from_;
289 uc16 to_;
290};
291
292
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000293// A set of unsigned integers that behaves especially well on small
294// integers (< 32). May do zone-allocation.
295class OutSet: public ZoneObject {
296 public:
297 OutSet() : first_(0), remaining_(NULL), successors_(NULL) { }
298 OutSet* Extend(unsigned value);
299 bool Get(unsigned value);
300 static const unsigned kFirstLimit = 32;
301
302 private:
303 // Destructively set a value in this set. In most cases you want
304 // to use Extend instead to ensure that only one instance exists
305 // that contains the same values.
306 void Set(unsigned value);
307
308 // The successors are a list of sets that contain the same values
309 // as this set and the one more value that is not present in this
310 // set.
311 ZoneList<OutSet*>* successors() { return successors_; }
312
313 OutSet(uint32_t first, ZoneList<unsigned>* remaining)
314 : first_(first), remaining_(remaining), successors_(NULL) { }
315 uint32_t first_;
316 ZoneList<unsigned>* remaining_;
317 ZoneList<OutSet*>* successors_;
ager@chromium.org32912102009-01-16 10:38:43 +0000318 friend class Trace;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000319};
320
321
322// A mapping from integers, specified as ranges, to a set of integers.
323// Used for mapping character ranges to choices.
324class DispatchTable : public ZoneObject {
325 public:
326 class Entry {
327 public:
328 Entry() : from_(0), to_(0), out_set_(NULL) { }
329 Entry(uc16 from, uc16 to, OutSet* out_set)
330 : from_(from), to_(to), out_set_(out_set) { }
331 uc16 from() { return from_; }
332 uc16 to() { return to_; }
333 void set_to(uc16 value) { to_ = value; }
334 void AddValue(int value) { out_set_ = out_set_->Extend(value); }
335 OutSet* out_set() { return out_set_; }
336 private:
337 uc16 from_;
338 uc16 to_;
339 OutSet* out_set_;
340 };
341
342 class Config {
343 public:
344 typedef uc16 Key;
345 typedef Entry Value;
346 static const uc16 kNoKey;
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000347 static const Entry NoValue() { return Value(); }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000348 static inline int Compare(uc16 a, uc16 b) {
349 if (a == b)
350 return 0;
351 else if (a < b)
352 return -1;
353 else
354 return 1;
355 }
356 };
357
358 void AddRange(CharacterRange range, int value);
359 OutSet* Get(uc16 value);
360 void Dump();
361
362 template <typename Callback>
363 void ForEach(Callback* callback) { return tree()->ForEach(callback); }
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000364
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000365 private:
366 // There can't be a static empty set since it allocates its
367 // successors in a zone and caches them.
368 OutSet* empty() { return &empty_; }
369 OutSet empty_;
370 ZoneSplayTree<Config>* tree() { return &tree_; }
371 ZoneSplayTree<Config> tree_;
372};
373
374
375#define FOR_EACH_NODE_TYPE(VISIT) \
376 VISIT(End) \
377 VISIT(Action) \
378 VISIT(Choice) \
379 VISIT(BackReference) \
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000380 VISIT(Assertion) \
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000381 VISIT(Text)
382
383
384#define FOR_EACH_REG_EXP_TREE_TYPE(VISIT) \
385 VISIT(Disjunction) \
386 VISIT(Alternative) \
387 VISIT(Assertion) \
388 VISIT(CharacterClass) \
389 VISIT(Atom) \
390 VISIT(Quantifier) \
391 VISIT(Capture) \
392 VISIT(Lookahead) \
393 VISIT(BackReference) \
394 VISIT(Empty) \
395 VISIT(Text)
396
397
398#define FORWARD_DECLARE(Name) class RegExp##Name;
399FOR_EACH_REG_EXP_TREE_TYPE(FORWARD_DECLARE)
400#undef FORWARD_DECLARE
401
402
403class TextElement {
404 public:
405 enum Type {UNINITIALIZED, ATOM, CHAR_CLASS};
406 TextElement() : type(UNINITIALIZED) { }
ager@chromium.org8bb60582008-12-11 12:02:20 +0000407 explicit TextElement(Type t) : type(t), cp_offset(-1) { }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000408 static TextElement Atom(RegExpAtom* atom);
409 static TextElement CharClass(RegExpCharacterClass* char_class);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000410 int length();
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000411 Type type;
412 union {
413 RegExpAtom* u_atom;
414 RegExpCharacterClass* u_char_class;
415 } data;
ager@chromium.org8bb60582008-12-11 12:02:20 +0000416 int cp_offset;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000417};
418
419
ager@chromium.org32912102009-01-16 10:38:43 +0000420class Trace;
ager@chromium.org8bb60582008-12-11 12:02:20 +0000421
422
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000423struct NodeInfo {
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000424 NodeInfo()
425 : being_analyzed(false),
426 been_analyzed(false),
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000427 follows_word_interest(false),
428 follows_newline_interest(false),
429 follows_start_interest(false),
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000430 at_end(false),
danno@chromium.org1044a4d2012-04-30 12:34:39 +0000431 visited(false),
432 replacement_calculated(false) { }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000433
434 // Returns true if the interests and assumptions of this node
435 // matches the given one.
436 bool Matches(NodeInfo* that) {
437 return (at_end == that->at_end) &&
438 (follows_word_interest == that->follows_word_interest) &&
439 (follows_newline_interest == that->follows_newline_interest) &&
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000440 (follows_start_interest == that->follows_start_interest);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000441 }
442
443 // Updates the interests of this node given the interests of the
444 // node preceding it.
445 void AddFromPreceding(NodeInfo* that) {
446 at_end |= that->at_end;
447 follows_word_interest |= that->follows_word_interest;
448 follows_newline_interest |= that->follows_newline_interest;
449 follows_start_interest |= that->follows_start_interest;
450 }
451
ager@chromium.org8bb60582008-12-11 12:02:20 +0000452 bool HasLookbehind() {
453 return follows_word_interest ||
454 follows_newline_interest ||
455 follows_start_interest;
456 }
457
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000458 // Sets the interests of this node to include the interests of the
459 // following node.
460 void AddFromFollowing(NodeInfo* that) {
461 follows_word_interest |= that->follows_word_interest;
462 follows_newline_interest |= that->follows_newline_interest;
463 follows_start_interest |= that->follows_start_interest;
464 }
465
466 void ResetCompilationState() {
467 being_analyzed = false;
468 been_analyzed = false;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000469 }
470
471 bool being_analyzed: 1;
472 bool been_analyzed: 1;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000473
474 // These bits are set of this node has to know what the preceding
475 // character was.
476 bool follows_word_interest: 1;
477 bool follows_newline_interest: 1;
478 bool follows_start_interest: 1;
479
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000480 bool at_end: 1;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000481 bool visited: 1;
danno@chromium.org1044a4d2012-04-30 12:34:39 +0000482 bool replacement_calculated: 1;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000483};
484
485
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000486// Details of a quick mask-compare check that can look ahead in the
487// input stream.
488class QuickCheckDetails {
489 public:
490 QuickCheckDetails()
491 : characters_(0),
492 mask_(0),
iposva@chromium.org245aa852009-02-10 00:49:54 +0000493 value_(0),
494 cannot_match_(false) { }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000495 explicit QuickCheckDetails(int characters)
496 : characters_(characters),
497 mask_(0),
iposva@chromium.org245aa852009-02-10 00:49:54 +0000498 value_(0),
499 cannot_match_(false) { }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000500 bool Rationalize(bool ascii);
501 // Merge in the information from another branch of an alternation.
502 void Merge(QuickCheckDetails* other, int from_index);
503 // Advance the current position by some amount.
504 void Advance(int by, bool ascii);
505 void Clear();
iposva@chromium.org245aa852009-02-10 00:49:54 +0000506 bool cannot_match() { return cannot_match_; }
507 void set_cannot_match() { cannot_match_ = true; }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000508 struct Position {
509 Position() : mask(0), value(0), determines_perfectly(false) { }
510 uc16 mask;
511 uc16 value;
512 bool determines_perfectly;
513 };
514 int characters() { return characters_; }
515 void set_characters(int characters) { characters_ = characters; }
516 Position* positions(int index) {
517 ASSERT(index >= 0);
518 ASSERT(index < characters_);
519 return positions_ + index;
520 }
521 uint32_t mask() { return mask_; }
522 uint32_t value() { return value_; }
523
524 private:
525 // How many characters do we have quick check information from. This is
526 // the same for all branches of a choice node.
527 int characters_;
528 Position positions_[4];
529 // These values are the condensate of the above array after Rationalize().
530 uint32_t mask_;
531 uint32_t value_;
iposva@chromium.org245aa852009-02-10 00:49:54 +0000532 // If set to true, there is no way this quick check can match at all.
533 // E.g., if it requires to be at the start of the input, and isn't.
534 bool cannot_match_;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000535};
536
537
danno@chromium.org1044a4d2012-04-30 12:34:39 +0000538extern int kUninitializedRegExpNodePlaceHolder;
539
540
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000541class RegExpNode: public ZoneObject {
542 public:
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000543 explicit RegExpNode(Zone* zone)
544 : replacement_(NULL), trace_count_(0), zone_(zone) {
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000545 bm_info_[0] = bm_info_[1] = NULL;
546 }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000547 virtual ~RegExpNode();
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000548 virtual void Accept(NodeVisitor* visitor) = 0;
549 // Generates a goto to this node or actually generates the code at this point.
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000550 virtual void Emit(RegExpCompiler* compiler, Trace* trace) = 0;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000551 // How many characters must this node consume at a minimum in order to
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000552 // succeed. If we have found at least 'still_to_find' characters that
553 // must be consumed there is no need to ask any following nodes whether
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000554 // they are sure to eat any more characters. The not_at_start argument is
555 // used to indicate that we know we are not at the start of the input. In
556 // this case anchored branches will always fail and can be ignored when
557 // determining how many characters are consumed on success.
558 virtual int EatsAtLeast(int still_to_find,
559 int recursion_depth,
560 bool not_at_start) = 0;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000561 // Emits some quick code that checks whether the preloaded characters match.
562 // Falls through on certain failure, jumps to the label on possible success.
563 // If the node cannot make a quick check it does nothing and returns false.
564 bool EmitQuickCheck(RegExpCompiler* compiler,
ager@chromium.org32912102009-01-16 10:38:43 +0000565 Trace* trace,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000566 bool preload_has_checked_bounds,
567 Label* on_possible_success,
568 QuickCheckDetails* details_return,
569 bool fall_through_on_failure);
570 // For a given number of characters this returns a mask and a value. The
571 // next n characters are anded with the mask and compared with the value.
572 // A comparison failure indicates the node cannot match the next n characters.
573 // A comparison success indicates the node may match.
574 virtual void GetQuickCheckDetails(QuickCheckDetails* details,
575 RegExpCompiler* compiler,
iposva@chromium.org245aa852009-02-10 00:49:54 +0000576 int characters_filled_in,
577 bool not_at_start) = 0;
ager@chromium.org8bb60582008-12-11 12:02:20 +0000578 static const int kNodeIsTooComplexForGreedyLoops = -1;
579 virtual int GreedyLoopTextLength() { return kNodeIsTooComplexForGreedyLoops; }
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000580 // Only returns the successor for a text node of length 1 that matches any
581 // character and that has no guards on it.
582 virtual RegExpNode* GetSuccessorOfOmnivorousTextNode(
583 RegExpCompiler* compiler) {
584 return NULL;
585 }
586
587 // Collects information on the possible code units (mod 128) that can match if
588 // we look forward. This is used for a Boyer-Moore-like string searching
589 // implementation. TODO(erikcorry): This should share more code with
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000590 // EatsAtLeast, GetQuickCheckDetails. The budget argument is used to limit
591 // the number of nodes we are willing to look at in order to create this data.
592 static const int kFillInBMBudget = 200;
verwaest@chromium.org37141392012-05-31 13:27:02 +0000593 virtual void FillInBMInfo(int offset,
594 int recursion_depth,
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000595 int budget,
verwaest@chromium.org37141392012-05-31 13:27:02 +0000596 BoyerMooreLookahead* bm,
597 bool not_at_start) {
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000598 UNREACHABLE();
599 }
danno@chromium.org1044a4d2012-04-30 12:34:39 +0000600
601 // If we know that the input is ASCII then there are some nodes that can
602 // never match. This method returns a node that can be substituted for
603 // itself, or NULL if the node can never match.
604 virtual RegExpNode* FilterASCII(int depth) { return this; }
605 // Helper for FilterASCII.
606 RegExpNode* replacement() {
607 ASSERT(info()->replacement_calculated);
608 return replacement_;
609 }
610 RegExpNode* set_replacement(RegExpNode* replacement) {
611 info()->replacement_calculated = true;
612 replacement_ = replacement;
613 return replacement; // For convenience.
614 }
615
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000616 // We want to avoid recalculating the lookahead info, so we store it on the
617 // node. Only info that is for this node is stored. We can tell that the
618 // info is for this node when offset == 0, so the information is calculated
619 // relative to this node.
620 void SaveBMInfo(BoyerMooreLookahead* bm, bool not_at_start, int offset) {
621 if (offset == 0) set_bm_info(not_at_start, bm);
622 }
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000623
ager@chromium.org8bb60582008-12-11 12:02:20 +0000624 Label* label() { return &label_; }
ulan@chromium.org2efb9002012-01-19 15:36:35 +0000625 // If non-generic code is generated for a node (i.e. the node is not at the
ager@chromium.org32912102009-01-16 10:38:43 +0000626 // start of the trace) then it cannot be reused. This variable sets a limit
627 // on how often we allow that to happen before we insist on starting a new
628 // trace and generating generic code for a node that can be reused by flushing
629 // the deferred actions in the current trace and generating a goto.
630 static const int kMaxCopiesCodeGenerated = 10;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000631
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000632 NodeInfo* info() { return &info_; }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000633
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000634 BoyerMooreLookahead* bm_info(bool not_at_start) {
635 return bm_info_[not_at_start ? 1 : 0];
636 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000637
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000638 Zone* zone() { return zone_; }
639
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000640 protected:
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000641 enum LimitResult { DONE, CONTINUE };
danno@chromium.org1044a4d2012-04-30 12:34:39 +0000642 RegExpNode* replacement_;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000643
ager@chromium.org32912102009-01-16 10:38:43 +0000644 LimitResult LimitVersions(RegExpCompiler* compiler, Trace* trace);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000645
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000646 void set_bm_info(bool not_at_start, BoyerMooreLookahead* bm) {
647 bm_info_[not_at_start ? 1 : 0] = bm;
648 }
649
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000650 private:
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000651 static const int kFirstCharBudget = 10;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000652 Label label_;
653 NodeInfo info_;
ager@chromium.org32912102009-01-16 10:38:43 +0000654 // This variable keeps track of how many times code has been generated for
655 // this node (in different traces). We don't keep track of where the
656 // generated code is located unless the code is generated at the start of
657 // a trace, in which case it is generic and can be reused by flushing the
658 // deferred operations in the current trace and generating a goto.
659 int trace_count_;
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000660 BoyerMooreLookahead* bm_info_[2];
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000661
662 Zone* zone_;
ager@chromium.org32912102009-01-16 10:38:43 +0000663};
664
665
666// A simple closed interval.
667class Interval {
668 public:
669 Interval() : from_(kNone), to_(kNone) { }
670 Interval(int from, int to) : from_(from), to_(to) { }
671 Interval Union(Interval that) {
672 if (that.from_ == kNone)
673 return *this;
674 else if (from_ == kNone)
675 return that;
676 else
677 return Interval(Min(from_, that.from_), Max(to_, that.to_));
678 }
679 bool Contains(int value) {
680 return (from_ <= value) && (value <= to_);
681 }
682 bool is_empty() { return from_ == kNone; }
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000683 int from() const { return from_; }
684 int to() const { return to_; }
ager@chromium.org32912102009-01-16 10:38:43 +0000685 static Interval Empty() { return Interval(); }
686 static const int kNone = -1;
687 private:
688 int from_;
689 int to_;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000690};
691
692
693class SeqRegExpNode: public RegExpNode {
694 public:
695 explicit SeqRegExpNode(RegExpNode* on_success)
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000696 : RegExpNode(on_success->zone()), on_success_(on_success) { }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000697 RegExpNode* on_success() { return on_success_; }
698 void set_on_success(RegExpNode* node) { on_success_ = node; }
danno@chromium.org1044a4d2012-04-30 12:34:39 +0000699 virtual RegExpNode* FilterASCII(int depth);
verwaest@chromium.org37141392012-05-31 13:27:02 +0000700 virtual void FillInBMInfo(int offset,
701 int recursion_depth,
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000702 int budget,
verwaest@chromium.org37141392012-05-31 13:27:02 +0000703 BoyerMooreLookahead* bm,
704 bool not_at_start) {
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000705 on_success_->FillInBMInfo(
706 offset, recursion_depth + 1, budget - 1, bm, not_at_start);
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000707 if (offset == 0) set_bm_info(not_at_start, bm);
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000708 }
danno@chromium.org1044a4d2012-04-30 12:34:39 +0000709
710 protected:
711 RegExpNode* FilterSuccessor(int depth);
712
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000713 private:
714 RegExpNode* on_success_;
715};
716
717
718class ActionNode: public SeqRegExpNode {
719 public:
720 enum Type {
ager@chromium.org8bb60582008-12-11 12:02:20 +0000721 SET_REGISTER,
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000722 INCREMENT_REGISTER,
723 STORE_POSITION,
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000724 BEGIN_SUBMATCH,
ager@chromium.org32912102009-01-16 10:38:43 +0000725 POSITIVE_SUBMATCH_SUCCESS,
726 EMPTY_MATCH_CHECK,
727 CLEAR_CAPTURES
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000728 };
ager@chromium.org8bb60582008-12-11 12:02:20 +0000729 static ActionNode* SetRegister(int reg, int val, RegExpNode* on_success);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000730 static ActionNode* IncrementRegister(int reg, RegExpNode* on_success);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000731 static ActionNode* StorePosition(int reg,
732 bool is_capture,
733 RegExpNode* on_success);
ager@chromium.org32912102009-01-16 10:38:43 +0000734 static ActionNode* ClearCaptures(Interval range, RegExpNode* on_success);
735 static ActionNode* BeginSubmatch(int stack_pointer_reg,
736 int position_reg,
737 RegExpNode* on_success);
738 static ActionNode* PositiveSubmatchSuccess(int stack_pointer_reg,
739 int restore_reg,
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000740 int clear_capture_count,
741 int clear_capture_from,
ager@chromium.org32912102009-01-16 10:38:43 +0000742 RegExpNode* on_success);
743 static ActionNode* EmptyMatchCheck(int start_register,
744 int repetition_register,
745 int repetition_limit,
746 RegExpNode* on_success);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000747 virtual void Accept(NodeVisitor* visitor);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000748 virtual void Emit(RegExpCompiler* compiler, Trace* trace);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000749 virtual int EatsAtLeast(int still_to_find,
750 int recursion_depth,
751 bool not_at_start);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000752 virtual void GetQuickCheckDetails(QuickCheckDetails* details,
753 RegExpCompiler* compiler,
iposva@chromium.org245aa852009-02-10 00:49:54 +0000754 int filled_in,
755 bool not_at_start) {
756 return on_success()->GetQuickCheckDetails(
757 details, compiler, filled_in, not_at_start);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000758 }
verwaest@chromium.org37141392012-05-31 13:27:02 +0000759 virtual void FillInBMInfo(int offset,
760 int recursion_depth,
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000761 int budget,
verwaest@chromium.org37141392012-05-31 13:27:02 +0000762 BoyerMooreLookahead* bm,
763 bool not_at_start);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000764 Type type() { return type_; }
765 // TODO(erikcorry): We should allow some action nodes in greedy loops.
766 virtual int GreedyLoopTextLength() { return kNodeIsTooComplexForGreedyLoops; }
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000767
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000768 private:
769 union {
770 struct {
771 int reg;
772 int value;
773 } u_store_register;
774 struct {
775 int reg;
776 } u_increment_register;
777 struct {
778 int reg;
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000779 bool is_capture;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000780 } u_position_register;
781 struct {
782 int stack_pointer_register;
783 int current_position_register;
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000784 int clear_register_count;
785 int clear_register_from;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000786 } u_submatch;
ager@chromium.org32912102009-01-16 10:38:43 +0000787 struct {
788 int start_register;
789 int repetition_register;
790 int repetition_limit;
791 } u_empty_match_check;
792 struct {
793 int range_from;
794 int range_to;
795 } u_clear_captures;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000796 } data_;
797 ActionNode(Type type, RegExpNode* on_success)
798 : SeqRegExpNode(on_success),
799 type_(type) { }
800 Type type_;
801 friend class DotPrinter;
802};
803
804
805class TextNode: public SeqRegExpNode {
806 public:
807 TextNode(ZoneList<TextElement>* elms,
ager@chromium.org8bb60582008-12-11 12:02:20 +0000808 RegExpNode* on_success)
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000809 : SeqRegExpNode(on_success),
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000810 elms_(elms) { }
811 TextNode(RegExpCharacterClass* that,
ager@chromium.org8bb60582008-12-11 12:02:20 +0000812 RegExpNode* on_success)
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000813 : SeqRegExpNode(on_success),
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000814 elms_(new ZoneList<TextElement>(1, zone())) {
815 elms_->Add(TextElement::CharClass(that), zone());
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000816 }
817 virtual void Accept(NodeVisitor* visitor);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000818 virtual void Emit(RegExpCompiler* compiler, Trace* trace);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000819 virtual int EatsAtLeast(int still_to_find,
820 int recursion_depth,
821 bool not_at_start);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000822 virtual void GetQuickCheckDetails(QuickCheckDetails* details,
823 RegExpCompiler* compiler,
iposva@chromium.org245aa852009-02-10 00:49:54 +0000824 int characters_filled_in,
825 bool not_at_start);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000826 ZoneList<TextElement>* elements() { return elms_; }
ager@chromium.org38e4c712009-11-11 09:11:58 +0000827 void MakeCaseIndependent(bool is_ascii);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000828 virtual int GreedyLoopTextLength();
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000829 virtual RegExpNode* GetSuccessorOfOmnivorousTextNode(
830 RegExpCompiler* compiler);
verwaest@chromium.org37141392012-05-31 13:27:02 +0000831 virtual void FillInBMInfo(int offset,
832 int recursion_depth,
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000833 int budget,
verwaest@chromium.org37141392012-05-31 13:27:02 +0000834 BoyerMooreLookahead* bm,
835 bool not_at_start);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000836 void CalculateOffsets();
danno@chromium.org1044a4d2012-04-30 12:34:39 +0000837 virtual RegExpNode* FilterASCII(int depth);
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000838
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000839 private:
840 enum TextEmitPassType {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000841 NON_ASCII_MATCH, // Check for characters that can't match.
842 SIMPLE_CHARACTER_MATCH, // Case-dependent single character check.
843 NON_LETTER_CHARACTER_MATCH, // Check characters that have no case equivs.
844 CASE_CHARACTER_MATCH, // Case-independent single character check.
845 CHARACTER_CLASS_MATCH // Character class.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000846 };
ager@chromium.org381abbb2009-02-25 13:23:22 +0000847 static bool SkipPass(int pass, bool ignore_case);
848 static const int kFirstRealPass = SIMPLE_CHARACTER_MATCH;
849 static const int kLastPass = CHARACTER_CLASS_MATCH;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000850 void TextEmitPass(RegExpCompiler* compiler,
851 TextEmitPassType pass,
852 bool preloaded,
ager@chromium.org32912102009-01-16 10:38:43 +0000853 Trace* trace,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000854 bool first_element_checked,
855 int* checked_up_to);
856 int Length();
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000857 ZoneList<TextElement>* elms_;
858};
859
860
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000861class AssertionNode: public SeqRegExpNode {
862 public:
863 enum AssertionNodeType {
864 AT_END,
865 AT_START,
866 AT_BOUNDARY,
867 AT_NON_BOUNDARY,
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000868 AFTER_NEWLINE
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000869 };
870 static AssertionNode* AtEnd(RegExpNode* on_success) {
871 return new AssertionNode(AT_END, on_success);
872 }
873 static AssertionNode* AtStart(RegExpNode* on_success) {
874 return new AssertionNode(AT_START, on_success);
875 }
876 static AssertionNode* AtBoundary(RegExpNode* on_success) {
877 return new AssertionNode(AT_BOUNDARY, on_success);
878 }
879 static AssertionNode* AtNonBoundary(RegExpNode* on_success) {
880 return new AssertionNode(AT_NON_BOUNDARY, on_success);
881 }
882 static AssertionNode* AfterNewline(RegExpNode* on_success) {
883 return new AssertionNode(AFTER_NEWLINE, on_success);
884 }
885 virtual void Accept(NodeVisitor* visitor);
886 virtual void Emit(RegExpCompiler* compiler, Trace* trace);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000887 virtual int EatsAtLeast(int still_to_find,
888 int recursion_depth,
889 bool not_at_start);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000890 virtual void GetQuickCheckDetails(QuickCheckDetails* details,
891 RegExpCompiler* compiler,
iposva@chromium.org245aa852009-02-10 00:49:54 +0000892 int filled_in,
893 bool not_at_start);
verwaest@chromium.org37141392012-05-31 13:27:02 +0000894 virtual void FillInBMInfo(int offset,
895 int recursion_depth,
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000896 int budget,
verwaest@chromium.org37141392012-05-31 13:27:02 +0000897 BoyerMooreLookahead* bm,
898 bool not_at_start);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000899 AssertionNodeType type() { return type_; }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000900 void set_type(AssertionNodeType type) { type_ = type; }
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000901
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000902 private:
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000903 void EmitBoundaryCheck(RegExpCompiler* compiler, Trace* trace);
904 enum IfPrevious { kIsNonWord, kIsWord };
905 void BacktrackIfPrevious(RegExpCompiler* compiler,
906 Trace* trace,
907 IfPrevious backtrack_if_previous);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000908 AssertionNode(AssertionNodeType t, RegExpNode* on_success)
909 : SeqRegExpNode(on_success), type_(t) { }
910 AssertionNodeType type_;
911};
912
913
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000914class BackReferenceNode: public SeqRegExpNode {
915 public:
916 BackReferenceNode(int start_reg,
917 int end_reg,
ager@chromium.org8bb60582008-12-11 12:02:20 +0000918 RegExpNode* on_success)
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000919 : SeqRegExpNode(on_success),
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000920 start_reg_(start_reg),
921 end_reg_(end_reg) { }
922 virtual void Accept(NodeVisitor* visitor);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000923 int start_register() { return start_reg_; }
924 int end_register() { return end_reg_; }
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000925 virtual void Emit(RegExpCompiler* compiler, Trace* trace);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000926 virtual int EatsAtLeast(int still_to_find,
927 int recursion_depth,
928 bool not_at_start);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000929 virtual void GetQuickCheckDetails(QuickCheckDetails* details,
930 RegExpCompiler* compiler,
iposva@chromium.org245aa852009-02-10 00:49:54 +0000931 int characters_filled_in,
932 bool not_at_start) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000933 return;
934 }
verwaest@chromium.org37141392012-05-31 13:27:02 +0000935 virtual void FillInBMInfo(int offset,
936 int recursion_depth,
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000937 int budget,
verwaest@chromium.org37141392012-05-31 13:27:02 +0000938 BoyerMooreLookahead* bm,
939 bool not_at_start);
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000940
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000941 private:
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000942 int start_reg_;
943 int end_reg_;
944};
945
946
947class EndNode: public RegExpNode {
948 public:
ager@chromium.org8bb60582008-12-11 12:02:20 +0000949 enum Action { ACCEPT, BACKTRACK, NEGATIVE_SUBMATCH_SUCCESS };
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000950 explicit EndNode(Action action, Zone* zone)
951 : RegExpNode(zone), action_(action) { }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000952 virtual void Accept(NodeVisitor* visitor);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000953 virtual void Emit(RegExpCompiler* compiler, Trace* trace);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000954 virtual int EatsAtLeast(int still_to_find,
955 int recursion_depth,
956 bool not_at_start) { return 0; }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000957 virtual void GetQuickCheckDetails(QuickCheckDetails* details,
958 RegExpCompiler* compiler,
iposva@chromium.org245aa852009-02-10 00:49:54 +0000959 int characters_filled_in,
960 bool not_at_start) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000961 // Returning 0 from EatsAtLeast should ensure we never get here.
962 UNREACHABLE();
963 }
verwaest@chromium.org37141392012-05-31 13:27:02 +0000964 virtual void FillInBMInfo(int offset,
965 int recursion_depth,
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000966 int budget,
verwaest@chromium.org37141392012-05-31 13:27:02 +0000967 BoyerMooreLookahead* bm,
968 bool not_at_start) {
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000969 // Returning 0 from EatsAtLeast should ensure we never get here.
970 UNREACHABLE();
971 }
danno@chromium.org1044a4d2012-04-30 12:34:39 +0000972
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000973 private:
974 Action action_;
975};
976
977
ager@chromium.org8bb60582008-12-11 12:02:20 +0000978class NegativeSubmatchSuccess: public EndNode {
979 public:
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000980 NegativeSubmatchSuccess(int stack_pointer_reg,
981 int position_reg,
982 int clear_capture_count,
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000983 int clear_capture_start,
984 Zone* zone)
985 : EndNode(NEGATIVE_SUBMATCH_SUCCESS, zone),
ager@chromium.org8bb60582008-12-11 12:02:20 +0000986 stack_pointer_register_(stack_pointer_reg),
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000987 current_position_register_(position_reg),
988 clear_capture_count_(clear_capture_count),
989 clear_capture_start_(clear_capture_start) { }
990 virtual void Emit(RegExpCompiler* compiler, Trace* trace);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000991
992 private:
993 int stack_pointer_register_;
994 int current_position_register_;
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000995 int clear_capture_count_;
996 int clear_capture_start_;
ager@chromium.org8bb60582008-12-11 12:02:20 +0000997};
998
999
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001000class Guard: public ZoneObject {
1001 public:
1002 enum Relation { LT, GEQ };
1003 Guard(int reg, Relation op, int value)
1004 : reg_(reg),
1005 op_(op),
1006 value_(value) { }
1007 int reg() { return reg_; }
1008 Relation op() { return op_; }
1009 int value() { return value_; }
1010
1011 private:
1012 int reg_;
1013 Relation op_;
1014 int value_;
1015};
1016
1017
1018class GuardedAlternative {
1019 public:
1020 explicit GuardedAlternative(RegExpNode* node) : node_(node), guards_(NULL) { }
1021 void AddGuard(Guard* guard);
1022 RegExpNode* node() { return node_; }
1023 void set_node(RegExpNode* node) { node_ = node; }
1024 ZoneList<Guard*>* guards() { return guards_; }
1025
1026 private:
1027 RegExpNode* node_;
1028 ZoneList<Guard*>* guards_;
1029};
1030
1031
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001032class AlternativeGeneration;
1033
1034
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001035class ChoiceNode: public RegExpNode {
1036 public:
rossberg@chromium.org400388e2012-06-06 09:29:22 +00001037 explicit ChoiceNode(int expected_size, Zone* zone)
1038 : RegExpNode(zone),
1039 alternatives_(new ZoneList<GuardedAlternative>(expected_size, zone)),
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001040 table_(NULL),
iposva@chromium.org245aa852009-02-10 00:49:54 +00001041 not_at_start_(false),
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001042 being_calculated_(false) { }
1043 virtual void Accept(NodeVisitor* visitor);
rossberg@chromium.org400388e2012-06-06 09:29:22 +00001044 void AddAlternative(GuardedAlternative node) {
1045 alternatives()->Add(node, zone());
1046 }
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001047 ZoneList<GuardedAlternative>* alternatives() { return alternatives_; }
1048 DispatchTable* GetTable(bool ignore_case);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001049 virtual void Emit(RegExpCompiler* compiler, Trace* trace);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001050 virtual int EatsAtLeast(int still_to_find,
1051 int recursion_depth,
1052 bool not_at_start);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001053 int EatsAtLeastHelper(int still_to_find,
1054 int recursion_depth,
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001055 RegExpNode* ignore_this_node,
1056 bool not_at_start);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001057 virtual void GetQuickCheckDetails(QuickCheckDetails* details,
1058 RegExpCompiler* compiler,
iposva@chromium.org245aa852009-02-10 00:49:54 +00001059 int characters_filled_in,
1060 bool not_at_start);
verwaest@chromium.org37141392012-05-31 13:27:02 +00001061 virtual void FillInBMInfo(int offset,
1062 int recursion_depth,
rossberg@chromium.org400388e2012-06-06 09:29:22 +00001063 int budget,
verwaest@chromium.org37141392012-05-31 13:27:02 +00001064 BoyerMooreLookahead* bm,
1065 bool not_at_start);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001066
1067 bool being_calculated() { return being_calculated_; }
iposva@chromium.org245aa852009-02-10 00:49:54 +00001068 bool not_at_start() { return not_at_start_; }
1069 void set_not_at_start() { not_at_start_ = true; }
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001070 void set_being_calculated(bool b) { being_calculated_ = b; }
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001071 virtual bool try_to_emit_quick_check_for_alternative(int i) { return true; }
danno@chromium.org1044a4d2012-04-30 12:34:39 +00001072 virtual RegExpNode* FilterASCII(int depth);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001073
ager@chromium.org8bb60582008-12-11 12:02:20 +00001074 protected:
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001075 int GreedyLoopTextLengthForAlternative(GuardedAlternative* alternative);
ager@chromium.org8bb60582008-12-11 12:02:20 +00001076 ZoneList<GuardedAlternative>* alternatives_;
1077
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001078 private:
1079 friend class DispatchTableConstructor;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001080 friend class Analysis;
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001081 void GenerateGuard(RegExpMacroAssembler* macro_assembler,
ager@chromium.org5ec48922009-05-05 07:25:34 +00001082 Guard* guard,
ager@chromium.org32912102009-01-16 10:38:43 +00001083 Trace* trace);
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00001084 int CalculatePreloadCharacters(RegExpCompiler* compiler, int eats_at_least);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001085 void EmitOutOfLineContinuation(RegExpCompiler* compiler,
ager@chromium.org32912102009-01-16 10:38:43 +00001086 Trace* trace,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001087 GuardedAlternative alternative,
1088 AlternativeGeneration* alt_gen,
1089 int preload_characters,
1090 bool next_expects_preload);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001091 DispatchTable* table_;
iposva@chromium.org245aa852009-02-10 00:49:54 +00001092 // If true, this node is never checked at the start of the input.
1093 // Allows a new trace to start with at_start() set to false.
1094 bool not_at_start_;
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001095 bool being_calculated_;
1096};
1097
1098
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001099class NegativeLookaheadChoiceNode: public ChoiceNode {
1100 public:
1101 explicit NegativeLookaheadChoiceNode(GuardedAlternative this_must_fail,
rossberg@chromium.org400388e2012-06-06 09:29:22 +00001102 GuardedAlternative then_do_this,
1103 Zone* zone)
1104 : ChoiceNode(2, zone) {
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001105 AddAlternative(this_must_fail);
1106 AddAlternative(then_do_this);
1107 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001108 virtual int EatsAtLeast(int still_to_find,
1109 int recursion_depth,
1110 bool not_at_start);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001111 virtual void GetQuickCheckDetails(QuickCheckDetails* details,
1112 RegExpCompiler* compiler,
iposva@chromium.org245aa852009-02-10 00:49:54 +00001113 int characters_filled_in,
1114 bool not_at_start);
verwaest@chromium.org37141392012-05-31 13:27:02 +00001115 virtual void FillInBMInfo(int offset,
1116 int recursion_depth,
rossberg@chromium.org400388e2012-06-06 09:29:22 +00001117 int budget,
verwaest@chromium.org37141392012-05-31 13:27:02 +00001118 BoyerMooreLookahead* bm,
1119 bool not_at_start) {
1120 alternatives_->at(1).node()->FillInBMInfo(
rossberg@chromium.org400388e2012-06-06 09:29:22 +00001121 offset, recursion_depth + 1, budget - 1, bm, not_at_start);
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00001122 if (offset == 0) set_bm_info(not_at_start, bm);
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00001123 }
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001124 // For a negative lookahead we don't emit the quick check for the
1125 // alternative that is expected to fail. This is because quick check code
1126 // starts by loading enough characters for the alternative that takes fewest
1127 // characters, but on a negative lookahead the negative branch did not take
1128 // part in that calculation (EatsAtLeast) so the assumptions don't hold.
1129 virtual bool try_to_emit_quick_check_for_alternative(int i) { return i != 0; }
danno@chromium.org1044a4d2012-04-30 12:34:39 +00001130 virtual RegExpNode* FilterASCII(int depth);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001131};
1132
1133
ager@chromium.org8bb60582008-12-11 12:02:20 +00001134class LoopChoiceNode: public ChoiceNode {
1135 public:
rossberg@chromium.org400388e2012-06-06 09:29:22 +00001136 explicit LoopChoiceNode(bool body_can_be_zero_length, Zone* zone)
1137 : ChoiceNode(2, zone),
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001138 loop_node_(NULL),
1139 continue_node_(NULL),
1140 body_can_be_zero_length_(body_can_be_zero_length) { }
1141 void AddLoopAlternative(GuardedAlternative alt);
1142 void AddContinueAlternative(GuardedAlternative alt);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001143 virtual void Emit(RegExpCompiler* compiler, Trace* trace);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001144 virtual int EatsAtLeast(int still_to_find,
1145 int recursion_depth,
1146 bool not_at_start);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001147 virtual void GetQuickCheckDetails(QuickCheckDetails* details,
1148 RegExpCompiler* compiler,
iposva@chromium.org245aa852009-02-10 00:49:54 +00001149 int characters_filled_in,
1150 bool not_at_start);
verwaest@chromium.org37141392012-05-31 13:27:02 +00001151 virtual void FillInBMInfo(int offset,
1152 int recursion_depth,
rossberg@chromium.org400388e2012-06-06 09:29:22 +00001153 int budget,
verwaest@chromium.org37141392012-05-31 13:27:02 +00001154 BoyerMooreLookahead* bm,
1155 bool not_at_start);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001156 RegExpNode* loop_node() { return loop_node_; }
1157 RegExpNode* continue_node() { return continue_node_; }
1158 bool body_can_be_zero_length() { return body_can_be_zero_length_; }
1159 virtual void Accept(NodeVisitor* visitor);
danno@chromium.org1044a4d2012-04-30 12:34:39 +00001160 virtual RegExpNode* FilterASCII(int depth);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001161
1162 private:
1163 // AddAlternative is made private for loop nodes because alternatives
1164 // should not be added freely, we need to keep track of which node
1165 // goes back to the node itself.
1166 void AddAlternative(GuardedAlternative node) {
1167 ChoiceNode::AddAlternative(node);
1168 }
1169
1170 RegExpNode* loop_node_;
1171 RegExpNode* continue_node_;
1172 bool body_can_be_zero_length_;
ager@chromium.org8bb60582008-12-11 12:02:20 +00001173};
1174
1175
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00001176// Improve the speed that we scan for an initial point where a non-anchored
1177// regexp can match by using a Boyer-Moore-like table. This is done by
1178// identifying non-greedy non-capturing loops in the nodes that eat any
1179// character one at a time. For example in the middle of the regexp
1180// /foo[\s\S]*?bar/ we find such a loop. There is also such a loop implicitly
1181// inserted at the start of any non-anchored regexp.
1182//
1183// When we have found such a loop we look ahead in the nodes to find the set of
1184// characters that can come at given distances. For example for the regexp
1185// /.?foo/ we know that there are at least 3 characters ahead of us, and the
1186// sets of characters that can occur are [any, [f, o], [o]]. We find a range in
1187// the lookahead info where the set of characters is reasonably constrained. In
1188// our example this is from index 1 to 2 (0 is not constrained). We can now
1189// look 3 characters ahead and if we don't find one of [f, o] (the union of
1190// [f, o] and [o]) then we can skip forwards by the range size (in this case 2).
1191//
1192// For Unicode input strings we do the same, but modulo 128.
1193//
1194// We also look at the first string fed to the regexp and use that to get a hint
1195// of the character frequencies in the inputs. This affects the assessment of
1196// whether the set of characters is 'reasonably constrained'.
1197//
1198// We also have another lookahead mechanism (called quick check in the code),
1199// which uses a wide load of multiple characters followed by a mask and compare
1200// to determine whether a match is possible at this point.
1201enum ContainedInLattice {
1202 kNotYet = 0,
1203 kLatticeIn = 1,
1204 kLatticeOut = 2,
1205 kLatticeUnknown = 3 // Can also mean both in and out.
1206};
1207
1208
1209inline ContainedInLattice Combine(ContainedInLattice a, ContainedInLattice b) {
1210 return static_cast<ContainedInLattice>(a | b);
1211}
1212
1213
1214ContainedInLattice AddRange(ContainedInLattice a,
1215 const int* ranges,
1216 int ranges_size,
1217 Interval new_range);
1218
1219
1220class BoyerMoorePositionInfo : public ZoneObject {
1221 public:
rossberg@chromium.org400388e2012-06-06 09:29:22 +00001222 explicit BoyerMoorePositionInfo(Zone* zone)
1223 : map_(new ZoneList<bool>(kMapSize, zone)),
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00001224 map_count_(0),
1225 w_(kNotYet),
1226 s_(kNotYet),
1227 d_(kNotYet),
1228 surrogate_(kNotYet) {
1229 for (int i = 0; i < kMapSize; i++) {
rossberg@chromium.org400388e2012-06-06 09:29:22 +00001230 map_->Add(false, zone);
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00001231 }
1232 }
1233
1234 bool& at(int i) { return map_->at(i); }
1235
1236 static const int kMapSize = 128;
1237 static const int kMask = kMapSize - 1;
1238
1239 int map_count() const { return map_count_; }
1240
1241 void Set(int character);
1242 void SetInterval(const Interval& interval);
1243 void SetAll();
1244 bool is_non_word() { return w_ == kLatticeOut; }
1245 bool is_word() { return w_ == kLatticeIn; }
1246
1247 private:
1248 ZoneList<bool>* map_;
1249 int map_count_; // Number of set bits in the map.
1250 ContainedInLattice w_; // The \w character class.
1251 ContainedInLattice s_; // The \s character class.
1252 ContainedInLattice d_; // The \d character class.
1253 ContainedInLattice surrogate_; // Surrogate UTF-16 code units.
1254};
1255
1256
1257class BoyerMooreLookahead : public ZoneObject {
1258 public:
rossberg@chromium.org400388e2012-06-06 09:29:22 +00001259 BoyerMooreLookahead(int length, RegExpCompiler* compiler, Zone* zone);
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00001260
1261 int length() { return length_; }
1262 int max_char() { return max_char_; }
1263 RegExpCompiler* compiler() { return compiler_; }
1264
1265 int Count(int map_number) {
1266 return bitmaps_->at(map_number)->map_count();
1267 }
1268
1269 BoyerMoorePositionInfo* at(int i) { return bitmaps_->at(i); }
1270
1271 void Set(int map_number, int character) {
1272 if (character > max_char_) return;
1273 BoyerMoorePositionInfo* info = bitmaps_->at(map_number);
1274 info->Set(character);
1275 }
1276
1277 void SetInterval(int map_number, const Interval& interval) {
1278 if (interval.from() > max_char_) return;
1279 BoyerMoorePositionInfo* info = bitmaps_->at(map_number);
1280 if (interval.to() > max_char_) {
1281 info->SetInterval(Interval(interval.from(), max_char_));
1282 } else {
1283 info->SetInterval(interval);
1284 }
1285 }
1286
1287 void SetAll(int map_number) {
1288 bitmaps_->at(map_number)->SetAll();
1289 }
1290
1291 void SetRest(int from_map) {
1292 for (int i = from_map; i < length_; i++) SetAll(i);
1293 }
1294 bool EmitSkipInstructions(RegExpMacroAssembler* masm);
1295
1296 private:
1297 // This is the value obtained by EatsAtLeast. If we do not have at least this
1298 // many characters left in the sample string then the match is bound to fail.
1299 // Therefore it is OK to read a character this far ahead of the current match
1300 // point.
1301 int length_;
1302 RegExpCompiler* compiler_;
1303 // 0x7f for ASCII, 0xffff for UTF-16.
1304 int max_char_;
1305 ZoneList<BoyerMoorePositionInfo*>* bitmaps_;
1306
1307 int GetSkipTable(int min_lookahead,
1308 int max_lookahead,
1309 Handle<ByteArray> boolean_skip_table);
1310 bool FindWorthwhileInterval(int* from, int* to);
1311 int FindBestInterval(
1312 int max_number_of_chars, int old_biggest_points, int* from, int* to);
1313};
1314
1315
ager@chromium.org8bb60582008-12-11 12:02:20 +00001316// There are many ways to generate code for a node. This class encapsulates
1317// the current way we should be generating. In other words it encapsulates
ager@chromium.org32912102009-01-16 10:38:43 +00001318// the current state of the code generator. The effect of this is that we
1319// generate code for paths that the matcher can take through the regular
1320// expression. A given node in the regexp can be code-generated several times
1321// as it can be part of several traces. For example for the regexp:
1322// /foo(bar|ip)baz/ the code to match baz will be generated twice, once as part
1323// of the foo-bar-baz trace and once as part of the foo-ip-baz trace. The code
1324// to match foo is generated only once (the traces have a common prefix). The
1325// code to store the capture is deferred and generated (twice) after the places
1326// where baz has been matched.
1327class Trace {
ager@chromium.org8bb60582008-12-11 12:02:20 +00001328 public:
iposva@chromium.org245aa852009-02-10 00:49:54 +00001329 // A value for a property that is either known to be true, know to be false,
1330 // or not known.
1331 enum TriBool {
1332 UNKNOWN = -1, FALSE = 0, TRUE = 1
1333 };
1334
ager@chromium.org8bb60582008-12-11 12:02:20 +00001335 class DeferredAction {
1336 public:
1337 DeferredAction(ActionNode::Type type, int reg)
1338 : type_(type), reg_(reg), next_(NULL) { }
1339 DeferredAction* next() { return next_; }
ager@chromium.org32912102009-01-16 10:38:43 +00001340 bool Mentions(int reg);
ager@chromium.org8bb60582008-12-11 12:02:20 +00001341 int reg() { return reg_; }
1342 ActionNode::Type type() { return type_; }
1343 private:
1344 ActionNode::Type type_;
1345 int reg_;
1346 DeferredAction* next_;
ager@chromium.org32912102009-01-16 10:38:43 +00001347 friend class Trace;
ager@chromium.org8bb60582008-12-11 12:02:20 +00001348 };
1349
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001350 class DeferredCapture : public DeferredAction {
ager@chromium.org8bb60582008-12-11 12:02:20 +00001351 public:
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001352 DeferredCapture(int reg, bool is_capture, Trace* trace)
ager@chromium.org8bb60582008-12-11 12:02:20 +00001353 : DeferredAction(ActionNode::STORE_POSITION, reg),
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001354 cp_offset_(trace->cp_offset()),
1355 is_capture_(is_capture) { }
ager@chromium.org8bb60582008-12-11 12:02:20 +00001356 int cp_offset() { return cp_offset_; }
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001357 bool is_capture() { return is_capture_; }
ager@chromium.org8bb60582008-12-11 12:02:20 +00001358 private:
1359 int cp_offset_;
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001360 bool is_capture_;
ager@chromium.org8bb60582008-12-11 12:02:20 +00001361 void set_cp_offset(int cp_offset) { cp_offset_ = cp_offset; }
1362 };
1363
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001364 class DeferredSetRegister : public DeferredAction {
ager@chromium.org8bb60582008-12-11 12:02:20 +00001365 public:
1366 DeferredSetRegister(int reg, int value)
1367 : DeferredAction(ActionNode::SET_REGISTER, reg),
1368 value_(value) { }
1369 int value() { return value_; }
1370 private:
1371 int value_;
1372 };
1373
ager@chromium.org32912102009-01-16 10:38:43 +00001374 class DeferredClearCaptures : public DeferredAction {
1375 public:
1376 explicit DeferredClearCaptures(Interval range)
1377 : DeferredAction(ActionNode::CLEAR_CAPTURES, -1),
1378 range_(range) { }
1379 Interval range() { return range_; }
1380 private:
1381 Interval range_;
1382 };
1383
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001384 class DeferredIncrementRegister : public DeferredAction {
ager@chromium.org8bb60582008-12-11 12:02:20 +00001385 public:
1386 explicit DeferredIncrementRegister(int reg)
1387 : DeferredAction(ActionNode::INCREMENT_REGISTER, reg) { }
1388 };
1389
ager@chromium.org32912102009-01-16 10:38:43 +00001390 Trace()
ager@chromium.org8bb60582008-12-11 12:02:20 +00001391 : cp_offset_(0),
1392 actions_(NULL),
1393 backtrack_(NULL),
1394 stop_node_(NULL),
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001395 loop_label_(NULL),
1396 characters_preloaded_(0),
iposva@chromium.org245aa852009-02-10 00:49:54 +00001397 bound_checked_up_to_(0),
ager@chromium.org381abbb2009-02-25 13:23:22 +00001398 flush_budget_(100),
iposva@chromium.org245aa852009-02-10 00:49:54 +00001399 at_start_(UNKNOWN) { }
1400
ager@chromium.org32912102009-01-16 10:38:43 +00001401 // End the trace. This involves flushing the deferred actions in the trace
1402 // and pushing a backtrack location onto the backtrack stack. Once this is
1403 // done we can start a new trace or go to one that has already been
1404 // generated.
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001405 void Flush(RegExpCompiler* compiler, RegExpNode* successor);
ager@chromium.org8bb60582008-12-11 12:02:20 +00001406 int cp_offset() { return cp_offset_; }
1407 DeferredAction* actions() { return actions_; }
ager@chromium.org32912102009-01-16 10:38:43 +00001408 // A trivial trace is one that has no deferred actions or other state that
1409 // affects the assumptions used when generating code. There is no recorded
1410 // backtrack location in a trivial trace, so with a trivial trace we will
1411 // generate code that, on a failure to match, gets the backtrack location
1412 // from the backtrack stack rather than using a direct jump instruction. We
1413 // always start code generation with a trivial trace and non-trivial traces
1414 // are created as we emit code for nodes or add to the list of deferred
1415 // actions in the trace. The location of the code generated for a node using
1416 // a trivial trace is recorded in a label in the node so that gotos can be
1417 // generated to that code.
ager@chromium.org8bb60582008-12-11 12:02:20 +00001418 bool is_trivial() {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001419 return backtrack_ == NULL &&
1420 actions_ == NULL &&
1421 cp_offset_ == 0 &&
1422 characters_preloaded_ == 0 &&
1423 bound_checked_up_to_ == 0 &&
iposva@chromium.org245aa852009-02-10 00:49:54 +00001424 quick_check_performed_.characters() == 0 &&
1425 at_start_ == UNKNOWN;
ager@chromium.org8bb60582008-12-11 12:02:20 +00001426 }
iposva@chromium.org245aa852009-02-10 00:49:54 +00001427 TriBool at_start() { return at_start_; }
1428 void set_at_start(bool at_start) { at_start_ = at_start ? TRUE : FALSE; }
ager@chromium.org8bb60582008-12-11 12:02:20 +00001429 Label* backtrack() { return backtrack_; }
1430 Label* loop_label() { return loop_label_; }
1431 RegExpNode* stop_node() { return stop_node_; }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001432 int characters_preloaded() { return characters_preloaded_; }
1433 int bound_checked_up_to() { return bound_checked_up_to_; }
ager@chromium.org381abbb2009-02-25 13:23:22 +00001434 int flush_budget() { return flush_budget_; }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001435 QuickCheckDetails* quick_check_performed() { return &quick_check_performed_; }
1436 bool mentions_reg(int reg);
ager@chromium.org32912102009-01-16 10:38:43 +00001437 // Returns true if a deferred position store exists to the specified
1438 // register and stores the offset in the out-parameter. Otherwise
1439 // returns false.
1440 bool GetStoredPosition(int reg, int* cp_offset);
1441 // These set methods and AdvanceCurrentPositionInTrace should be used only on
1442 // new traces - the intention is that traces are immutable after creation.
ager@chromium.org8bb60582008-12-11 12:02:20 +00001443 void add_action(DeferredAction* new_action) {
1444 ASSERT(new_action->next_ == NULL);
1445 new_action->next_ = actions_;
1446 actions_ = new_action;
1447 }
ager@chromium.org8bb60582008-12-11 12:02:20 +00001448 void set_backtrack(Label* backtrack) { backtrack_ = backtrack; }
1449 void set_stop_node(RegExpNode* node) { stop_node_ = node; }
1450 void set_loop_label(Label* label) { loop_label_ = label; }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001451 void set_characters_preloaded(int count) { characters_preloaded_ = count; }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001452 void set_bound_checked_up_to(int to) { bound_checked_up_to_ = to; }
ager@chromium.org381abbb2009-02-25 13:23:22 +00001453 void set_flush_budget(int to) { flush_budget_ = to; }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001454 void set_quick_check_performed(QuickCheckDetails* d) {
1455 quick_check_performed_ = *d;
1456 }
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001457 void InvalidateCurrentCharacter();
1458 void AdvanceCurrentPositionInTrace(int by, RegExpCompiler* compiler);
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00001459
ager@chromium.org8bb60582008-12-11 12:02:20 +00001460 private:
1461 int FindAffectedRegisters(OutSet* affected_registers);
1462 void PerformDeferredActions(RegExpMacroAssembler* macro,
1463 int max_register,
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001464 OutSet& affected_registers,
1465 OutSet* registers_to_pop,
1466 OutSet* registers_to_clear);
ager@chromium.org8bb60582008-12-11 12:02:20 +00001467 void RestoreAffectedRegisters(RegExpMacroAssembler* macro,
1468 int max_register,
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001469 OutSet& registers_to_pop,
1470 OutSet& registers_to_clear);
ager@chromium.org8bb60582008-12-11 12:02:20 +00001471 int cp_offset_;
1472 DeferredAction* actions_;
1473 Label* backtrack_;
1474 RegExpNode* stop_node_;
1475 Label* loop_label_;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001476 int characters_preloaded_;
1477 int bound_checked_up_to_;
1478 QuickCheckDetails quick_check_performed_;
ager@chromium.org381abbb2009-02-25 13:23:22 +00001479 int flush_budget_;
iposva@chromium.org245aa852009-02-10 00:49:54 +00001480 TriBool at_start_;
ager@chromium.org8bb60582008-12-11 12:02:20 +00001481};
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001482
1483
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001484class NodeVisitor {
1485 public:
1486 virtual ~NodeVisitor() { }
1487#define DECLARE_VISIT(Type) \
1488 virtual void Visit##Type(Type##Node* that) = 0;
1489FOR_EACH_NODE_TYPE(DECLARE_VISIT)
1490#undef DECLARE_VISIT
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001491 virtual void VisitLoopChoice(LoopChoiceNode* that) { VisitChoice(that); }
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001492};
1493
1494
1495// Node visitor used to add the start set of the alternatives to the
1496// dispatch table of a choice node.
1497class DispatchTableConstructor: public NodeVisitor {
1498 public:
1499 DispatchTableConstructor(DispatchTable* table, bool ignore_case)
1500 : table_(table),
1501 choice_index_(-1),
1502 ignore_case_(ignore_case) { }
1503
1504 void BuildTable(ChoiceNode* node);
1505
1506 void AddRange(CharacterRange range) {
1507 table()->AddRange(range, choice_index_);
1508 }
1509
1510 void AddInverse(ZoneList<CharacterRange>* ranges);
1511
1512#define DECLARE_VISIT(Type) \
1513 virtual void Visit##Type(Type##Node* that);
1514FOR_EACH_NODE_TYPE(DECLARE_VISIT)
1515#undef DECLARE_VISIT
1516
1517 DispatchTable* table() { return table_; }
1518 void set_choice_index(int value) { choice_index_ = value; }
1519
1520 protected:
ager@chromium.org5ec48922009-05-05 07:25:34 +00001521 DispatchTable* table_;
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001522 int choice_index_;
1523 bool ignore_case_;
1524};
1525
1526
ager@chromium.org8bb60582008-12-11 12:02:20 +00001527// Assertion propagation moves information about assertions such as
1528// \b to the affected nodes. For instance, in /.\b./ information must
1529// be propagated to the first '.' that whatever follows needs to know
1530// if it matched a word or a non-word, and to the second '.' that it
1531// has to check if it succeeds a word or non-word. In this case the
1532// result will be something like:
1533//
1534// +-------+ +------------+
1535// | . | | . |
1536// +-------+ ---> +------------+
1537// | word? | | check word |
1538// +-------+ +------------+
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001539class Analysis: public NodeVisitor {
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001540 public:
ager@chromium.org38e4c712009-11-11 09:11:58 +00001541 Analysis(bool ignore_case, bool is_ascii)
1542 : ignore_case_(ignore_case),
1543 is_ascii_(is_ascii),
1544 error_message_(NULL) { }
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001545 void EnsureAnalyzed(RegExpNode* node);
1546
1547#define DECLARE_VISIT(Type) \
1548 virtual void Visit##Type(Type##Node* that);
1549FOR_EACH_NODE_TYPE(DECLARE_VISIT)
1550#undef DECLARE_VISIT
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001551 virtual void VisitLoopChoice(LoopChoiceNode* that);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001552
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00001553 bool has_failed() { return error_message_ != NULL; }
1554 const char* error_message() {
1555 ASSERT(error_message_ != NULL);
1556 return error_message_;
1557 }
1558 void fail(const char* error_message) {
1559 error_message_ = error_message;
1560 }
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00001561
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001562 private:
1563 bool ignore_case_;
ager@chromium.org38e4c712009-11-11 09:11:58 +00001564 bool is_ascii_;
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00001565 const char* error_message_;
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001566
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001567 DISALLOW_IMPLICIT_CONSTRUCTORS(Analysis);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001568};
1569
1570
ager@chromium.org8bb60582008-12-11 12:02:20 +00001571struct RegExpCompileData {
1572 RegExpCompileData()
1573 : tree(NULL),
1574 node(NULL),
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001575 simple(true),
iposva@chromium.org245aa852009-02-10 00:49:54 +00001576 contains_anchor(false),
ager@chromium.org8bb60582008-12-11 12:02:20 +00001577 capture_count(0) { }
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001578 RegExpTree* tree;
ager@chromium.org8bb60582008-12-11 12:02:20 +00001579 RegExpNode* node;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001580 bool simple;
iposva@chromium.org245aa852009-02-10 00:49:54 +00001581 bool contains_anchor;
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001582 Handle<String> error;
1583 int capture_count;
1584};
1585
1586
1587class RegExpEngine: public AllStatic {
1588 public:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001589 struct CompilationResult {
1590 explicit CompilationResult(const char* error_message)
1591 : error_message(error_message),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001592 code(HEAP->the_hole_value()),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001593 num_registers(0) {}
1594 CompilationResult(Object* code, int registers)
1595 : error_message(NULL),
1596 code(code),
1597 num_registers(registers) {}
1598 const char* error_message;
1599 Object* code;
1600 int num_registers;
1601 };
1602
1603 static CompilationResult Compile(RegExpCompileData* input,
1604 bool ignore_case,
mstarzinger@chromium.org15613d02012-05-23 12:04:37 +00001605 bool global,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001606 bool multiline,
1607 Handle<String> pattern,
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00001608 Handle<String> sample_subject,
rossberg@chromium.org400388e2012-06-06 09:29:22 +00001609 bool is_ascii, Zone* zone);
ager@chromium.org8bb60582008-12-11 12:02:20 +00001610
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001611 static void DotPrint(const char* label, RegExpNode* node, bool ignore_case);
1612};
1613
1614
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001615class OffsetsVector {
1616 public:
ulan@chromium.org812308e2012-02-29 15:58:45 +00001617 inline OffsetsVector(int num_registers, Isolate* isolate)
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001618 : offsets_vector_length_(num_registers) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001619 if (offsets_vector_length_ > Isolate::kJSRegexpStaticOffsetsVectorSize) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001620 vector_ = NewArray<int>(offsets_vector_length_);
1621 } else {
ulan@chromium.org812308e2012-02-29 15:58:45 +00001622 vector_ = isolate->jsregexp_static_offsets_vector();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001623 }
1624 }
1625 inline ~OffsetsVector() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001626 if (offsets_vector_length_ > Isolate::kJSRegexpStaticOffsetsVectorSize) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001627 DeleteArray(vector_);
1628 vector_ = NULL;
1629 }
1630 }
1631 inline int* vector() { return vector_; }
1632 inline int length() { return offsets_vector_length_; }
1633
mstarzinger@chromium.org15613d02012-05-23 12:04:37 +00001634 static const int kStaticOffsetsVectorSize =
1635 Isolate::kJSRegexpStaticOffsetsVectorSize;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001636
1637 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001638 static Address static_offsets_vector_address(Isolate* isolate) {
1639 return reinterpret_cast<Address>(isolate->jsregexp_static_offsets_vector());
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001640 }
1641
1642 int* vector_;
1643 int offsets_vector_length_;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001644
1645 friend class ExternalReference;
1646};
1647
1648
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001649} } // namespace v8::internal
1650
1651#endif // V8_JSREGEXP_H_