blob: 20313ca559ff1dff6a47c4dd86879a64b6451dc5 [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,
81 Handle<JSArray> lastMatchInfo);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000082
ager@chromium.org8bb60582008-12-11 12:02:20 +000083 // Prepares a JSRegExp object with Irregexp-specific data.
whesse@chromium.orgcec079d2010-03-22 14:44:04 +000084 static void IrregexpInitialize(Handle<JSRegExp> re,
85 Handle<String> pattern,
86 JSRegExp::Flags flags,
87 int capture_register_count);
ager@chromium.orga74f0da2008-12-03 16:05:52 +000088
89
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000090 static void AtomCompile(Handle<JSRegExp> re,
91 Handle<String> pattern,
92 JSRegExp::Flags flags,
93 Handle<String> match_pattern);
94
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000095 static Handle<Object> AtomExec(Handle<JSRegExp> regexp,
96 Handle<String> subject,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000097 int index,
98 Handle<JSArray> lastMatchInfo);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000099
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000100 enum IrregexpResult { RE_FAILURE = 0, RE_SUCCESS = 1, RE_EXCEPTION = -1 };
101
102 // Prepare a RegExp for being executed one or more times (using
103 // IrregexpExecOnce) on the subject.
104 // This ensures that the regexp is compiled for the subject, and that
105 // the subject is flat.
106 // Returns the number of integer spaces required by IrregexpExecOnce
107 // as its "registers" argument. If the regexp cannot be compiled,
108 // an exception is set as pending, and this function returns negative.
109 static int IrregexpPrepare(Handle<JSRegExp> regexp,
110 Handle<String> subject);
111
112 // Execute a regular expression once on the subject, starting from
113 // character "index".
114 // If successful, returns RE_SUCCESS and set the capture positions
115 // in the first registers.
116 // If matching fails, returns RE_FAILURE.
117 // If execution fails, sets a pending exception and returns RE_EXCEPTION.
118 static IrregexpResult IrregexpExecOnce(Handle<JSRegExp> regexp,
119 Handle<String> subject,
120 int index,
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000121 Vector<int> registers);
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000122
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000123 // Execute an Irregexp bytecode pattern.
ager@chromium.org41826e72009-03-30 13:30:57 +0000124 // On a successful match, the result is a JSArray containing
125 // captured positions. On a failure, the result is the null value.
126 // Returns an empty handle in case of an exception.
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000127 static Handle<Object> IrregexpExec(Handle<JSRegExp> regexp,
128 Handle<String> subject,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000129 int index,
130 Handle<JSArray> lastMatchInfo);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000131
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000132 // Array index in the lastMatchInfo array.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000133 static const int kLastCaptureCount = 0;
134 static const int kLastSubject = 1;
135 static const int kLastInput = 2;
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000136 static const int kFirstCapture = 3;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000137 static const int kLastMatchOverhead = 3;
138
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000139 // Direct offset into the lastMatchInfo array.
140 static const int kLastCaptureCountOffset =
141 FixedArray::kHeaderSize + kLastCaptureCount * kPointerSize;
142 static const int kLastSubjectOffset =
143 FixedArray::kHeaderSize + kLastSubject * kPointerSize;
144 static const int kLastInputOffset =
145 FixedArray::kHeaderSize + kLastInput * kPointerSize;
146 static const int kFirstCaptureOffset =
147 FixedArray::kHeaderSize + kFirstCapture * kPointerSize;
148
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000149 // Used to access the lastMatchInfo array.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000150 static int GetCapture(FixedArray* array, int index) {
151 return Smi::cast(array->get(index + kFirstCapture))->value();
152 }
153
154 static void SetLastCaptureCount(FixedArray* array, int to) {
155 array->set(kLastCaptureCount, Smi::FromInt(to));
156 }
157
158 static void SetLastSubject(FixedArray* array, String* to) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000159 array->set(kLastSubject, to);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000160 }
161
162 static void SetLastInput(FixedArray* array, String* to) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000163 array->set(kLastInput, to);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000164 }
165
166 static void SetCapture(FixedArray* array, int index, int to) {
167 array->set(index + kFirstCapture, Smi::FromInt(to));
168 }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000169
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000170 static int GetLastCaptureCount(FixedArray* array) {
171 return Smi::cast(array->get(kLastCaptureCount))->value();
172 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000173
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000174 // For acting on the JSRegExp data FixedArray.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000175 static int IrregexpMaxRegisterCount(FixedArray* re);
176 static void SetIrregexpMaxRegisterCount(FixedArray* re, int value);
177 static int IrregexpNumberOfCaptures(FixedArray* re);
178 static int IrregexpNumberOfRegisters(FixedArray* re);
179 static ByteArray* IrregexpByteCode(FixedArray* re, bool is_ascii);
180 static Code* IrregexpNativeCode(FixedArray* re, bool is_ascii);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000181
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000182 // Limit the space regexps take up on the heap. In order to limit this we
183 // would like to keep track of the amount of regexp code on the heap. This
184 // is not tracked, however. As a conservative approximation we track the
185 // total regexp code compiled including code that has subsequently been freed
186 // and the total executable memory at any point.
187 static const int kRegExpExecutableMemoryLimit = 16 * MB;
188 static const int kRegWxpCompiledLimit = 1 * MB;
189
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000190 private:
191 static String* last_ascii_string_;
192 static String* two_byte_cached_string_;
193
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000194 static bool CompileIrregexp(
195 Handle<JSRegExp> re, Handle<String> sample_subject, bool is_ascii);
196 static inline bool EnsureCompiledIrregexp(
197 Handle<JSRegExp> re, Handle<String> sample_subject, bool is_ascii);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000198
199
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000200 // Set the subject cache. The previous string buffer is not deleted, so the
201 // caller should ensure that it doesn't leak.
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000202 static void SetSubjectCache(String* subject,
203 char* utf8_subject,
204 int uft8_length,
205 int character_position,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000206 int utf8_position);
207
208 // A one element cache of the last utf8_subject string and its length. The
209 // subject JS String object is cached in the heap. We also cache a
210 // translation between position and utf8 position.
211 static char* utf8_subject_cache_;
212 static int utf8_length_cache_;
213 static int utf8_position_;
214 static int character_position_;
215};
216
217
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000218// Represents the location of one element relative to the intersection of
219// two sets. Corresponds to the four areas of a Venn diagram.
220enum ElementInSetsRelation {
221 kInsideNone = 0,
222 kInsideFirst = 1,
223 kInsideSecond = 2,
224 kInsideBoth = 3
225};
226
227
danno@chromium.org1044a4d2012-04-30 12:34:39 +0000228// Represents code units in the range from from_ to to_, both ends are
229// inclusive.
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000230class CharacterRange {
231 public:
232 CharacterRange() : from_(0), to_(0) { }
233 // For compatibility with the CHECK_OK macro
234 CharacterRange(void* null) { ASSERT_EQ(NULL, null); } //NOLINT
235 CharacterRange(uc16 from, uc16 to) : from_(from), to_(to) { }
236 static void AddClassEscape(uc16 type, ZoneList<CharacterRange>* ranges);
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000237 static Vector<const int> GetWordBounds();
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000238 static inline CharacterRange Singleton(uc16 value) {
239 return CharacterRange(value, value);
240 }
241 static inline CharacterRange Range(uc16 from, uc16 to) {
242 ASSERT(from <= to);
243 return CharacterRange(from, to);
244 }
245 static inline CharacterRange Everything() {
246 return CharacterRange(0, 0xFFFF);
247 }
248 bool Contains(uc16 i) { return from_ <= i && i <= to_; }
249 uc16 from() const { return from_; }
250 void set_from(uc16 value) { from_ = value; }
251 uc16 to() const { return to_; }
252 void set_to(uc16 value) { to_ = value; }
253 bool is_valid() { return from_ <= to_; }
ager@chromium.org8bb60582008-12-11 12:02:20 +0000254 bool IsEverything(uc16 max) { return from_ == 0 && to_ >= max; }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000255 bool IsSingleton() { return (from_ == to_); }
ager@chromium.org38e4c712009-11-11 09:11:58 +0000256 void AddCaseEquivalents(ZoneList<CharacterRange>* ranges, bool is_ascii);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000257 static void Split(ZoneList<CharacterRange>* base,
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000258 Vector<const int> overlay,
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000259 ZoneList<CharacterRange>** included,
260 ZoneList<CharacterRange>** excluded);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000261 // Whether a range list is in canonical form: Ranges ordered by from value,
262 // and ranges non-overlapping and non-adjacent.
263 static bool IsCanonical(ZoneList<CharacterRange>* ranges);
264 // Convert range list to canonical form. The characters covered by the ranges
265 // will still be the same, but no character is in more than one range, and
266 // adjacent ranges are merged. The resulting list may be shorter than the
267 // original, but cannot be longer.
268 static void Canonicalize(ZoneList<CharacterRange>* ranges);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000269 // Negate the contents of a character range in canonical form.
270 static void Negate(ZoneList<CharacterRange>* src,
271 ZoneList<CharacterRange>* dst);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000272 static const int kStartMarker = (1 << 24);
273 static const int kPayloadMask = (1 << 24) - 1;
274
275 private:
276 uc16 from_;
277 uc16 to_;
278};
279
280
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000281// A set of unsigned integers that behaves especially well on small
282// integers (< 32). May do zone-allocation.
283class OutSet: public ZoneObject {
284 public:
285 OutSet() : first_(0), remaining_(NULL), successors_(NULL) { }
286 OutSet* Extend(unsigned value);
287 bool Get(unsigned value);
288 static const unsigned kFirstLimit = 32;
289
290 private:
291 // Destructively set a value in this set. In most cases you want
292 // to use Extend instead to ensure that only one instance exists
293 // that contains the same values.
294 void Set(unsigned value);
295
296 // The successors are a list of sets that contain the same values
297 // as this set and the one more value that is not present in this
298 // set.
299 ZoneList<OutSet*>* successors() { return successors_; }
300
301 OutSet(uint32_t first, ZoneList<unsigned>* remaining)
302 : first_(first), remaining_(remaining), successors_(NULL) { }
303 uint32_t first_;
304 ZoneList<unsigned>* remaining_;
305 ZoneList<OutSet*>* successors_;
ager@chromium.org32912102009-01-16 10:38:43 +0000306 friend class Trace;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000307};
308
309
310// A mapping from integers, specified as ranges, to a set of integers.
311// Used for mapping character ranges to choices.
312class DispatchTable : public ZoneObject {
313 public:
314 class Entry {
315 public:
316 Entry() : from_(0), to_(0), out_set_(NULL) { }
317 Entry(uc16 from, uc16 to, OutSet* out_set)
318 : from_(from), to_(to), out_set_(out_set) { }
319 uc16 from() { return from_; }
320 uc16 to() { return to_; }
321 void set_to(uc16 value) { to_ = value; }
322 void AddValue(int value) { out_set_ = out_set_->Extend(value); }
323 OutSet* out_set() { return out_set_; }
324 private:
325 uc16 from_;
326 uc16 to_;
327 OutSet* out_set_;
328 };
329
330 class Config {
331 public:
332 typedef uc16 Key;
333 typedef Entry Value;
334 static const uc16 kNoKey;
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000335 static const Entry NoValue() { return Value(); }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000336 static inline int Compare(uc16 a, uc16 b) {
337 if (a == b)
338 return 0;
339 else if (a < b)
340 return -1;
341 else
342 return 1;
343 }
344 };
345
346 void AddRange(CharacterRange range, int value);
347 OutSet* Get(uc16 value);
348 void Dump();
349
350 template <typename Callback>
351 void ForEach(Callback* callback) { return tree()->ForEach(callback); }
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000352
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000353 private:
354 // There can't be a static empty set since it allocates its
355 // successors in a zone and caches them.
356 OutSet* empty() { return &empty_; }
357 OutSet empty_;
358 ZoneSplayTree<Config>* tree() { return &tree_; }
359 ZoneSplayTree<Config> tree_;
360};
361
362
363#define FOR_EACH_NODE_TYPE(VISIT) \
364 VISIT(End) \
365 VISIT(Action) \
366 VISIT(Choice) \
367 VISIT(BackReference) \
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000368 VISIT(Assertion) \
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000369 VISIT(Text)
370
371
372#define FOR_EACH_REG_EXP_TREE_TYPE(VISIT) \
373 VISIT(Disjunction) \
374 VISIT(Alternative) \
375 VISIT(Assertion) \
376 VISIT(CharacterClass) \
377 VISIT(Atom) \
378 VISIT(Quantifier) \
379 VISIT(Capture) \
380 VISIT(Lookahead) \
381 VISIT(BackReference) \
382 VISIT(Empty) \
383 VISIT(Text)
384
385
386#define FORWARD_DECLARE(Name) class RegExp##Name;
387FOR_EACH_REG_EXP_TREE_TYPE(FORWARD_DECLARE)
388#undef FORWARD_DECLARE
389
390
391class TextElement {
392 public:
393 enum Type {UNINITIALIZED, ATOM, CHAR_CLASS};
394 TextElement() : type(UNINITIALIZED) { }
ager@chromium.org8bb60582008-12-11 12:02:20 +0000395 explicit TextElement(Type t) : type(t), cp_offset(-1) { }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000396 static TextElement Atom(RegExpAtom* atom);
397 static TextElement CharClass(RegExpCharacterClass* char_class);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000398 int length();
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000399 Type type;
400 union {
401 RegExpAtom* u_atom;
402 RegExpCharacterClass* u_char_class;
403 } data;
ager@chromium.org8bb60582008-12-11 12:02:20 +0000404 int cp_offset;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000405};
406
407
ager@chromium.org32912102009-01-16 10:38:43 +0000408class Trace;
ager@chromium.org8bb60582008-12-11 12:02:20 +0000409
410
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000411struct NodeInfo {
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000412 NodeInfo()
413 : being_analyzed(false),
414 been_analyzed(false),
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000415 follows_word_interest(false),
416 follows_newline_interest(false),
417 follows_start_interest(false),
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000418 at_end(false),
danno@chromium.org1044a4d2012-04-30 12:34:39 +0000419 visited(false),
420 replacement_calculated(false) { }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000421
422 // Returns true if the interests and assumptions of this node
423 // matches the given one.
424 bool Matches(NodeInfo* that) {
425 return (at_end == that->at_end) &&
426 (follows_word_interest == that->follows_word_interest) &&
427 (follows_newline_interest == that->follows_newline_interest) &&
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000428 (follows_start_interest == that->follows_start_interest);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000429 }
430
431 // Updates the interests of this node given the interests of the
432 // node preceding it.
433 void AddFromPreceding(NodeInfo* that) {
434 at_end |= that->at_end;
435 follows_word_interest |= that->follows_word_interest;
436 follows_newline_interest |= that->follows_newline_interest;
437 follows_start_interest |= that->follows_start_interest;
438 }
439
ager@chromium.org8bb60582008-12-11 12:02:20 +0000440 bool HasLookbehind() {
441 return follows_word_interest ||
442 follows_newline_interest ||
443 follows_start_interest;
444 }
445
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000446 // Sets the interests of this node to include the interests of the
447 // following node.
448 void AddFromFollowing(NodeInfo* that) {
449 follows_word_interest |= that->follows_word_interest;
450 follows_newline_interest |= that->follows_newline_interest;
451 follows_start_interest |= that->follows_start_interest;
452 }
453
454 void ResetCompilationState() {
455 being_analyzed = false;
456 been_analyzed = false;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000457 }
458
459 bool being_analyzed: 1;
460 bool been_analyzed: 1;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000461
462 // These bits are set of this node has to know what the preceding
463 // character was.
464 bool follows_word_interest: 1;
465 bool follows_newline_interest: 1;
466 bool follows_start_interest: 1;
467
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000468 bool at_end: 1;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000469 bool visited: 1;
danno@chromium.org1044a4d2012-04-30 12:34:39 +0000470 bool replacement_calculated: 1;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000471};
472
473
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000474// Details of a quick mask-compare check that can look ahead in the
475// input stream.
476class QuickCheckDetails {
477 public:
478 QuickCheckDetails()
479 : characters_(0),
480 mask_(0),
iposva@chromium.org245aa852009-02-10 00:49:54 +0000481 value_(0),
482 cannot_match_(false) { }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000483 explicit QuickCheckDetails(int characters)
484 : characters_(characters),
485 mask_(0),
iposva@chromium.org245aa852009-02-10 00:49:54 +0000486 value_(0),
487 cannot_match_(false) { }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000488 bool Rationalize(bool ascii);
489 // Merge in the information from another branch of an alternation.
490 void Merge(QuickCheckDetails* other, int from_index);
491 // Advance the current position by some amount.
492 void Advance(int by, bool ascii);
493 void Clear();
iposva@chromium.org245aa852009-02-10 00:49:54 +0000494 bool cannot_match() { return cannot_match_; }
495 void set_cannot_match() { cannot_match_ = true; }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000496 struct Position {
497 Position() : mask(0), value(0), determines_perfectly(false) { }
498 uc16 mask;
499 uc16 value;
500 bool determines_perfectly;
501 };
502 int characters() { return characters_; }
503 void set_characters(int characters) { characters_ = characters; }
504 Position* positions(int index) {
505 ASSERT(index >= 0);
506 ASSERT(index < characters_);
507 return positions_ + index;
508 }
509 uint32_t mask() { return mask_; }
510 uint32_t value() { return value_; }
511
512 private:
513 // How many characters do we have quick check information from. This is
514 // the same for all branches of a choice node.
515 int characters_;
516 Position positions_[4];
517 // These values are the condensate of the above array after Rationalize().
518 uint32_t mask_;
519 uint32_t value_;
iposva@chromium.org245aa852009-02-10 00:49:54 +0000520 // If set to true, there is no way this quick check can match at all.
521 // E.g., if it requires to be at the start of the input, and isn't.
522 bool cannot_match_;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000523};
524
525
danno@chromium.org1044a4d2012-04-30 12:34:39 +0000526extern int kUninitializedRegExpNodePlaceHolder;
527
528
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000529class RegExpNode: public ZoneObject {
530 public:
danno@chromium.org1044a4d2012-04-30 12:34:39 +0000531 RegExpNode() : replacement_(NULL), trace_count_(0) {
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000532 bm_info_[0] = bm_info_[1] = NULL;
533 }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000534 virtual ~RegExpNode();
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000535 virtual void Accept(NodeVisitor* visitor) = 0;
536 // Generates a goto to this node or actually generates the code at this point.
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000537 virtual void Emit(RegExpCompiler* compiler, Trace* trace) = 0;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000538 // How many characters must this node consume at a minimum in order to
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000539 // succeed. If we have found at least 'still_to_find' characters that
540 // must be consumed there is no need to ask any following nodes whether
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000541 // they are sure to eat any more characters. The not_at_start argument is
542 // used to indicate that we know we are not at the start of the input. In
543 // this case anchored branches will always fail and can be ignored when
544 // determining how many characters are consumed on success.
545 virtual int EatsAtLeast(int still_to_find,
546 int recursion_depth,
547 bool not_at_start) = 0;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000548 // Emits some quick code that checks whether the preloaded characters match.
549 // Falls through on certain failure, jumps to the label on possible success.
550 // If the node cannot make a quick check it does nothing and returns false.
551 bool EmitQuickCheck(RegExpCompiler* compiler,
ager@chromium.org32912102009-01-16 10:38:43 +0000552 Trace* trace,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000553 bool preload_has_checked_bounds,
554 Label* on_possible_success,
555 QuickCheckDetails* details_return,
556 bool fall_through_on_failure);
557 // For a given number of characters this returns a mask and a value. The
558 // next n characters are anded with the mask and compared with the value.
559 // A comparison failure indicates the node cannot match the next n characters.
560 // A comparison success indicates the node may match.
561 virtual void GetQuickCheckDetails(QuickCheckDetails* details,
562 RegExpCompiler* compiler,
iposva@chromium.org245aa852009-02-10 00:49:54 +0000563 int characters_filled_in,
564 bool not_at_start) = 0;
ager@chromium.org8bb60582008-12-11 12:02:20 +0000565 static const int kNodeIsTooComplexForGreedyLoops = -1;
566 virtual int GreedyLoopTextLength() { return kNodeIsTooComplexForGreedyLoops; }
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000567 // Only returns the successor for a text node of length 1 that matches any
568 // character and that has no guards on it.
569 virtual RegExpNode* GetSuccessorOfOmnivorousTextNode(
570 RegExpCompiler* compiler) {
571 return NULL;
572 }
573
574 // Collects information on the possible code units (mod 128) that can match if
575 // we look forward. This is used for a Boyer-Moore-like string searching
576 // implementation. TODO(erikcorry): This should share more code with
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000577 // EatsAtLeast, GetQuickCheckDetails.
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000578 virtual void FillInBMInfo(
579 int offset, BoyerMooreLookahead* bm, bool not_at_start) {
580 UNREACHABLE();
581 }
danno@chromium.org1044a4d2012-04-30 12:34:39 +0000582
583 // If we know that the input is ASCII then there are some nodes that can
584 // never match. This method returns a node that can be substituted for
585 // itself, or NULL if the node can never match.
586 virtual RegExpNode* FilterASCII(int depth) { return this; }
587 // Helper for FilterASCII.
588 RegExpNode* replacement() {
589 ASSERT(info()->replacement_calculated);
590 return replacement_;
591 }
592 RegExpNode* set_replacement(RegExpNode* replacement) {
593 info()->replacement_calculated = true;
594 replacement_ = replacement;
595 return replacement; // For convenience.
596 }
597
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000598 // We want to avoid recalculating the lookahead info, so we store it on the
599 // node. Only info that is for this node is stored. We can tell that the
600 // info is for this node when offset == 0, so the information is calculated
601 // relative to this node.
602 void SaveBMInfo(BoyerMooreLookahead* bm, bool not_at_start, int offset) {
603 if (offset == 0) set_bm_info(not_at_start, bm);
604 }
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000605
ager@chromium.org8bb60582008-12-11 12:02:20 +0000606 Label* label() { return &label_; }
ulan@chromium.org2efb9002012-01-19 15:36:35 +0000607 // 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 +0000608 // start of the trace) then it cannot be reused. This variable sets a limit
609 // on how often we allow that to happen before we insist on starting a new
610 // trace and generating generic code for a node that can be reused by flushing
611 // the deferred actions in the current trace and generating a goto.
612 static const int kMaxCopiesCodeGenerated = 10;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000613
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000614 NodeInfo* info() { return &info_; }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000615
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000616 BoyerMooreLookahead* bm_info(bool not_at_start) {
617 return bm_info_[not_at_start ? 1 : 0];
618 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000619
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000620 protected:
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000621 enum LimitResult { DONE, CONTINUE };
danno@chromium.org1044a4d2012-04-30 12:34:39 +0000622 RegExpNode* replacement_;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000623
ager@chromium.org32912102009-01-16 10:38:43 +0000624 LimitResult LimitVersions(RegExpCompiler* compiler, Trace* trace);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000625
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000626 void set_bm_info(bool not_at_start, BoyerMooreLookahead* bm) {
627 bm_info_[not_at_start ? 1 : 0] = bm;
628 }
629
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000630 private:
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000631 static const int kFirstCharBudget = 10;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000632 Label label_;
633 NodeInfo info_;
ager@chromium.org32912102009-01-16 10:38:43 +0000634 // This variable keeps track of how many times code has been generated for
635 // this node (in different traces). We don't keep track of where the
636 // generated code is located unless the code is generated at the start of
637 // a trace, in which case it is generic and can be reused by flushing the
638 // deferred operations in the current trace and generating a goto.
639 int trace_count_;
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000640 BoyerMooreLookahead* bm_info_[2];
ager@chromium.org32912102009-01-16 10:38:43 +0000641};
642
643
644// A simple closed interval.
645class Interval {
646 public:
647 Interval() : from_(kNone), to_(kNone) { }
648 Interval(int from, int to) : from_(from), to_(to) { }
649 Interval Union(Interval that) {
650 if (that.from_ == kNone)
651 return *this;
652 else if (from_ == kNone)
653 return that;
654 else
655 return Interval(Min(from_, that.from_), Max(to_, that.to_));
656 }
657 bool Contains(int value) {
658 return (from_ <= value) && (value <= to_);
659 }
660 bool is_empty() { return from_ == kNone; }
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000661 int from() const { return from_; }
662 int to() const { return to_; }
ager@chromium.org32912102009-01-16 10:38:43 +0000663 static Interval Empty() { return Interval(); }
664 static const int kNone = -1;
665 private:
666 int from_;
667 int to_;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000668};
669
670
671class SeqRegExpNode: public RegExpNode {
672 public:
673 explicit SeqRegExpNode(RegExpNode* on_success)
674 : on_success_(on_success) { }
675 RegExpNode* on_success() { return on_success_; }
676 void set_on_success(RegExpNode* node) { on_success_ = node; }
danno@chromium.org1044a4d2012-04-30 12:34:39 +0000677 virtual RegExpNode* FilterASCII(int depth);
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000678 virtual void FillInBMInfo(
679 int offset, BoyerMooreLookahead* bm, bool not_at_start) {
680 on_success_->FillInBMInfo(offset, bm, not_at_start);
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000681 if (offset == 0) set_bm_info(not_at_start, bm);
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000682 }
danno@chromium.org1044a4d2012-04-30 12:34:39 +0000683
684 protected:
685 RegExpNode* FilterSuccessor(int depth);
686
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000687 private:
688 RegExpNode* on_success_;
689};
690
691
692class ActionNode: public SeqRegExpNode {
693 public:
694 enum Type {
ager@chromium.org8bb60582008-12-11 12:02:20 +0000695 SET_REGISTER,
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000696 INCREMENT_REGISTER,
697 STORE_POSITION,
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000698 BEGIN_SUBMATCH,
ager@chromium.org32912102009-01-16 10:38:43 +0000699 POSITIVE_SUBMATCH_SUCCESS,
700 EMPTY_MATCH_CHECK,
701 CLEAR_CAPTURES
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000702 };
ager@chromium.org8bb60582008-12-11 12:02:20 +0000703 static ActionNode* SetRegister(int reg, int val, RegExpNode* on_success);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000704 static ActionNode* IncrementRegister(int reg, RegExpNode* on_success);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000705 static ActionNode* StorePosition(int reg,
706 bool is_capture,
707 RegExpNode* on_success);
ager@chromium.org32912102009-01-16 10:38:43 +0000708 static ActionNode* ClearCaptures(Interval range, RegExpNode* on_success);
709 static ActionNode* BeginSubmatch(int stack_pointer_reg,
710 int position_reg,
711 RegExpNode* on_success);
712 static ActionNode* PositiveSubmatchSuccess(int stack_pointer_reg,
713 int restore_reg,
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000714 int clear_capture_count,
715 int clear_capture_from,
ager@chromium.org32912102009-01-16 10:38:43 +0000716 RegExpNode* on_success);
717 static ActionNode* EmptyMatchCheck(int start_register,
718 int repetition_register,
719 int repetition_limit,
720 RegExpNode* on_success);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000721 virtual void Accept(NodeVisitor* visitor);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000722 virtual void Emit(RegExpCompiler* compiler, Trace* trace);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000723 virtual int EatsAtLeast(int still_to_find,
724 int recursion_depth,
725 bool not_at_start);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000726 virtual void GetQuickCheckDetails(QuickCheckDetails* details,
727 RegExpCompiler* compiler,
iposva@chromium.org245aa852009-02-10 00:49:54 +0000728 int filled_in,
729 bool not_at_start) {
730 return on_success()->GetQuickCheckDetails(
731 details, compiler, filled_in, not_at_start);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000732 }
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000733 virtual void FillInBMInfo(
734 int offset, BoyerMooreLookahead* bm, bool not_at_start);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000735 Type type() { return type_; }
736 // TODO(erikcorry): We should allow some action nodes in greedy loops.
737 virtual int GreedyLoopTextLength() { return kNodeIsTooComplexForGreedyLoops; }
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000738
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000739 private:
740 union {
741 struct {
742 int reg;
743 int value;
744 } u_store_register;
745 struct {
746 int reg;
747 } u_increment_register;
748 struct {
749 int reg;
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000750 bool is_capture;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000751 } u_position_register;
752 struct {
753 int stack_pointer_register;
754 int current_position_register;
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000755 int clear_register_count;
756 int clear_register_from;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000757 } u_submatch;
ager@chromium.org32912102009-01-16 10:38:43 +0000758 struct {
759 int start_register;
760 int repetition_register;
761 int repetition_limit;
762 } u_empty_match_check;
763 struct {
764 int range_from;
765 int range_to;
766 } u_clear_captures;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000767 } data_;
768 ActionNode(Type type, RegExpNode* on_success)
769 : SeqRegExpNode(on_success),
770 type_(type) { }
771 Type type_;
772 friend class DotPrinter;
773};
774
775
776class TextNode: public SeqRegExpNode {
777 public:
778 TextNode(ZoneList<TextElement>* elms,
ager@chromium.org8bb60582008-12-11 12:02:20 +0000779 RegExpNode* on_success)
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000780 : SeqRegExpNode(on_success),
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000781 elms_(elms) { }
782 TextNode(RegExpCharacterClass* that,
ager@chromium.org8bb60582008-12-11 12:02:20 +0000783 RegExpNode* on_success)
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000784 : SeqRegExpNode(on_success),
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000785 elms_(new ZoneList<TextElement>(1)) {
786 elms_->Add(TextElement::CharClass(that));
787 }
788 virtual void Accept(NodeVisitor* visitor);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000789 virtual void Emit(RegExpCompiler* compiler, Trace* trace);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000790 virtual int EatsAtLeast(int still_to_find,
791 int recursion_depth,
792 bool not_at_start);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000793 virtual void GetQuickCheckDetails(QuickCheckDetails* details,
794 RegExpCompiler* compiler,
iposva@chromium.org245aa852009-02-10 00:49:54 +0000795 int characters_filled_in,
796 bool not_at_start);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000797 ZoneList<TextElement>* elements() { return elms_; }
ager@chromium.org38e4c712009-11-11 09:11:58 +0000798 void MakeCaseIndependent(bool is_ascii);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000799 virtual int GreedyLoopTextLength();
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000800 virtual RegExpNode* GetSuccessorOfOmnivorousTextNode(
801 RegExpCompiler* compiler);
802 virtual void FillInBMInfo(
803 int offset, BoyerMooreLookahead* bm, bool not_at_start);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000804 void CalculateOffsets();
danno@chromium.org1044a4d2012-04-30 12:34:39 +0000805 virtual RegExpNode* FilterASCII(int depth);
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000806
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000807 private:
808 enum TextEmitPassType {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000809 NON_ASCII_MATCH, // Check for characters that can't match.
810 SIMPLE_CHARACTER_MATCH, // Case-dependent single character check.
811 NON_LETTER_CHARACTER_MATCH, // Check characters that have no case equivs.
812 CASE_CHARACTER_MATCH, // Case-independent single character check.
813 CHARACTER_CLASS_MATCH // Character class.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000814 };
ager@chromium.org381abbb2009-02-25 13:23:22 +0000815 static bool SkipPass(int pass, bool ignore_case);
816 static const int kFirstRealPass = SIMPLE_CHARACTER_MATCH;
817 static const int kLastPass = CHARACTER_CLASS_MATCH;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000818 void TextEmitPass(RegExpCompiler* compiler,
819 TextEmitPassType pass,
820 bool preloaded,
ager@chromium.org32912102009-01-16 10:38:43 +0000821 Trace* trace,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000822 bool first_element_checked,
823 int* checked_up_to);
824 int Length();
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000825 ZoneList<TextElement>* elms_;
826};
827
828
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000829class AssertionNode: public SeqRegExpNode {
830 public:
831 enum AssertionNodeType {
832 AT_END,
833 AT_START,
834 AT_BOUNDARY,
835 AT_NON_BOUNDARY,
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000836 AFTER_NEWLINE
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000837 };
838 static AssertionNode* AtEnd(RegExpNode* on_success) {
839 return new AssertionNode(AT_END, on_success);
840 }
841 static AssertionNode* AtStart(RegExpNode* on_success) {
842 return new AssertionNode(AT_START, on_success);
843 }
844 static AssertionNode* AtBoundary(RegExpNode* on_success) {
845 return new AssertionNode(AT_BOUNDARY, on_success);
846 }
847 static AssertionNode* AtNonBoundary(RegExpNode* on_success) {
848 return new AssertionNode(AT_NON_BOUNDARY, on_success);
849 }
850 static AssertionNode* AfterNewline(RegExpNode* on_success) {
851 return new AssertionNode(AFTER_NEWLINE, on_success);
852 }
853 virtual void Accept(NodeVisitor* visitor);
854 virtual void Emit(RegExpCompiler* compiler, Trace* trace);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000855 virtual int EatsAtLeast(int still_to_find,
856 int recursion_depth,
857 bool not_at_start);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000858 virtual void GetQuickCheckDetails(QuickCheckDetails* details,
859 RegExpCompiler* compiler,
iposva@chromium.org245aa852009-02-10 00:49:54 +0000860 int filled_in,
861 bool not_at_start);
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000862 virtual void FillInBMInfo(
863 int offset, BoyerMooreLookahead* bm, bool not_at_start);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000864 AssertionNodeType type() { return type_; }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000865 void set_type(AssertionNodeType type) { type_ = type; }
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000866
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000867 private:
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000868 void EmitBoundaryCheck(RegExpCompiler* compiler, Trace* trace);
869 enum IfPrevious { kIsNonWord, kIsWord };
870 void BacktrackIfPrevious(RegExpCompiler* compiler,
871 Trace* trace,
872 IfPrevious backtrack_if_previous);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000873 AssertionNode(AssertionNodeType t, RegExpNode* on_success)
874 : SeqRegExpNode(on_success), type_(t) { }
875 AssertionNodeType type_;
876};
877
878
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000879class BackReferenceNode: public SeqRegExpNode {
880 public:
881 BackReferenceNode(int start_reg,
882 int end_reg,
ager@chromium.org8bb60582008-12-11 12:02:20 +0000883 RegExpNode* on_success)
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000884 : SeqRegExpNode(on_success),
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000885 start_reg_(start_reg),
886 end_reg_(end_reg) { }
887 virtual void Accept(NodeVisitor* visitor);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000888 int start_register() { return start_reg_; }
889 int end_register() { return end_reg_; }
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000890 virtual void Emit(RegExpCompiler* compiler, Trace* trace);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000891 virtual int EatsAtLeast(int still_to_find,
892 int recursion_depth,
893 bool not_at_start);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000894 virtual void GetQuickCheckDetails(QuickCheckDetails* details,
895 RegExpCompiler* compiler,
iposva@chromium.org245aa852009-02-10 00:49:54 +0000896 int characters_filled_in,
897 bool not_at_start) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000898 return;
899 }
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000900 virtual void FillInBMInfo(
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000901 int offset, BoyerMooreLookahead* bm, bool not_at_start);
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000902
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000903 private:
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000904 int start_reg_;
905 int end_reg_;
906};
907
908
909class EndNode: public RegExpNode {
910 public:
ager@chromium.org8bb60582008-12-11 12:02:20 +0000911 enum Action { ACCEPT, BACKTRACK, NEGATIVE_SUBMATCH_SUCCESS };
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000912 explicit EndNode(Action action) : action_(action) { }
913 virtual void Accept(NodeVisitor* visitor);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000914 virtual void Emit(RegExpCompiler* compiler, Trace* trace);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000915 virtual int EatsAtLeast(int still_to_find,
916 int recursion_depth,
917 bool not_at_start) { return 0; }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000918 virtual void GetQuickCheckDetails(QuickCheckDetails* details,
919 RegExpCompiler* compiler,
iposva@chromium.org245aa852009-02-10 00:49:54 +0000920 int characters_filled_in,
921 bool not_at_start) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000922 // Returning 0 from EatsAtLeast should ensure we never get here.
923 UNREACHABLE();
924 }
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000925 virtual void FillInBMInfo(
926 int offset, BoyerMooreLookahead* bm, bool not_at_start) {
927 // Returning 0 from EatsAtLeast should ensure we never get here.
928 UNREACHABLE();
929 }
danno@chromium.org1044a4d2012-04-30 12:34:39 +0000930
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000931 private:
932 Action action_;
933};
934
935
ager@chromium.org8bb60582008-12-11 12:02:20 +0000936class NegativeSubmatchSuccess: public EndNode {
937 public:
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000938 NegativeSubmatchSuccess(int stack_pointer_reg,
939 int position_reg,
940 int clear_capture_count,
941 int clear_capture_start)
ager@chromium.org8bb60582008-12-11 12:02:20 +0000942 : EndNode(NEGATIVE_SUBMATCH_SUCCESS),
943 stack_pointer_register_(stack_pointer_reg),
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000944 current_position_register_(position_reg),
945 clear_capture_count_(clear_capture_count),
946 clear_capture_start_(clear_capture_start) { }
947 virtual void Emit(RegExpCompiler* compiler, Trace* trace);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000948
949 private:
950 int stack_pointer_register_;
951 int current_position_register_;
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000952 int clear_capture_count_;
953 int clear_capture_start_;
ager@chromium.org8bb60582008-12-11 12:02:20 +0000954};
955
956
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000957class Guard: public ZoneObject {
958 public:
959 enum Relation { LT, GEQ };
960 Guard(int reg, Relation op, int value)
961 : reg_(reg),
962 op_(op),
963 value_(value) { }
964 int reg() { return reg_; }
965 Relation op() { return op_; }
966 int value() { return value_; }
967
968 private:
969 int reg_;
970 Relation op_;
971 int value_;
972};
973
974
975class GuardedAlternative {
976 public:
977 explicit GuardedAlternative(RegExpNode* node) : node_(node), guards_(NULL) { }
978 void AddGuard(Guard* guard);
979 RegExpNode* node() { return node_; }
980 void set_node(RegExpNode* node) { node_ = node; }
981 ZoneList<Guard*>* guards() { return guards_; }
982
983 private:
984 RegExpNode* node_;
985 ZoneList<Guard*>* guards_;
986};
987
988
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000989class AlternativeGeneration;
990
991
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000992class ChoiceNode: public RegExpNode {
993 public:
ager@chromium.org8bb60582008-12-11 12:02:20 +0000994 explicit ChoiceNode(int expected_size)
995 : alternatives_(new ZoneList<GuardedAlternative>(expected_size)),
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000996 table_(NULL),
iposva@chromium.org245aa852009-02-10 00:49:54 +0000997 not_at_start_(false),
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000998 being_calculated_(false) { }
999 virtual void Accept(NodeVisitor* visitor);
1000 void AddAlternative(GuardedAlternative node) { alternatives()->Add(node); }
1001 ZoneList<GuardedAlternative>* alternatives() { return alternatives_; }
1002 DispatchTable* GetTable(bool ignore_case);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001003 virtual void Emit(RegExpCompiler* compiler, Trace* trace);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001004 virtual int EatsAtLeast(int still_to_find,
1005 int recursion_depth,
1006 bool not_at_start);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001007 int EatsAtLeastHelper(int still_to_find,
1008 int recursion_depth,
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001009 RegExpNode* ignore_this_node,
1010 bool not_at_start);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001011 virtual void GetQuickCheckDetails(QuickCheckDetails* details,
1012 RegExpCompiler* compiler,
iposva@chromium.org245aa852009-02-10 00:49:54 +00001013 int characters_filled_in,
1014 bool not_at_start);
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00001015 virtual void FillInBMInfo(
1016 int offset, BoyerMooreLookahead* bm, bool not_at_start);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001017
1018 bool being_calculated() { return being_calculated_; }
iposva@chromium.org245aa852009-02-10 00:49:54 +00001019 bool not_at_start() { return not_at_start_; }
1020 void set_not_at_start() { not_at_start_ = true; }
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001021 void set_being_calculated(bool b) { being_calculated_ = b; }
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001022 virtual bool try_to_emit_quick_check_for_alternative(int i) { return true; }
danno@chromium.org1044a4d2012-04-30 12:34:39 +00001023 virtual RegExpNode* FilterASCII(int depth);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001024
ager@chromium.org8bb60582008-12-11 12:02:20 +00001025 protected:
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001026 int GreedyLoopTextLengthForAlternative(GuardedAlternative* alternative);
ager@chromium.org8bb60582008-12-11 12:02:20 +00001027 ZoneList<GuardedAlternative>* alternatives_;
1028
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001029 private:
1030 friend class DispatchTableConstructor;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001031 friend class Analysis;
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001032 void GenerateGuard(RegExpMacroAssembler* macro_assembler,
ager@chromium.org5ec48922009-05-05 07:25:34 +00001033 Guard* guard,
ager@chromium.org32912102009-01-16 10:38:43 +00001034 Trace* trace);
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00001035 int CalculatePreloadCharacters(RegExpCompiler* compiler, int eats_at_least);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001036 void EmitOutOfLineContinuation(RegExpCompiler* compiler,
ager@chromium.org32912102009-01-16 10:38:43 +00001037 Trace* trace,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001038 GuardedAlternative alternative,
1039 AlternativeGeneration* alt_gen,
1040 int preload_characters,
1041 bool next_expects_preload);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001042 DispatchTable* table_;
iposva@chromium.org245aa852009-02-10 00:49:54 +00001043 // If true, this node is never checked at the start of the input.
1044 // Allows a new trace to start with at_start() set to false.
1045 bool not_at_start_;
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001046 bool being_calculated_;
1047};
1048
1049
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001050class NegativeLookaheadChoiceNode: public ChoiceNode {
1051 public:
1052 explicit NegativeLookaheadChoiceNode(GuardedAlternative this_must_fail,
1053 GuardedAlternative then_do_this)
1054 : ChoiceNode(2) {
1055 AddAlternative(this_must_fail);
1056 AddAlternative(then_do_this);
1057 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001058 virtual int EatsAtLeast(int still_to_find,
1059 int recursion_depth,
1060 bool not_at_start);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001061 virtual void GetQuickCheckDetails(QuickCheckDetails* details,
1062 RegExpCompiler* compiler,
iposva@chromium.org245aa852009-02-10 00:49:54 +00001063 int characters_filled_in,
1064 bool not_at_start);
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00001065 virtual void FillInBMInfo(
1066 int offset, BoyerMooreLookahead* bm, bool not_at_start) {
1067 alternatives_->at(1).node()->FillInBMInfo(offset, bm, not_at_start);
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00001068 if (offset == 0) set_bm_info(not_at_start, bm);
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00001069 }
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001070 // For a negative lookahead we don't emit the quick check for the
1071 // alternative that is expected to fail. This is because quick check code
1072 // starts by loading enough characters for the alternative that takes fewest
1073 // characters, but on a negative lookahead the negative branch did not take
1074 // part in that calculation (EatsAtLeast) so the assumptions don't hold.
1075 virtual bool try_to_emit_quick_check_for_alternative(int i) { return i != 0; }
danno@chromium.org1044a4d2012-04-30 12:34:39 +00001076 virtual RegExpNode* FilterASCII(int depth);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001077};
1078
1079
ager@chromium.org8bb60582008-12-11 12:02:20 +00001080class LoopChoiceNode: public ChoiceNode {
1081 public:
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001082 explicit LoopChoiceNode(bool body_can_be_zero_length)
1083 : ChoiceNode(2),
1084 loop_node_(NULL),
1085 continue_node_(NULL),
1086 body_can_be_zero_length_(body_can_be_zero_length) { }
1087 void AddLoopAlternative(GuardedAlternative alt);
1088 void AddContinueAlternative(GuardedAlternative alt);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001089 virtual void Emit(RegExpCompiler* compiler, Trace* trace);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001090 virtual int EatsAtLeast(int still_to_find,
1091 int recursion_depth,
1092 bool not_at_start);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001093 virtual void GetQuickCheckDetails(QuickCheckDetails* details,
1094 RegExpCompiler* compiler,
iposva@chromium.org245aa852009-02-10 00:49:54 +00001095 int characters_filled_in,
1096 bool not_at_start);
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00001097 virtual void FillInBMInfo(
1098 int offset, BoyerMooreLookahead* bm, bool not_at_start);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001099 RegExpNode* loop_node() { return loop_node_; }
1100 RegExpNode* continue_node() { return continue_node_; }
1101 bool body_can_be_zero_length() { return body_can_be_zero_length_; }
1102 virtual void Accept(NodeVisitor* visitor);
danno@chromium.org1044a4d2012-04-30 12:34:39 +00001103 virtual RegExpNode* FilterASCII(int depth);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001104
1105 private:
1106 // AddAlternative is made private for loop nodes because alternatives
1107 // should not be added freely, we need to keep track of which node
1108 // goes back to the node itself.
1109 void AddAlternative(GuardedAlternative node) {
1110 ChoiceNode::AddAlternative(node);
1111 }
1112
1113 RegExpNode* loop_node_;
1114 RegExpNode* continue_node_;
1115 bool body_can_be_zero_length_;
ager@chromium.org8bb60582008-12-11 12:02:20 +00001116};
1117
1118
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00001119// Improve the speed that we scan for an initial point where a non-anchored
1120// regexp can match by using a Boyer-Moore-like table. This is done by
1121// identifying non-greedy non-capturing loops in the nodes that eat any
1122// character one at a time. For example in the middle of the regexp
1123// /foo[\s\S]*?bar/ we find such a loop. There is also such a loop implicitly
1124// inserted at the start of any non-anchored regexp.
1125//
1126// When we have found such a loop we look ahead in the nodes to find the set of
1127// characters that can come at given distances. For example for the regexp
1128// /.?foo/ we know that there are at least 3 characters ahead of us, and the
1129// sets of characters that can occur are [any, [f, o], [o]]. We find a range in
1130// the lookahead info where the set of characters is reasonably constrained. In
1131// our example this is from index 1 to 2 (0 is not constrained). We can now
1132// look 3 characters ahead and if we don't find one of [f, o] (the union of
1133// [f, o] and [o]) then we can skip forwards by the range size (in this case 2).
1134//
1135// For Unicode input strings we do the same, but modulo 128.
1136//
1137// We also look at the first string fed to the regexp and use that to get a hint
1138// of the character frequencies in the inputs. This affects the assessment of
1139// whether the set of characters is 'reasonably constrained'.
1140//
1141// We also have another lookahead mechanism (called quick check in the code),
1142// which uses a wide load of multiple characters followed by a mask and compare
1143// to determine whether a match is possible at this point.
1144enum ContainedInLattice {
1145 kNotYet = 0,
1146 kLatticeIn = 1,
1147 kLatticeOut = 2,
1148 kLatticeUnknown = 3 // Can also mean both in and out.
1149};
1150
1151
1152inline ContainedInLattice Combine(ContainedInLattice a, ContainedInLattice b) {
1153 return static_cast<ContainedInLattice>(a | b);
1154}
1155
1156
1157ContainedInLattice AddRange(ContainedInLattice a,
1158 const int* ranges,
1159 int ranges_size,
1160 Interval new_range);
1161
1162
1163class BoyerMoorePositionInfo : public ZoneObject {
1164 public:
1165 BoyerMoorePositionInfo()
1166 : map_(new ZoneList<bool>(kMapSize)),
1167 map_count_(0),
1168 w_(kNotYet),
1169 s_(kNotYet),
1170 d_(kNotYet),
1171 surrogate_(kNotYet) {
1172 for (int i = 0; i < kMapSize; i++) {
1173 map_->Add(false);
1174 }
1175 }
1176
1177 bool& at(int i) { return map_->at(i); }
1178
1179 static const int kMapSize = 128;
1180 static const int kMask = kMapSize - 1;
1181
1182 int map_count() const { return map_count_; }
1183
1184 void Set(int character);
1185 void SetInterval(const Interval& interval);
1186 void SetAll();
1187 bool is_non_word() { return w_ == kLatticeOut; }
1188 bool is_word() { return w_ == kLatticeIn; }
1189
1190 private:
1191 ZoneList<bool>* map_;
1192 int map_count_; // Number of set bits in the map.
1193 ContainedInLattice w_; // The \w character class.
1194 ContainedInLattice s_; // The \s character class.
1195 ContainedInLattice d_; // The \d character class.
1196 ContainedInLattice surrogate_; // Surrogate UTF-16 code units.
1197};
1198
1199
1200class BoyerMooreLookahead : public ZoneObject {
1201 public:
1202 BoyerMooreLookahead(int length, RegExpCompiler* compiler);
1203
1204 int length() { return length_; }
1205 int max_char() { return max_char_; }
1206 RegExpCompiler* compiler() { return compiler_; }
1207
1208 int Count(int map_number) {
1209 return bitmaps_->at(map_number)->map_count();
1210 }
1211
1212 BoyerMoorePositionInfo* at(int i) { return bitmaps_->at(i); }
1213
1214 void Set(int map_number, int character) {
1215 if (character > max_char_) return;
1216 BoyerMoorePositionInfo* info = bitmaps_->at(map_number);
1217 info->Set(character);
1218 }
1219
1220 void SetInterval(int map_number, const Interval& interval) {
1221 if (interval.from() > max_char_) return;
1222 BoyerMoorePositionInfo* info = bitmaps_->at(map_number);
1223 if (interval.to() > max_char_) {
1224 info->SetInterval(Interval(interval.from(), max_char_));
1225 } else {
1226 info->SetInterval(interval);
1227 }
1228 }
1229
1230 void SetAll(int map_number) {
1231 bitmaps_->at(map_number)->SetAll();
1232 }
1233
1234 void SetRest(int from_map) {
1235 for (int i = from_map; i < length_; i++) SetAll(i);
1236 }
1237 bool EmitSkipInstructions(RegExpMacroAssembler* masm);
1238
1239 private:
1240 // This is the value obtained by EatsAtLeast. If we do not have at least this
1241 // many characters left in the sample string then the match is bound to fail.
1242 // Therefore it is OK to read a character this far ahead of the current match
1243 // point.
1244 int length_;
1245 RegExpCompiler* compiler_;
1246 // 0x7f for ASCII, 0xffff for UTF-16.
1247 int max_char_;
1248 ZoneList<BoyerMoorePositionInfo*>* bitmaps_;
1249
1250 int GetSkipTable(int min_lookahead,
1251 int max_lookahead,
1252 Handle<ByteArray> boolean_skip_table);
1253 bool FindWorthwhileInterval(int* from, int* to);
1254 int FindBestInterval(
1255 int max_number_of_chars, int old_biggest_points, int* from, int* to);
1256};
1257
1258
ager@chromium.org8bb60582008-12-11 12:02:20 +00001259// There are many ways to generate code for a node. This class encapsulates
1260// the current way we should be generating. In other words it encapsulates
ager@chromium.org32912102009-01-16 10:38:43 +00001261// the current state of the code generator. The effect of this is that we
1262// generate code for paths that the matcher can take through the regular
1263// expression. A given node in the regexp can be code-generated several times
1264// as it can be part of several traces. For example for the regexp:
1265// /foo(bar|ip)baz/ the code to match baz will be generated twice, once as part
1266// of the foo-bar-baz trace and once as part of the foo-ip-baz trace. The code
1267// to match foo is generated only once (the traces have a common prefix). The
1268// code to store the capture is deferred and generated (twice) after the places
1269// where baz has been matched.
1270class Trace {
ager@chromium.org8bb60582008-12-11 12:02:20 +00001271 public:
iposva@chromium.org245aa852009-02-10 00:49:54 +00001272 // A value for a property that is either known to be true, know to be false,
1273 // or not known.
1274 enum TriBool {
1275 UNKNOWN = -1, FALSE = 0, TRUE = 1
1276 };
1277
ager@chromium.org8bb60582008-12-11 12:02:20 +00001278 class DeferredAction {
1279 public:
1280 DeferredAction(ActionNode::Type type, int reg)
1281 : type_(type), reg_(reg), next_(NULL) { }
1282 DeferredAction* next() { return next_; }
ager@chromium.org32912102009-01-16 10:38:43 +00001283 bool Mentions(int reg);
ager@chromium.org8bb60582008-12-11 12:02:20 +00001284 int reg() { return reg_; }
1285 ActionNode::Type type() { return type_; }
1286 private:
1287 ActionNode::Type type_;
1288 int reg_;
1289 DeferredAction* next_;
ager@chromium.org32912102009-01-16 10:38:43 +00001290 friend class Trace;
ager@chromium.org8bb60582008-12-11 12:02:20 +00001291 };
1292
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001293 class DeferredCapture : public DeferredAction {
ager@chromium.org8bb60582008-12-11 12:02:20 +00001294 public:
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001295 DeferredCapture(int reg, bool is_capture, Trace* trace)
ager@chromium.org8bb60582008-12-11 12:02:20 +00001296 : DeferredAction(ActionNode::STORE_POSITION, reg),
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001297 cp_offset_(trace->cp_offset()),
1298 is_capture_(is_capture) { }
ager@chromium.org8bb60582008-12-11 12:02:20 +00001299 int cp_offset() { return cp_offset_; }
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001300 bool is_capture() { return is_capture_; }
ager@chromium.org8bb60582008-12-11 12:02:20 +00001301 private:
1302 int cp_offset_;
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001303 bool is_capture_;
ager@chromium.org8bb60582008-12-11 12:02:20 +00001304 void set_cp_offset(int cp_offset) { cp_offset_ = cp_offset; }
1305 };
1306
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001307 class DeferredSetRegister : public DeferredAction {
ager@chromium.org8bb60582008-12-11 12:02:20 +00001308 public:
1309 DeferredSetRegister(int reg, int value)
1310 : DeferredAction(ActionNode::SET_REGISTER, reg),
1311 value_(value) { }
1312 int value() { return value_; }
1313 private:
1314 int value_;
1315 };
1316
ager@chromium.org32912102009-01-16 10:38:43 +00001317 class DeferredClearCaptures : public DeferredAction {
1318 public:
1319 explicit DeferredClearCaptures(Interval range)
1320 : DeferredAction(ActionNode::CLEAR_CAPTURES, -1),
1321 range_(range) { }
1322 Interval range() { return range_; }
1323 private:
1324 Interval range_;
1325 };
1326
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001327 class DeferredIncrementRegister : public DeferredAction {
ager@chromium.org8bb60582008-12-11 12:02:20 +00001328 public:
1329 explicit DeferredIncrementRegister(int reg)
1330 : DeferredAction(ActionNode::INCREMENT_REGISTER, reg) { }
1331 };
1332
ager@chromium.org32912102009-01-16 10:38:43 +00001333 Trace()
ager@chromium.org8bb60582008-12-11 12:02:20 +00001334 : cp_offset_(0),
1335 actions_(NULL),
1336 backtrack_(NULL),
1337 stop_node_(NULL),
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001338 loop_label_(NULL),
1339 characters_preloaded_(0),
iposva@chromium.org245aa852009-02-10 00:49:54 +00001340 bound_checked_up_to_(0),
ager@chromium.org381abbb2009-02-25 13:23:22 +00001341 flush_budget_(100),
iposva@chromium.org245aa852009-02-10 00:49:54 +00001342 at_start_(UNKNOWN) { }
1343
ager@chromium.org32912102009-01-16 10:38:43 +00001344 // End the trace. This involves flushing the deferred actions in the trace
1345 // and pushing a backtrack location onto the backtrack stack. Once this is
1346 // done we can start a new trace or go to one that has already been
1347 // generated.
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001348 void Flush(RegExpCompiler* compiler, RegExpNode* successor);
ager@chromium.org8bb60582008-12-11 12:02:20 +00001349 int cp_offset() { return cp_offset_; }
1350 DeferredAction* actions() { return actions_; }
ager@chromium.org32912102009-01-16 10:38:43 +00001351 // A trivial trace is one that has no deferred actions or other state that
1352 // affects the assumptions used when generating code. There is no recorded
1353 // backtrack location in a trivial trace, so with a trivial trace we will
1354 // generate code that, on a failure to match, gets the backtrack location
1355 // from the backtrack stack rather than using a direct jump instruction. We
1356 // always start code generation with a trivial trace and non-trivial traces
1357 // are created as we emit code for nodes or add to the list of deferred
1358 // actions in the trace. The location of the code generated for a node using
1359 // a trivial trace is recorded in a label in the node so that gotos can be
1360 // generated to that code.
ager@chromium.org8bb60582008-12-11 12:02:20 +00001361 bool is_trivial() {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001362 return backtrack_ == NULL &&
1363 actions_ == NULL &&
1364 cp_offset_ == 0 &&
1365 characters_preloaded_ == 0 &&
1366 bound_checked_up_to_ == 0 &&
iposva@chromium.org245aa852009-02-10 00:49:54 +00001367 quick_check_performed_.characters() == 0 &&
1368 at_start_ == UNKNOWN;
ager@chromium.org8bb60582008-12-11 12:02:20 +00001369 }
iposva@chromium.org245aa852009-02-10 00:49:54 +00001370 TriBool at_start() { return at_start_; }
1371 void set_at_start(bool at_start) { at_start_ = at_start ? TRUE : FALSE; }
ager@chromium.org8bb60582008-12-11 12:02:20 +00001372 Label* backtrack() { return backtrack_; }
1373 Label* loop_label() { return loop_label_; }
1374 RegExpNode* stop_node() { return stop_node_; }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001375 int characters_preloaded() { return characters_preloaded_; }
1376 int bound_checked_up_to() { return bound_checked_up_to_; }
ager@chromium.org381abbb2009-02-25 13:23:22 +00001377 int flush_budget() { return flush_budget_; }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001378 QuickCheckDetails* quick_check_performed() { return &quick_check_performed_; }
1379 bool mentions_reg(int reg);
ager@chromium.org32912102009-01-16 10:38:43 +00001380 // Returns true if a deferred position store exists to the specified
1381 // register and stores the offset in the out-parameter. Otherwise
1382 // returns false.
1383 bool GetStoredPosition(int reg, int* cp_offset);
1384 // These set methods and AdvanceCurrentPositionInTrace should be used only on
1385 // new traces - the intention is that traces are immutable after creation.
ager@chromium.org8bb60582008-12-11 12:02:20 +00001386 void add_action(DeferredAction* new_action) {
1387 ASSERT(new_action->next_ == NULL);
1388 new_action->next_ = actions_;
1389 actions_ = new_action;
1390 }
ager@chromium.org8bb60582008-12-11 12:02:20 +00001391 void set_backtrack(Label* backtrack) { backtrack_ = backtrack; }
1392 void set_stop_node(RegExpNode* node) { stop_node_ = node; }
1393 void set_loop_label(Label* label) { loop_label_ = label; }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001394 void set_characters_preloaded(int count) { characters_preloaded_ = count; }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001395 void set_bound_checked_up_to(int to) { bound_checked_up_to_ = to; }
ager@chromium.org381abbb2009-02-25 13:23:22 +00001396 void set_flush_budget(int to) { flush_budget_ = to; }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001397 void set_quick_check_performed(QuickCheckDetails* d) {
1398 quick_check_performed_ = *d;
1399 }
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001400 void InvalidateCurrentCharacter();
1401 void AdvanceCurrentPositionInTrace(int by, RegExpCompiler* compiler);
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00001402
ager@chromium.org8bb60582008-12-11 12:02:20 +00001403 private:
1404 int FindAffectedRegisters(OutSet* affected_registers);
1405 void PerformDeferredActions(RegExpMacroAssembler* macro,
1406 int max_register,
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001407 OutSet& affected_registers,
1408 OutSet* registers_to_pop,
1409 OutSet* registers_to_clear);
ager@chromium.org8bb60582008-12-11 12:02:20 +00001410 void RestoreAffectedRegisters(RegExpMacroAssembler* macro,
1411 int max_register,
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001412 OutSet& registers_to_pop,
1413 OutSet& registers_to_clear);
ager@chromium.org8bb60582008-12-11 12:02:20 +00001414 int cp_offset_;
1415 DeferredAction* actions_;
1416 Label* backtrack_;
1417 RegExpNode* stop_node_;
1418 Label* loop_label_;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001419 int characters_preloaded_;
1420 int bound_checked_up_to_;
1421 QuickCheckDetails quick_check_performed_;
ager@chromium.org381abbb2009-02-25 13:23:22 +00001422 int flush_budget_;
iposva@chromium.org245aa852009-02-10 00:49:54 +00001423 TriBool at_start_;
ager@chromium.org8bb60582008-12-11 12:02:20 +00001424};
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001425
1426
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001427class NodeVisitor {
1428 public:
1429 virtual ~NodeVisitor() { }
1430#define DECLARE_VISIT(Type) \
1431 virtual void Visit##Type(Type##Node* that) = 0;
1432FOR_EACH_NODE_TYPE(DECLARE_VISIT)
1433#undef DECLARE_VISIT
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001434 virtual void VisitLoopChoice(LoopChoiceNode* that) { VisitChoice(that); }
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001435};
1436
1437
1438// Node visitor used to add the start set of the alternatives to the
1439// dispatch table of a choice node.
1440class DispatchTableConstructor: public NodeVisitor {
1441 public:
1442 DispatchTableConstructor(DispatchTable* table, bool ignore_case)
1443 : table_(table),
1444 choice_index_(-1),
1445 ignore_case_(ignore_case) { }
1446
1447 void BuildTable(ChoiceNode* node);
1448
1449 void AddRange(CharacterRange range) {
1450 table()->AddRange(range, choice_index_);
1451 }
1452
1453 void AddInverse(ZoneList<CharacterRange>* ranges);
1454
1455#define DECLARE_VISIT(Type) \
1456 virtual void Visit##Type(Type##Node* that);
1457FOR_EACH_NODE_TYPE(DECLARE_VISIT)
1458#undef DECLARE_VISIT
1459
1460 DispatchTable* table() { return table_; }
1461 void set_choice_index(int value) { choice_index_ = value; }
1462
1463 protected:
ager@chromium.org5ec48922009-05-05 07:25:34 +00001464 DispatchTable* table_;
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001465 int choice_index_;
1466 bool ignore_case_;
1467};
1468
1469
ager@chromium.org8bb60582008-12-11 12:02:20 +00001470// Assertion propagation moves information about assertions such as
1471// \b to the affected nodes. For instance, in /.\b./ information must
1472// be propagated to the first '.' that whatever follows needs to know
1473// if it matched a word or a non-word, and to the second '.' that it
1474// has to check if it succeeds a word or non-word. In this case the
1475// result will be something like:
1476//
1477// +-------+ +------------+
1478// | . | | . |
1479// +-------+ ---> +------------+
1480// | word? | | check word |
1481// +-------+ +------------+
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001482class Analysis: public NodeVisitor {
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001483 public:
ager@chromium.org38e4c712009-11-11 09:11:58 +00001484 Analysis(bool ignore_case, bool is_ascii)
1485 : ignore_case_(ignore_case),
1486 is_ascii_(is_ascii),
1487 error_message_(NULL) { }
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001488 void EnsureAnalyzed(RegExpNode* node);
1489
1490#define DECLARE_VISIT(Type) \
1491 virtual void Visit##Type(Type##Node* that);
1492FOR_EACH_NODE_TYPE(DECLARE_VISIT)
1493#undef DECLARE_VISIT
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001494 virtual void VisitLoopChoice(LoopChoiceNode* that);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001495
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00001496 bool has_failed() { return error_message_ != NULL; }
1497 const char* error_message() {
1498 ASSERT(error_message_ != NULL);
1499 return error_message_;
1500 }
1501 void fail(const char* error_message) {
1502 error_message_ = error_message;
1503 }
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00001504
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001505 private:
1506 bool ignore_case_;
ager@chromium.org38e4c712009-11-11 09:11:58 +00001507 bool is_ascii_;
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00001508 const char* error_message_;
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001509
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001510 DISALLOW_IMPLICIT_CONSTRUCTORS(Analysis);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001511};
1512
1513
ager@chromium.org8bb60582008-12-11 12:02:20 +00001514struct RegExpCompileData {
1515 RegExpCompileData()
1516 : tree(NULL),
1517 node(NULL),
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001518 simple(true),
iposva@chromium.org245aa852009-02-10 00:49:54 +00001519 contains_anchor(false),
ager@chromium.org8bb60582008-12-11 12:02:20 +00001520 capture_count(0) { }
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001521 RegExpTree* tree;
ager@chromium.org8bb60582008-12-11 12:02:20 +00001522 RegExpNode* node;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001523 bool simple;
iposva@chromium.org245aa852009-02-10 00:49:54 +00001524 bool contains_anchor;
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001525 Handle<String> error;
1526 int capture_count;
1527};
1528
1529
1530class RegExpEngine: public AllStatic {
1531 public:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001532 struct CompilationResult {
1533 explicit CompilationResult(const char* error_message)
1534 : error_message(error_message),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001535 code(HEAP->the_hole_value()),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001536 num_registers(0) {}
1537 CompilationResult(Object* code, int registers)
1538 : error_message(NULL),
1539 code(code),
1540 num_registers(registers) {}
1541 const char* error_message;
1542 Object* code;
1543 int num_registers;
1544 };
1545
1546 static CompilationResult Compile(RegExpCompileData* input,
1547 bool ignore_case,
1548 bool multiline,
1549 Handle<String> pattern,
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00001550 Handle<String> sample_subject,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001551 bool is_ascii);
ager@chromium.org8bb60582008-12-11 12:02:20 +00001552
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001553 static void DotPrint(const char* label, RegExpNode* node, bool ignore_case);
1554};
1555
1556
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001557class OffsetsVector {
1558 public:
ulan@chromium.org812308e2012-02-29 15:58:45 +00001559 inline OffsetsVector(int num_registers, Isolate* isolate)
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001560 : offsets_vector_length_(num_registers) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001561 if (offsets_vector_length_ > Isolate::kJSRegexpStaticOffsetsVectorSize) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001562 vector_ = NewArray<int>(offsets_vector_length_);
1563 } else {
ulan@chromium.org812308e2012-02-29 15:58:45 +00001564 vector_ = isolate->jsregexp_static_offsets_vector();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001565 }
1566 }
1567 inline ~OffsetsVector() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001568 if (offsets_vector_length_ > Isolate::kJSRegexpStaticOffsetsVectorSize) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001569 DeleteArray(vector_);
1570 vector_ = NULL;
1571 }
1572 }
1573 inline int* vector() { return vector_; }
1574 inline int length() { return offsets_vector_length_; }
1575
1576 static const int kStaticOffsetsVectorSize = 50;
1577
1578 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001579 static Address static_offsets_vector_address(Isolate* isolate) {
1580 return reinterpret_cast<Address>(isolate->jsregexp_static_offsets_vector());
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001581 }
1582
1583 int* vector_;
1584 int offsets_vector_length_;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001585
1586 friend class ExternalReference;
1587};
1588
1589
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001590} } // namespace v8::internal
1591
1592#endif // V8_JSREGEXP_H_