blob: 8875de9eb2316cb27511cd7b9a9408bc08fd4f41 [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;
ager@chromium.orga74f0da2008-12-03 16:05:52 +000043
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000044class RegExpImpl {
45 public:
kasperl@chromium.org68ac0092009-07-09 06:00:35 +000046 // Whether V8 is compiled with native regexp support or not.
47 static bool UsesNativeRegExp() {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000048#ifdef V8_INTERPRETED_REGEXP
kasperl@chromium.org68ac0092009-07-09 06:00:35 +000049 return false;
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000050#else
51 return true;
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000052#endif
53 }
kasperl@chromium.org68ac0092009-07-09 06:00:35 +000054
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000055 // Creates a regular expression literal in the old space.
56 // This function calls the garbage collector if necessary.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000057 static Handle<Object> CreateRegExpLiteral(Handle<JSFunction> constructor,
58 Handle<String> pattern,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000059 Handle<String> flags,
60 bool* has_pending_exception);
61
62 // Returns a string representation of a regular expression.
63 // Implements RegExp.prototype.toString, see ECMA-262 section 15.10.6.4.
64 // This function calls the garbage collector if necessary.
65 static Handle<String> ToString(Handle<Object> value);
66
ager@chromium.org8bb60582008-12-11 12:02:20 +000067 // Parses the RegExp pattern and prepares the JSRegExp object with
68 // generic data and choice of implementation - as well as what
69 // the implementation wants to store in the data field.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000070 // Returns false if compilation fails.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000071 static Handle<Object> Compile(Handle<JSRegExp> re,
72 Handle<String> pattern,
73 Handle<String> flags);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000074
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000075 // See ECMA-262 section 15.10.6.2.
76 // This function calls the garbage collector if necessary.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000077 static Handle<Object> Exec(Handle<JSRegExp> regexp,
78 Handle<String> subject,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000079 int index,
80 Handle<JSArray> lastMatchInfo);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000081
ager@chromium.org8bb60582008-12-11 12:02:20 +000082 // Prepares a JSRegExp object with Irregexp-specific data.
whesse@chromium.orgcec079d2010-03-22 14:44:04 +000083 static void IrregexpInitialize(Handle<JSRegExp> re,
84 Handle<String> pattern,
85 JSRegExp::Flags flags,
86 int capture_register_count);
ager@chromium.orga74f0da2008-12-03 16:05:52 +000087
88
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000089 static void AtomCompile(Handle<JSRegExp> re,
90 Handle<String> pattern,
91 JSRegExp::Flags flags,
92 Handle<String> match_pattern);
93
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000094 static Handle<Object> AtomExec(Handle<JSRegExp> regexp,
95 Handle<String> subject,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000096 int index,
97 Handle<JSArray> lastMatchInfo);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000098
whesse@chromium.orgcec079d2010-03-22 14:44:04 +000099 enum IrregexpResult { RE_FAILURE = 0, RE_SUCCESS = 1, RE_EXCEPTION = -1 };
100
101 // Prepare a RegExp for being executed one or more times (using
102 // IrregexpExecOnce) on the subject.
103 // This ensures that the regexp is compiled for the subject, and that
104 // the subject is flat.
105 // Returns the number of integer spaces required by IrregexpExecOnce
106 // as its "registers" argument. If the regexp cannot be compiled,
107 // an exception is set as pending, and this function returns negative.
108 static int IrregexpPrepare(Handle<JSRegExp> regexp,
109 Handle<String> subject);
110
111 // Execute a regular expression once on the subject, starting from
112 // character "index".
113 // If successful, returns RE_SUCCESS and set the capture positions
114 // in the first registers.
115 // If matching fails, returns RE_FAILURE.
116 // If execution fails, sets a pending exception and returns RE_EXCEPTION.
117 static IrregexpResult IrregexpExecOnce(Handle<JSRegExp> regexp,
118 Handle<String> subject,
119 int index,
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000120 Vector<int> registers);
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000121
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000122 // Execute an Irregexp bytecode pattern.
ager@chromium.org41826e72009-03-30 13:30:57 +0000123 // On a successful match, the result is a JSArray containing
124 // captured positions. On a failure, the result is the null value.
125 // Returns an empty handle in case of an exception.
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000126 static Handle<Object> IrregexpExec(Handle<JSRegExp> regexp,
127 Handle<String> subject,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000128 int index,
129 Handle<JSArray> lastMatchInfo);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000130
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000131 // Array index in the lastMatchInfo array.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000132 static const int kLastCaptureCount = 0;
133 static const int kLastSubject = 1;
134 static const int kLastInput = 2;
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000135 static const int kFirstCapture = 3;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000136 static const int kLastMatchOverhead = 3;
137
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000138 // Direct offset into the lastMatchInfo array.
139 static const int kLastCaptureCountOffset =
140 FixedArray::kHeaderSize + kLastCaptureCount * kPointerSize;
141 static const int kLastSubjectOffset =
142 FixedArray::kHeaderSize + kLastSubject * kPointerSize;
143 static const int kLastInputOffset =
144 FixedArray::kHeaderSize + kLastInput * kPointerSize;
145 static const int kFirstCaptureOffset =
146 FixedArray::kHeaderSize + kFirstCapture * kPointerSize;
147
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000148 // Used to access the lastMatchInfo array.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000149 static int GetCapture(FixedArray* array, int index) {
150 return Smi::cast(array->get(index + kFirstCapture))->value();
151 }
152
153 static void SetLastCaptureCount(FixedArray* array, int to) {
154 array->set(kLastCaptureCount, Smi::FromInt(to));
155 }
156
157 static void SetLastSubject(FixedArray* array, String* to) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000158 array->set(kLastSubject, to);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000159 }
160
161 static void SetLastInput(FixedArray* array, String* to) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000162 array->set(kLastInput, to);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000163 }
164
165 static void SetCapture(FixedArray* array, int index, int to) {
166 array->set(index + kFirstCapture, Smi::FromInt(to));
167 }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000168
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000169 static int GetLastCaptureCount(FixedArray* array) {
170 return Smi::cast(array->get(kLastCaptureCount))->value();
171 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000172
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000173 // For acting on the JSRegExp data FixedArray.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000174 static int IrregexpMaxRegisterCount(FixedArray* re);
175 static void SetIrregexpMaxRegisterCount(FixedArray* re, int value);
176 static int IrregexpNumberOfCaptures(FixedArray* re);
177 static int IrregexpNumberOfRegisters(FixedArray* re);
178 static ByteArray* IrregexpByteCode(FixedArray* re, bool is_ascii);
179 static Code* IrregexpNativeCode(FixedArray* re, bool is_ascii);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000180
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000181 // Limit the space regexps take up on the heap. In order to limit this we
182 // would like to keep track of the amount of regexp code on the heap. This
183 // is not tracked, however. As a conservative approximation we track the
184 // total regexp code compiled including code that has subsequently been freed
185 // and the total executable memory at any point.
186 static const int kRegExpExecutableMemoryLimit = 16 * MB;
187 static const int kRegWxpCompiledLimit = 1 * MB;
188
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000189 private:
190 static String* last_ascii_string_;
191 static String* two_byte_cached_string_;
192
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000193 static bool CompileIrregexp(Handle<JSRegExp> re, bool is_ascii);
194 static inline bool EnsureCompiledIrregexp(Handle<JSRegExp> re, bool is_ascii);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000195
196
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000197 // Set the subject cache. The previous string buffer is not deleted, so the
198 // caller should ensure that it doesn't leak.
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000199 static void SetSubjectCache(String* subject,
200 char* utf8_subject,
201 int uft8_length,
202 int character_position,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000203 int utf8_position);
204
205 // A one element cache of the last utf8_subject string and its length. The
206 // subject JS String object is cached in the heap. We also cache a
207 // translation between position and utf8 position.
208 static char* utf8_subject_cache_;
209 static int utf8_length_cache_;
210 static int utf8_position_;
211 static int character_position_;
212};
213
214
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000215// Represents the location of one element relative to the intersection of
216// two sets. Corresponds to the four areas of a Venn diagram.
217enum ElementInSetsRelation {
218 kInsideNone = 0,
219 kInsideFirst = 1,
220 kInsideSecond = 2,
221 kInsideBoth = 3
222};
223
224
225// Represents the relation of two sets.
226// Sets can be either disjoint, partially or fully overlapping, or equal.
227class SetRelation BASE_EMBEDDED {
228 public:
229 // Relation is represented by a bit saying whether there are elements in
230 // one set that is not in the other, and a bit saying that there are elements
231 // that are in both sets.
232
233 // Location of an element. Corresponds to the internal areas of
234 // a Venn diagram.
235 enum {
236 kInFirst = 1 << kInsideFirst,
237 kInSecond = 1 << kInsideSecond,
238 kInBoth = 1 << kInsideBoth
239 };
240 SetRelation() : bits_(0) {}
241 ~SetRelation() {}
242 // Add the existence of objects in a particular
243 void SetElementsInFirstSet() { bits_ |= kInFirst; }
244 void SetElementsInSecondSet() { bits_ |= kInSecond; }
245 void SetElementsInBothSets() { bits_ |= kInBoth; }
246 // Check the currently known relation of the sets (common functions only,
247 // for other combinations, use value() to get the bits and check them
248 // manually).
249 // Sets are completely disjoint.
250 bool Disjoint() { return (bits_ & kInBoth) == 0; }
251 // Sets are equal.
252 bool Equals() { return (bits_ & (kInFirst | kInSecond)) == 0; }
253 // First set contains second.
254 bool Contains() { return (bits_ & kInSecond) == 0; }
255 // Second set contains first.
256 bool ContainedIn() { return (bits_ & kInFirst) == 0; }
257 bool NonTrivialIntersection() {
258 return (bits_ == (kInFirst | kInSecond | kInBoth));
259 }
260 int value() { return bits_; }
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000261
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000262 private:
263 int bits_;
264};
265
266
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000267class CharacterRange {
268 public:
269 CharacterRange() : from_(0), to_(0) { }
270 // For compatibility with the CHECK_OK macro
271 CharacterRange(void* null) { ASSERT_EQ(NULL, null); } //NOLINT
272 CharacterRange(uc16 from, uc16 to) : from_(from), to_(to) { }
273 static void AddClassEscape(uc16 type, ZoneList<CharacterRange>* ranges);
274 static Vector<const uc16> GetWordBounds();
275 static inline CharacterRange Singleton(uc16 value) {
276 return CharacterRange(value, value);
277 }
278 static inline CharacterRange Range(uc16 from, uc16 to) {
279 ASSERT(from <= to);
280 return CharacterRange(from, to);
281 }
282 static inline CharacterRange Everything() {
283 return CharacterRange(0, 0xFFFF);
284 }
285 bool Contains(uc16 i) { return from_ <= i && i <= to_; }
286 uc16 from() const { return from_; }
287 void set_from(uc16 value) { from_ = value; }
288 uc16 to() const { return to_; }
289 void set_to(uc16 value) { to_ = value; }
290 bool is_valid() { return from_ <= to_; }
ager@chromium.org8bb60582008-12-11 12:02:20 +0000291 bool IsEverything(uc16 max) { return from_ == 0 && to_ >= max; }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000292 bool IsSingleton() { return (from_ == to_); }
ager@chromium.org38e4c712009-11-11 09:11:58 +0000293 void AddCaseEquivalents(ZoneList<CharacterRange>* ranges, bool is_ascii);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000294 static void Split(ZoneList<CharacterRange>* base,
295 Vector<const uc16> overlay,
296 ZoneList<CharacterRange>** included,
297 ZoneList<CharacterRange>** excluded);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000298 // Whether a range list is in canonical form: Ranges ordered by from value,
299 // and ranges non-overlapping and non-adjacent.
300 static bool IsCanonical(ZoneList<CharacterRange>* ranges);
301 // Convert range list to canonical form. The characters covered by the ranges
302 // will still be the same, but no character is in more than one range, and
303 // adjacent ranges are merged. The resulting list may be shorter than the
304 // original, but cannot be longer.
305 static void Canonicalize(ZoneList<CharacterRange>* ranges);
306 // Check how the set of characters defined by a CharacterRange list relates
307 // to the set of word characters. List must be in canonical form.
308 static SetRelation WordCharacterRelation(ZoneList<CharacterRange>* ranges);
309 // Takes two character range lists (representing character sets) in canonical
310 // form and merges them.
311 // The characters that are only covered by the first set are added to
312 // first_set_only_out. the characters that are only in the second set are
313 // added to second_set_only_out, and the characters that are in both are
314 // added to both_sets_out.
315 // The pointers to first_set_only_out, second_set_only_out and both_sets_out
316 // should be to empty lists, but they need not be distinct, and may be NULL.
317 // If NULL, the characters are dropped, and if two arguments are the same
318 // pointer, the result is the union of the two sets that would be created
319 // if the pointers had been distinct.
320 // This way, the Merge function can compute all the usual set operations:
321 // union (all three out-sets are equal), intersection (only both_sets_out is
322 // non-NULL), and set difference (only first_set is non-NULL).
323 static void Merge(ZoneList<CharacterRange>* first_set,
324 ZoneList<CharacterRange>* second_set,
325 ZoneList<CharacterRange>* first_set_only_out,
326 ZoneList<CharacterRange>* second_set_only_out,
327 ZoneList<CharacterRange>* both_sets_out);
328 // Negate the contents of a character range in canonical form.
329 static void Negate(ZoneList<CharacterRange>* src,
330 ZoneList<CharacterRange>* dst);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000331 static const int kStartMarker = (1 << 24);
332 static const int kPayloadMask = (1 << 24) - 1;
333
334 private:
335 uc16 from_;
336 uc16 to_;
337};
338
339
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000340// A set of unsigned integers that behaves especially well on small
341// integers (< 32). May do zone-allocation.
342class OutSet: public ZoneObject {
343 public:
344 OutSet() : first_(0), remaining_(NULL), successors_(NULL) { }
345 OutSet* Extend(unsigned value);
346 bool Get(unsigned value);
347 static const unsigned kFirstLimit = 32;
348
349 private:
350 // Destructively set a value in this set. In most cases you want
351 // to use Extend instead to ensure that only one instance exists
352 // that contains the same values.
353 void Set(unsigned value);
354
355 // The successors are a list of sets that contain the same values
356 // as this set and the one more value that is not present in this
357 // set.
358 ZoneList<OutSet*>* successors() { return successors_; }
359
360 OutSet(uint32_t first, ZoneList<unsigned>* remaining)
361 : first_(first), remaining_(remaining), successors_(NULL) { }
362 uint32_t first_;
363 ZoneList<unsigned>* remaining_;
364 ZoneList<OutSet*>* successors_;
ager@chromium.org32912102009-01-16 10:38:43 +0000365 friend class Trace;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000366};
367
368
369// A mapping from integers, specified as ranges, to a set of integers.
370// Used for mapping character ranges to choices.
371class DispatchTable : public ZoneObject {
372 public:
373 class Entry {
374 public:
375 Entry() : from_(0), to_(0), out_set_(NULL) { }
376 Entry(uc16 from, uc16 to, OutSet* out_set)
377 : from_(from), to_(to), out_set_(out_set) { }
378 uc16 from() { return from_; }
379 uc16 to() { return to_; }
380 void set_to(uc16 value) { to_ = value; }
381 void AddValue(int value) { out_set_ = out_set_->Extend(value); }
382 OutSet* out_set() { return out_set_; }
383 private:
384 uc16 from_;
385 uc16 to_;
386 OutSet* out_set_;
387 };
388
389 class Config {
390 public:
391 typedef uc16 Key;
392 typedef Entry Value;
393 static const uc16 kNoKey;
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000394 static const Entry NoValue() { return Value(); }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000395 static inline int Compare(uc16 a, uc16 b) {
396 if (a == b)
397 return 0;
398 else if (a < b)
399 return -1;
400 else
401 return 1;
402 }
403 };
404
405 void AddRange(CharacterRange range, int value);
406 OutSet* Get(uc16 value);
407 void Dump();
408
409 template <typename Callback>
410 void ForEach(Callback* callback) { return tree()->ForEach(callback); }
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000411
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000412 private:
413 // There can't be a static empty set since it allocates its
414 // successors in a zone and caches them.
415 OutSet* empty() { return &empty_; }
416 OutSet empty_;
417 ZoneSplayTree<Config>* tree() { return &tree_; }
418 ZoneSplayTree<Config> tree_;
419};
420
421
422#define FOR_EACH_NODE_TYPE(VISIT) \
423 VISIT(End) \
424 VISIT(Action) \
425 VISIT(Choice) \
426 VISIT(BackReference) \
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000427 VISIT(Assertion) \
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000428 VISIT(Text)
429
430
431#define FOR_EACH_REG_EXP_TREE_TYPE(VISIT) \
432 VISIT(Disjunction) \
433 VISIT(Alternative) \
434 VISIT(Assertion) \
435 VISIT(CharacterClass) \
436 VISIT(Atom) \
437 VISIT(Quantifier) \
438 VISIT(Capture) \
439 VISIT(Lookahead) \
440 VISIT(BackReference) \
441 VISIT(Empty) \
442 VISIT(Text)
443
444
445#define FORWARD_DECLARE(Name) class RegExp##Name;
446FOR_EACH_REG_EXP_TREE_TYPE(FORWARD_DECLARE)
447#undef FORWARD_DECLARE
448
449
450class TextElement {
451 public:
452 enum Type {UNINITIALIZED, ATOM, CHAR_CLASS};
453 TextElement() : type(UNINITIALIZED) { }
ager@chromium.org8bb60582008-12-11 12:02:20 +0000454 explicit TextElement(Type t) : type(t), cp_offset(-1) { }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000455 static TextElement Atom(RegExpAtom* atom);
456 static TextElement CharClass(RegExpCharacterClass* char_class);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000457 int length();
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000458 Type type;
459 union {
460 RegExpAtom* u_atom;
461 RegExpCharacterClass* u_char_class;
462 } data;
ager@chromium.org8bb60582008-12-11 12:02:20 +0000463 int cp_offset;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000464};
465
466
ager@chromium.org32912102009-01-16 10:38:43 +0000467class Trace;
ager@chromium.org8bb60582008-12-11 12:02:20 +0000468
469
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000470struct NodeInfo {
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000471 NodeInfo()
472 : being_analyzed(false),
473 been_analyzed(false),
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000474 follows_word_interest(false),
475 follows_newline_interest(false),
476 follows_start_interest(false),
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000477 at_end(false),
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000478 visited(false) { }
479
480 // Returns true if the interests and assumptions of this node
481 // matches the given one.
482 bool Matches(NodeInfo* that) {
483 return (at_end == that->at_end) &&
484 (follows_word_interest == that->follows_word_interest) &&
485 (follows_newline_interest == that->follows_newline_interest) &&
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000486 (follows_start_interest == that->follows_start_interest);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000487 }
488
489 // Updates the interests of this node given the interests of the
490 // node preceding it.
491 void AddFromPreceding(NodeInfo* that) {
492 at_end |= that->at_end;
493 follows_word_interest |= that->follows_word_interest;
494 follows_newline_interest |= that->follows_newline_interest;
495 follows_start_interest |= that->follows_start_interest;
496 }
497
ager@chromium.org8bb60582008-12-11 12:02:20 +0000498 bool HasLookbehind() {
499 return follows_word_interest ||
500 follows_newline_interest ||
501 follows_start_interest;
502 }
503
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000504 // Sets the interests of this node to include the interests of the
505 // following node.
506 void AddFromFollowing(NodeInfo* that) {
507 follows_word_interest |= that->follows_word_interest;
508 follows_newline_interest |= that->follows_newline_interest;
509 follows_start_interest |= that->follows_start_interest;
510 }
511
512 void ResetCompilationState() {
513 being_analyzed = false;
514 been_analyzed = false;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000515 }
516
517 bool being_analyzed: 1;
518 bool been_analyzed: 1;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000519
520 // These bits are set of this node has to know what the preceding
521 // character was.
522 bool follows_word_interest: 1;
523 bool follows_newline_interest: 1;
524 bool follows_start_interest: 1;
525
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000526 bool at_end: 1;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000527 bool visited: 1;
528};
529
530
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000531class SiblingList {
532 public:
533 SiblingList() : list_(NULL) { }
534 int length() {
535 return list_ == NULL ? 0 : list_->length();
536 }
537 void Ensure(RegExpNode* parent) {
538 if (list_ == NULL) {
539 list_ = new ZoneList<RegExpNode*>(2);
540 list_->Add(parent);
541 }
542 }
543 void Add(RegExpNode* node) { list_->Add(node); }
544 RegExpNode* Get(int index) { return list_->at(index); }
545 private:
546 ZoneList<RegExpNode*>* list_;
547};
548
549
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000550// Details of a quick mask-compare check that can look ahead in the
551// input stream.
552class QuickCheckDetails {
553 public:
554 QuickCheckDetails()
555 : characters_(0),
556 mask_(0),
iposva@chromium.org245aa852009-02-10 00:49:54 +0000557 value_(0),
558 cannot_match_(false) { }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000559 explicit QuickCheckDetails(int characters)
560 : characters_(characters),
561 mask_(0),
iposva@chromium.org245aa852009-02-10 00:49:54 +0000562 value_(0),
563 cannot_match_(false) { }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000564 bool Rationalize(bool ascii);
565 // Merge in the information from another branch of an alternation.
566 void Merge(QuickCheckDetails* other, int from_index);
567 // Advance the current position by some amount.
568 void Advance(int by, bool ascii);
569 void Clear();
iposva@chromium.org245aa852009-02-10 00:49:54 +0000570 bool cannot_match() { return cannot_match_; }
571 void set_cannot_match() { cannot_match_ = true; }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000572 struct Position {
573 Position() : mask(0), value(0), determines_perfectly(false) { }
574 uc16 mask;
575 uc16 value;
576 bool determines_perfectly;
577 };
578 int characters() { return characters_; }
579 void set_characters(int characters) { characters_ = characters; }
580 Position* positions(int index) {
581 ASSERT(index >= 0);
582 ASSERT(index < characters_);
583 return positions_ + index;
584 }
585 uint32_t mask() { return mask_; }
586 uint32_t value() { return value_; }
587
588 private:
589 // How many characters do we have quick check information from. This is
590 // the same for all branches of a choice node.
591 int characters_;
592 Position positions_[4];
593 // These values are the condensate of the above array after Rationalize().
594 uint32_t mask_;
595 uint32_t value_;
iposva@chromium.org245aa852009-02-10 00:49:54 +0000596 // If set to true, there is no way this quick check can match at all.
597 // E.g., if it requires to be at the start of the input, and isn't.
598 bool cannot_match_;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000599};
600
601
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000602class RegExpNode: public ZoneObject {
603 public:
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000604 RegExpNode() : first_character_set_(NULL), trace_count_(0) { }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000605 virtual ~RegExpNode();
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000606 virtual void Accept(NodeVisitor* visitor) = 0;
607 // Generates a goto to this node or actually generates the code at this point.
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000608 virtual void Emit(RegExpCompiler* compiler, Trace* trace) = 0;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000609 // How many characters must this node consume at a minimum in order to
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000610 // succeed. If we have found at least 'still_to_find' characters that
611 // must be consumed there is no need to ask any following nodes whether
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000612 // they are sure to eat any more characters. The not_at_start argument is
613 // used to indicate that we know we are not at the start of the input. In
614 // this case anchored branches will always fail and can be ignored when
615 // determining how many characters are consumed on success.
616 virtual int EatsAtLeast(int still_to_find,
617 int recursion_depth,
618 bool not_at_start) = 0;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000619 // Emits some quick code that checks whether the preloaded characters match.
620 // Falls through on certain failure, jumps to the label on possible success.
621 // If the node cannot make a quick check it does nothing and returns false.
622 bool EmitQuickCheck(RegExpCompiler* compiler,
ager@chromium.org32912102009-01-16 10:38:43 +0000623 Trace* trace,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000624 bool preload_has_checked_bounds,
625 Label* on_possible_success,
626 QuickCheckDetails* details_return,
627 bool fall_through_on_failure);
628 // For a given number of characters this returns a mask and a value. The
629 // next n characters are anded with the mask and compared with the value.
630 // A comparison failure indicates the node cannot match the next n characters.
631 // A comparison success indicates the node may match.
632 virtual void GetQuickCheckDetails(QuickCheckDetails* details,
633 RegExpCompiler* compiler,
iposva@chromium.org245aa852009-02-10 00:49:54 +0000634 int characters_filled_in,
635 bool not_at_start) = 0;
ager@chromium.org8bb60582008-12-11 12:02:20 +0000636 static const int kNodeIsTooComplexForGreedyLoops = -1;
637 virtual int GreedyLoopTextLength() { return kNodeIsTooComplexForGreedyLoops; }
638 Label* label() { return &label_; }
ulan@chromium.org2efb9002012-01-19 15:36:35 +0000639 // 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 +0000640 // start of the trace) then it cannot be reused. This variable sets a limit
641 // on how often we allow that to happen before we insist on starting a new
642 // trace and generating generic code for a node that can be reused by flushing
643 // the deferred actions in the current trace and generating a goto.
644 static const int kMaxCopiesCodeGenerated = 10;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000645
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000646 NodeInfo* info() { return &info_; }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000647
648 void AddSibling(RegExpNode* node) { siblings_.Add(node); }
649
650 // Static version of EnsureSibling that expresses the fact that the
651 // result has the same type as the input.
652 template <class C>
653 static C* EnsureSibling(C* node, NodeInfo* info, bool* cloned) {
654 return static_cast<C*>(node->EnsureSibling(info, cloned));
655 }
656
657 SiblingList* siblings() { return &siblings_; }
658 void set_siblings(SiblingList* other) { siblings_ = *other; }
659
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000660 // Return the set of possible next characters recognized by the regexp
661 // (or a safe subset, potentially the set of all characters).
662 ZoneList<CharacterRange>* FirstCharacterSet();
663
664 // Compute (if possible within the budget of traversed nodes) the
665 // possible first characters of the input matched by this node and
666 // its continuation. Returns the remaining budget after the computation.
667 // If the budget is spent, the result is negative, and the cached
668 // first_character_set_ value isn't set.
669 virtual int ComputeFirstCharacterSet(int budget);
670
671 // Get and set the cached first character set value.
672 ZoneList<CharacterRange>* first_character_set() {
673 return first_character_set_;
674 }
675 void set_first_character_set(ZoneList<CharacterRange>* character_set) {
676 first_character_set_ = character_set;
677 }
678
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000679 protected:
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000680 enum LimitResult { DONE, CONTINUE };
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000681 static const int kComputeFirstCharacterSetFail = -1;
682
ager@chromium.org32912102009-01-16 10:38:43 +0000683 LimitResult LimitVersions(RegExpCompiler* compiler, Trace* trace);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000684
685 // Returns a sibling of this node whose interests and assumptions
686 // match the ones in the given node info. If no sibling exists NULL
687 // is returned.
688 RegExpNode* TryGetSibling(NodeInfo* info);
689
690 // Returns a sibling of this node whose interests match the ones in
691 // the given node info. The info must not contain any assertions.
692 // If no node exists a new one will be created by cloning the current
693 // node. The result will always be an instance of the same concrete
694 // class as this node.
695 RegExpNode* EnsureSibling(NodeInfo* info, bool* cloned);
696
697 // Returns a clone of this node initialized using the copy constructor
698 // of its concrete class. Note that the node may have to be pre-
ager@chromium.org32912102009-01-16 10:38:43 +0000699 // processed before it is on a usable state.
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000700 virtual RegExpNode* Clone() = 0;
701
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000702 private:
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000703 static const int kFirstCharBudget = 10;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000704 Label label_;
705 NodeInfo info_;
706 SiblingList siblings_;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000707 ZoneList<CharacterRange>* first_character_set_;
ager@chromium.org32912102009-01-16 10:38:43 +0000708 // This variable keeps track of how many times code has been generated for
709 // this node (in different traces). We don't keep track of where the
710 // generated code is located unless the code is generated at the start of
711 // a trace, in which case it is generic and can be reused by flushing the
712 // deferred operations in the current trace and generating a goto.
713 int trace_count_;
714};
715
716
717// A simple closed interval.
718class Interval {
719 public:
720 Interval() : from_(kNone), to_(kNone) { }
721 Interval(int from, int to) : from_(from), to_(to) { }
722 Interval Union(Interval that) {
723 if (that.from_ == kNone)
724 return *this;
725 else if (from_ == kNone)
726 return that;
727 else
728 return Interval(Min(from_, that.from_), Max(to_, that.to_));
729 }
730 bool Contains(int value) {
731 return (from_ <= value) && (value <= to_);
732 }
733 bool is_empty() { return from_ == kNone; }
734 int from() { return from_; }
735 int to() { return to_; }
736 static Interval Empty() { return Interval(); }
737 static const int kNone = -1;
738 private:
739 int from_;
740 int to_;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000741};
742
743
744class SeqRegExpNode: public RegExpNode {
745 public:
746 explicit SeqRegExpNode(RegExpNode* on_success)
747 : on_success_(on_success) { }
748 RegExpNode* on_success() { return on_success_; }
749 void set_on_success(RegExpNode* node) { on_success_ = node; }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000750 private:
751 RegExpNode* on_success_;
752};
753
754
755class ActionNode: public SeqRegExpNode {
756 public:
757 enum Type {
ager@chromium.org8bb60582008-12-11 12:02:20 +0000758 SET_REGISTER,
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000759 INCREMENT_REGISTER,
760 STORE_POSITION,
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000761 BEGIN_SUBMATCH,
ager@chromium.org32912102009-01-16 10:38:43 +0000762 POSITIVE_SUBMATCH_SUCCESS,
763 EMPTY_MATCH_CHECK,
764 CLEAR_CAPTURES
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000765 };
ager@chromium.org8bb60582008-12-11 12:02:20 +0000766 static ActionNode* SetRegister(int reg, int val, RegExpNode* on_success);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000767 static ActionNode* IncrementRegister(int reg, RegExpNode* on_success);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000768 static ActionNode* StorePosition(int reg,
769 bool is_capture,
770 RegExpNode* on_success);
ager@chromium.org32912102009-01-16 10:38:43 +0000771 static ActionNode* ClearCaptures(Interval range, RegExpNode* on_success);
772 static ActionNode* BeginSubmatch(int stack_pointer_reg,
773 int position_reg,
774 RegExpNode* on_success);
775 static ActionNode* PositiveSubmatchSuccess(int stack_pointer_reg,
776 int restore_reg,
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000777 int clear_capture_count,
778 int clear_capture_from,
ager@chromium.org32912102009-01-16 10:38:43 +0000779 RegExpNode* on_success);
780 static ActionNode* EmptyMatchCheck(int start_register,
781 int repetition_register,
782 int repetition_limit,
783 RegExpNode* on_success);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000784 virtual void Accept(NodeVisitor* visitor);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000785 virtual void Emit(RegExpCompiler* compiler, Trace* trace);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000786 virtual int EatsAtLeast(int still_to_find,
787 int recursion_depth,
788 bool not_at_start);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000789 virtual void GetQuickCheckDetails(QuickCheckDetails* details,
790 RegExpCompiler* compiler,
iposva@chromium.org245aa852009-02-10 00:49:54 +0000791 int filled_in,
792 bool not_at_start) {
793 return on_success()->GetQuickCheckDetails(
794 details, compiler, filled_in, not_at_start);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000795 }
ager@chromium.org8bb60582008-12-11 12:02:20 +0000796 Type type() { return type_; }
797 // TODO(erikcorry): We should allow some action nodes in greedy loops.
798 virtual int GreedyLoopTextLength() { return kNodeIsTooComplexForGreedyLoops; }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000799 virtual ActionNode* Clone() { return new ActionNode(*this); }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000800 virtual int ComputeFirstCharacterSet(int budget);
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000801
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000802 private:
803 union {
804 struct {
805 int reg;
806 int value;
807 } u_store_register;
808 struct {
809 int reg;
810 } u_increment_register;
811 struct {
812 int reg;
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000813 bool is_capture;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000814 } u_position_register;
815 struct {
816 int stack_pointer_register;
817 int current_position_register;
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000818 int clear_register_count;
819 int clear_register_from;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000820 } u_submatch;
ager@chromium.org32912102009-01-16 10:38:43 +0000821 struct {
822 int start_register;
823 int repetition_register;
824 int repetition_limit;
825 } u_empty_match_check;
826 struct {
827 int range_from;
828 int range_to;
829 } u_clear_captures;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000830 } data_;
831 ActionNode(Type type, RegExpNode* on_success)
832 : SeqRegExpNode(on_success),
833 type_(type) { }
834 Type type_;
835 friend class DotPrinter;
836};
837
838
839class TextNode: public SeqRegExpNode {
840 public:
841 TextNode(ZoneList<TextElement>* elms,
ager@chromium.org8bb60582008-12-11 12:02:20 +0000842 RegExpNode* on_success)
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000843 : SeqRegExpNode(on_success),
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000844 elms_(elms) { }
845 TextNode(RegExpCharacterClass* that,
ager@chromium.org8bb60582008-12-11 12:02:20 +0000846 RegExpNode* on_success)
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000847 : SeqRegExpNode(on_success),
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000848 elms_(new ZoneList<TextElement>(1)) {
849 elms_->Add(TextElement::CharClass(that));
850 }
851 virtual void Accept(NodeVisitor* visitor);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000852 virtual void Emit(RegExpCompiler* compiler, Trace* trace);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000853 virtual int EatsAtLeast(int still_to_find,
854 int recursion_depth,
855 bool not_at_start);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000856 virtual void GetQuickCheckDetails(QuickCheckDetails* details,
857 RegExpCompiler* compiler,
iposva@chromium.org245aa852009-02-10 00:49:54 +0000858 int characters_filled_in,
859 bool not_at_start);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000860 ZoneList<TextElement>* elements() { return elms_; }
ager@chromium.org38e4c712009-11-11 09:11:58 +0000861 void MakeCaseIndependent(bool is_ascii);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000862 virtual int GreedyLoopTextLength();
863 virtual TextNode* Clone() {
864 TextNode* result = new TextNode(*this);
865 result->CalculateOffsets();
866 return result;
867 }
868 void CalculateOffsets();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000869 virtual int ComputeFirstCharacterSet(int budget);
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000870
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000871 private:
872 enum TextEmitPassType {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000873 NON_ASCII_MATCH, // Check for characters that can't match.
874 SIMPLE_CHARACTER_MATCH, // Case-dependent single character check.
875 NON_LETTER_CHARACTER_MATCH, // Check characters that have no case equivs.
876 CASE_CHARACTER_MATCH, // Case-independent single character check.
877 CHARACTER_CLASS_MATCH // Character class.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000878 };
ager@chromium.org381abbb2009-02-25 13:23:22 +0000879 static bool SkipPass(int pass, bool ignore_case);
880 static const int kFirstRealPass = SIMPLE_CHARACTER_MATCH;
881 static const int kLastPass = CHARACTER_CLASS_MATCH;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000882 void TextEmitPass(RegExpCompiler* compiler,
883 TextEmitPassType pass,
884 bool preloaded,
ager@chromium.org32912102009-01-16 10:38:43 +0000885 Trace* trace,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000886 bool first_element_checked,
887 int* checked_up_to);
888 int Length();
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000889 ZoneList<TextElement>* elms_;
890};
891
892
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000893class AssertionNode: public SeqRegExpNode {
894 public:
895 enum AssertionNodeType {
896 AT_END,
897 AT_START,
898 AT_BOUNDARY,
899 AT_NON_BOUNDARY,
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000900 AFTER_NEWLINE,
901 // Types not directly expressible in regexp syntax.
902 // Used for modifying a boundary node if its following character is
903 // known to be word and/or non-word.
904 AFTER_NONWORD_CHARACTER,
905 AFTER_WORD_CHARACTER
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000906 };
907 static AssertionNode* AtEnd(RegExpNode* on_success) {
908 return new AssertionNode(AT_END, on_success);
909 }
910 static AssertionNode* AtStart(RegExpNode* on_success) {
911 return new AssertionNode(AT_START, on_success);
912 }
913 static AssertionNode* AtBoundary(RegExpNode* on_success) {
914 return new AssertionNode(AT_BOUNDARY, on_success);
915 }
916 static AssertionNode* AtNonBoundary(RegExpNode* on_success) {
917 return new AssertionNode(AT_NON_BOUNDARY, on_success);
918 }
919 static AssertionNode* AfterNewline(RegExpNode* on_success) {
920 return new AssertionNode(AFTER_NEWLINE, on_success);
921 }
922 virtual void Accept(NodeVisitor* visitor);
923 virtual void Emit(RegExpCompiler* compiler, Trace* trace);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000924 virtual int EatsAtLeast(int still_to_find,
925 int recursion_depth,
926 bool not_at_start);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000927 virtual void GetQuickCheckDetails(QuickCheckDetails* details,
928 RegExpCompiler* compiler,
iposva@chromium.org245aa852009-02-10 00:49:54 +0000929 int filled_in,
930 bool not_at_start);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000931 virtual int ComputeFirstCharacterSet(int budget);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000932 virtual AssertionNode* Clone() { return new AssertionNode(*this); }
933 AssertionNodeType type() { return type_; }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000934 void set_type(AssertionNodeType type) { type_ = type; }
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000935
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000936 private:
937 AssertionNode(AssertionNodeType t, RegExpNode* on_success)
938 : SeqRegExpNode(on_success), type_(t) { }
939 AssertionNodeType type_;
940};
941
942
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000943class BackReferenceNode: public SeqRegExpNode {
944 public:
945 BackReferenceNode(int start_reg,
946 int end_reg,
ager@chromium.org8bb60582008-12-11 12:02:20 +0000947 RegExpNode* on_success)
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000948 : SeqRegExpNode(on_success),
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000949 start_reg_(start_reg),
950 end_reg_(end_reg) { }
951 virtual void Accept(NodeVisitor* visitor);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000952 int start_register() { return start_reg_; }
953 int end_register() { return end_reg_; }
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000954 virtual void Emit(RegExpCompiler* compiler, Trace* trace);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000955 virtual int EatsAtLeast(int still_to_find,
956 int recursion_depth,
957 bool not_at_start);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000958 virtual void GetQuickCheckDetails(QuickCheckDetails* details,
959 RegExpCompiler* compiler,
iposva@chromium.org245aa852009-02-10 00:49:54 +0000960 int characters_filled_in,
961 bool not_at_start) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000962 return;
963 }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000964 virtual BackReferenceNode* Clone() { return new BackReferenceNode(*this); }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000965 virtual int ComputeFirstCharacterSet(int budget);
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000966
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000967 private:
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000968 int start_reg_;
969 int end_reg_;
970};
971
972
973class EndNode: public RegExpNode {
974 public:
ager@chromium.org8bb60582008-12-11 12:02:20 +0000975 enum Action { ACCEPT, BACKTRACK, NEGATIVE_SUBMATCH_SUCCESS };
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000976 explicit EndNode(Action action) : action_(action) { }
977 virtual void Accept(NodeVisitor* visitor);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000978 virtual void Emit(RegExpCompiler* compiler, Trace* trace);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000979 virtual int EatsAtLeast(int still_to_find,
980 int recursion_depth,
981 bool not_at_start) { return 0; }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000982 virtual void GetQuickCheckDetails(QuickCheckDetails* details,
983 RegExpCompiler* compiler,
iposva@chromium.org245aa852009-02-10 00:49:54 +0000984 int characters_filled_in,
985 bool not_at_start) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000986 // Returning 0 from EatsAtLeast should ensure we never get here.
987 UNREACHABLE();
988 }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000989 virtual EndNode* Clone() { return new EndNode(*this); }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000990 private:
991 Action action_;
992};
993
994
ager@chromium.org8bb60582008-12-11 12:02:20 +0000995class NegativeSubmatchSuccess: public EndNode {
996 public:
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000997 NegativeSubmatchSuccess(int stack_pointer_reg,
998 int position_reg,
999 int clear_capture_count,
1000 int clear_capture_start)
ager@chromium.org8bb60582008-12-11 12:02:20 +00001001 : EndNode(NEGATIVE_SUBMATCH_SUCCESS),
1002 stack_pointer_register_(stack_pointer_reg),
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001003 current_position_register_(position_reg),
1004 clear_capture_count_(clear_capture_count),
1005 clear_capture_start_(clear_capture_start) { }
1006 virtual void Emit(RegExpCompiler* compiler, Trace* trace);
ager@chromium.org8bb60582008-12-11 12:02:20 +00001007
1008 private:
1009 int stack_pointer_register_;
1010 int current_position_register_;
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001011 int clear_capture_count_;
1012 int clear_capture_start_;
ager@chromium.org8bb60582008-12-11 12:02:20 +00001013};
1014
1015
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001016class Guard: public ZoneObject {
1017 public:
1018 enum Relation { LT, GEQ };
1019 Guard(int reg, Relation op, int value)
1020 : reg_(reg),
1021 op_(op),
1022 value_(value) { }
1023 int reg() { return reg_; }
1024 Relation op() { return op_; }
1025 int value() { return value_; }
1026
1027 private:
1028 int reg_;
1029 Relation op_;
1030 int value_;
1031};
1032
1033
1034class GuardedAlternative {
1035 public:
1036 explicit GuardedAlternative(RegExpNode* node) : node_(node), guards_(NULL) { }
1037 void AddGuard(Guard* guard);
1038 RegExpNode* node() { return node_; }
1039 void set_node(RegExpNode* node) { node_ = node; }
1040 ZoneList<Guard*>* guards() { return guards_; }
1041
1042 private:
1043 RegExpNode* node_;
1044 ZoneList<Guard*>* guards_;
1045};
1046
1047
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001048class AlternativeGeneration;
1049
1050
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001051class ChoiceNode: public RegExpNode {
1052 public:
ager@chromium.org8bb60582008-12-11 12:02:20 +00001053 explicit ChoiceNode(int expected_size)
1054 : alternatives_(new ZoneList<GuardedAlternative>(expected_size)),
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001055 table_(NULL),
iposva@chromium.org245aa852009-02-10 00:49:54 +00001056 not_at_start_(false),
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001057 being_calculated_(false) { }
1058 virtual void Accept(NodeVisitor* visitor);
1059 void AddAlternative(GuardedAlternative node) { alternatives()->Add(node); }
1060 ZoneList<GuardedAlternative>* alternatives() { return alternatives_; }
1061 DispatchTable* GetTable(bool ignore_case);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001062 virtual void Emit(RegExpCompiler* compiler, Trace* trace);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001063 virtual int EatsAtLeast(int still_to_find,
1064 int recursion_depth,
1065 bool not_at_start);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001066 int EatsAtLeastHelper(int still_to_find,
1067 int recursion_depth,
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001068 RegExpNode* ignore_this_node,
1069 bool not_at_start);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001070 virtual void GetQuickCheckDetails(QuickCheckDetails* details,
1071 RegExpCompiler* compiler,
iposva@chromium.org245aa852009-02-10 00:49:54 +00001072 int characters_filled_in,
1073 bool not_at_start);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001074 virtual ChoiceNode* Clone() { return new ChoiceNode(*this); }
1075
1076 bool being_calculated() { return being_calculated_; }
iposva@chromium.org245aa852009-02-10 00:49:54 +00001077 bool not_at_start() { return not_at_start_; }
1078 void set_not_at_start() { not_at_start_ = true; }
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001079 void set_being_calculated(bool b) { being_calculated_ = b; }
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001080 virtual bool try_to_emit_quick_check_for_alternative(int i) { return true; }
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001081
ager@chromium.org8bb60582008-12-11 12:02:20 +00001082 protected:
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001083 int GreedyLoopTextLengthForAlternative(GuardedAlternative* alternative);
ager@chromium.org8bb60582008-12-11 12:02:20 +00001084 ZoneList<GuardedAlternative>* alternatives_;
1085
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001086 private:
1087 friend class DispatchTableConstructor;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001088 friend class Analysis;
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001089 void GenerateGuard(RegExpMacroAssembler* macro_assembler,
ager@chromium.org5ec48922009-05-05 07:25:34 +00001090 Guard* guard,
ager@chromium.org32912102009-01-16 10:38:43 +00001091 Trace* trace);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001092 int CalculatePreloadCharacters(RegExpCompiler* compiler, bool not_at_start);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001093 void EmitOutOfLineContinuation(RegExpCompiler* compiler,
ager@chromium.org32912102009-01-16 10:38:43 +00001094 Trace* trace,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001095 GuardedAlternative alternative,
1096 AlternativeGeneration* alt_gen,
1097 int preload_characters,
1098 bool next_expects_preload);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001099 DispatchTable* table_;
iposva@chromium.org245aa852009-02-10 00:49:54 +00001100 // If true, this node is never checked at the start of the input.
1101 // Allows a new trace to start with at_start() set to false.
1102 bool not_at_start_;
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001103 bool being_calculated_;
1104};
1105
1106
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001107class NegativeLookaheadChoiceNode: public ChoiceNode {
1108 public:
1109 explicit NegativeLookaheadChoiceNode(GuardedAlternative this_must_fail,
1110 GuardedAlternative then_do_this)
1111 : ChoiceNode(2) {
1112 AddAlternative(this_must_fail);
1113 AddAlternative(then_do_this);
1114 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001115 virtual int EatsAtLeast(int still_to_find,
1116 int recursion_depth,
1117 bool not_at_start);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001118 virtual void GetQuickCheckDetails(QuickCheckDetails* details,
1119 RegExpCompiler* compiler,
iposva@chromium.org245aa852009-02-10 00:49:54 +00001120 int characters_filled_in,
1121 bool not_at_start);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001122 // For a negative lookahead we don't emit the quick check for the
1123 // alternative that is expected to fail. This is because quick check code
1124 // starts by loading enough characters for the alternative that takes fewest
1125 // characters, but on a negative lookahead the negative branch did not take
1126 // part in that calculation (EatsAtLeast) so the assumptions don't hold.
1127 virtual bool try_to_emit_quick_check_for_alternative(int i) { return i != 0; }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001128 virtual int ComputeFirstCharacterSet(int budget);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001129};
1130
1131
ager@chromium.org8bb60582008-12-11 12:02:20 +00001132class LoopChoiceNode: public ChoiceNode {
1133 public:
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001134 explicit LoopChoiceNode(bool body_can_be_zero_length)
1135 : ChoiceNode(2),
1136 loop_node_(NULL),
1137 continue_node_(NULL),
1138 body_can_be_zero_length_(body_can_be_zero_length) { }
1139 void AddLoopAlternative(GuardedAlternative alt);
1140 void AddContinueAlternative(GuardedAlternative alt);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001141 virtual void Emit(RegExpCompiler* compiler, Trace* trace);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001142 virtual int EatsAtLeast(int still_to_find,
1143 int recursion_depth,
1144 bool not_at_start);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001145 virtual void GetQuickCheckDetails(QuickCheckDetails* details,
1146 RegExpCompiler* compiler,
iposva@chromium.org245aa852009-02-10 00:49:54 +00001147 int characters_filled_in,
1148 bool not_at_start);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001149 virtual int ComputeFirstCharacterSet(int budget);
ager@chromium.org8bb60582008-12-11 12:02:20 +00001150 virtual LoopChoiceNode* Clone() { return new LoopChoiceNode(*this); }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001151 RegExpNode* loop_node() { return loop_node_; }
1152 RegExpNode* continue_node() { return continue_node_; }
1153 bool body_can_be_zero_length() { return body_can_be_zero_length_; }
1154 virtual void Accept(NodeVisitor* visitor);
1155
1156 private:
1157 // AddAlternative is made private for loop nodes because alternatives
1158 // should not be added freely, we need to keep track of which node
1159 // goes back to the node itself.
1160 void AddAlternative(GuardedAlternative node) {
1161 ChoiceNode::AddAlternative(node);
1162 }
1163
1164 RegExpNode* loop_node_;
1165 RegExpNode* continue_node_;
1166 bool body_can_be_zero_length_;
ager@chromium.org8bb60582008-12-11 12:02:20 +00001167};
1168
1169
1170// There are many ways to generate code for a node. This class encapsulates
1171// the current way we should be generating. In other words it encapsulates
ager@chromium.org32912102009-01-16 10:38:43 +00001172// the current state of the code generator. The effect of this is that we
1173// generate code for paths that the matcher can take through the regular
1174// expression. A given node in the regexp can be code-generated several times
1175// as it can be part of several traces. For example for the regexp:
1176// /foo(bar|ip)baz/ the code to match baz will be generated twice, once as part
1177// of the foo-bar-baz trace and once as part of the foo-ip-baz trace. The code
1178// to match foo is generated only once (the traces have a common prefix). The
1179// code to store the capture is deferred and generated (twice) after the places
1180// where baz has been matched.
1181class Trace {
ager@chromium.org8bb60582008-12-11 12:02:20 +00001182 public:
iposva@chromium.org245aa852009-02-10 00:49:54 +00001183 // A value for a property that is either known to be true, know to be false,
1184 // or not known.
1185 enum TriBool {
1186 UNKNOWN = -1, FALSE = 0, TRUE = 1
1187 };
1188
ager@chromium.org8bb60582008-12-11 12:02:20 +00001189 class DeferredAction {
1190 public:
1191 DeferredAction(ActionNode::Type type, int reg)
1192 : type_(type), reg_(reg), next_(NULL) { }
1193 DeferredAction* next() { return next_; }
ager@chromium.org32912102009-01-16 10:38:43 +00001194 bool Mentions(int reg);
ager@chromium.org8bb60582008-12-11 12:02:20 +00001195 int reg() { return reg_; }
1196 ActionNode::Type type() { return type_; }
1197 private:
1198 ActionNode::Type type_;
1199 int reg_;
1200 DeferredAction* next_;
ager@chromium.org32912102009-01-16 10:38:43 +00001201 friend class Trace;
ager@chromium.org8bb60582008-12-11 12:02:20 +00001202 };
1203
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001204 class DeferredCapture : public DeferredAction {
ager@chromium.org8bb60582008-12-11 12:02:20 +00001205 public:
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001206 DeferredCapture(int reg, bool is_capture, Trace* trace)
ager@chromium.org8bb60582008-12-11 12:02:20 +00001207 : DeferredAction(ActionNode::STORE_POSITION, reg),
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001208 cp_offset_(trace->cp_offset()),
1209 is_capture_(is_capture) { }
ager@chromium.org8bb60582008-12-11 12:02:20 +00001210 int cp_offset() { return cp_offset_; }
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001211 bool is_capture() { return is_capture_; }
ager@chromium.org8bb60582008-12-11 12:02:20 +00001212 private:
1213 int cp_offset_;
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001214 bool is_capture_;
ager@chromium.org8bb60582008-12-11 12:02:20 +00001215 void set_cp_offset(int cp_offset) { cp_offset_ = cp_offset; }
1216 };
1217
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001218 class DeferredSetRegister : public DeferredAction {
ager@chromium.org8bb60582008-12-11 12:02:20 +00001219 public:
1220 DeferredSetRegister(int reg, int value)
1221 : DeferredAction(ActionNode::SET_REGISTER, reg),
1222 value_(value) { }
1223 int value() { return value_; }
1224 private:
1225 int value_;
1226 };
1227
ager@chromium.org32912102009-01-16 10:38:43 +00001228 class DeferredClearCaptures : public DeferredAction {
1229 public:
1230 explicit DeferredClearCaptures(Interval range)
1231 : DeferredAction(ActionNode::CLEAR_CAPTURES, -1),
1232 range_(range) { }
1233 Interval range() { return range_; }
1234 private:
1235 Interval range_;
1236 };
1237
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001238 class DeferredIncrementRegister : public DeferredAction {
ager@chromium.org8bb60582008-12-11 12:02:20 +00001239 public:
1240 explicit DeferredIncrementRegister(int reg)
1241 : DeferredAction(ActionNode::INCREMENT_REGISTER, reg) { }
1242 };
1243
ager@chromium.org32912102009-01-16 10:38:43 +00001244 Trace()
ager@chromium.org8bb60582008-12-11 12:02:20 +00001245 : cp_offset_(0),
1246 actions_(NULL),
1247 backtrack_(NULL),
1248 stop_node_(NULL),
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001249 loop_label_(NULL),
1250 characters_preloaded_(0),
iposva@chromium.org245aa852009-02-10 00:49:54 +00001251 bound_checked_up_to_(0),
ager@chromium.org381abbb2009-02-25 13:23:22 +00001252 flush_budget_(100),
iposva@chromium.org245aa852009-02-10 00:49:54 +00001253 at_start_(UNKNOWN) { }
1254
ager@chromium.org32912102009-01-16 10:38:43 +00001255 // End the trace. This involves flushing the deferred actions in the trace
1256 // and pushing a backtrack location onto the backtrack stack. Once this is
1257 // done we can start a new trace or go to one that has already been
1258 // generated.
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001259 void Flush(RegExpCompiler* compiler, RegExpNode* successor);
ager@chromium.org8bb60582008-12-11 12:02:20 +00001260 int cp_offset() { return cp_offset_; }
1261 DeferredAction* actions() { return actions_; }
ager@chromium.org32912102009-01-16 10:38:43 +00001262 // A trivial trace is one that has no deferred actions or other state that
1263 // affects the assumptions used when generating code. There is no recorded
1264 // backtrack location in a trivial trace, so with a trivial trace we will
1265 // generate code that, on a failure to match, gets the backtrack location
1266 // from the backtrack stack rather than using a direct jump instruction. We
1267 // always start code generation with a trivial trace and non-trivial traces
1268 // are created as we emit code for nodes or add to the list of deferred
1269 // actions in the trace. The location of the code generated for a node using
1270 // a trivial trace is recorded in a label in the node so that gotos can be
1271 // generated to that code.
ager@chromium.org8bb60582008-12-11 12:02:20 +00001272 bool is_trivial() {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001273 return backtrack_ == NULL &&
1274 actions_ == NULL &&
1275 cp_offset_ == 0 &&
1276 characters_preloaded_ == 0 &&
1277 bound_checked_up_to_ == 0 &&
iposva@chromium.org245aa852009-02-10 00:49:54 +00001278 quick_check_performed_.characters() == 0 &&
1279 at_start_ == UNKNOWN;
ager@chromium.org8bb60582008-12-11 12:02:20 +00001280 }
iposva@chromium.org245aa852009-02-10 00:49:54 +00001281 TriBool at_start() { return at_start_; }
1282 void set_at_start(bool at_start) { at_start_ = at_start ? TRUE : FALSE; }
ager@chromium.org8bb60582008-12-11 12:02:20 +00001283 Label* backtrack() { return backtrack_; }
1284 Label* loop_label() { return loop_label_; }
1285 RegExpNode* stop_node() { return stop_node_; }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001286 int characters_preloaded() { return characters_preloaded_; }
1287 int bound_checked_up_to() { return bound_checked_up_to_; }
ager@chromium.org381abbb2009-02-25 13:23:22 +00001288 int flush_budget() { return flush_budget_; }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001289 QuickCheckDetails* quick_check_performed() { return &quick_check_performed_; }
1290 bool mentions_reg(int reg);
ager@chromium.org32912102009-01-16 10:38:43 +00001291 // Returns true if a deferred position store exists to the specified
1292 // register and stores the offset in the out-parameter. Otherwise
1293 // returns false.
1294 bool GetStoredPosition(int reg, int* cp_offset);
1295 // These set methods and AdvanceCurrentPositionInTrace should be used only on
1296 // new traces - the intention is that traces are immutable after creation.
ager@chromium.org8bb60582008-12-11 12:02:20 +00001297 void add_action(DeferredAction* new_action) {
1298 ASSERT(new_action->next_ == NULL);
1299 new_action->next_ = actions_;
1300 actions_ = new_action;
1301 }
ager@chromium.org8bb60582008-12-11 12:02:20 +00001302 void set_backtrack(Label* backtrack) { backtrack_ = backtrack; }
1303 void set_stop_node(RegExpNode* node) { stop_node_ = node; }
1304 void set_loop_label(Label* label) { loop_label_ = label; }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001305 void set_characters_preloaded(int count) { characters_preloaded_ = count; }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001306 void set_bound_checked_up_to(int to) { bound_checked_up_to_ = to; }
ager@chromium.org381abbb2009-02-25 13:23:22 +00001307 void set_flush_budget(int to) { flush_budget_ = to; }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001308 void set_quick_check_performed(QuickCheckDetails* d) {
1309 quick_check_performed_ = *d;
1310 }
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001311 void InvalidateCurrentCharacter();
1312 void AdvanceCurrentPositionInTrace(int by, RegExpCompiler* compiler);
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00001313
ager@chromium.org8bb60582008-12-11 12:02:20 +00001314 private:
1315 int FindAffectedRegisters(OutSet* affected_registers);
1316 void PerformDeferredActions(RegExpMacroAssembler* macro,
1317 int max_register,
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001318 OutSet& affected_registers,
1319 OutSet* registers_to_pop,
1320 OutSet* registers_to_clear);
ager@chromium.org8bb60582008-12-11 12:02:20 +00001321 void RestoreAffectedRegisters(RegExpMacroAssembler* macro,
1322 int max_register,
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001323 OutSet& registers_to_pop,
1324 OutSet& registers_to_clear);
ager@chromium.org8bb60582008-12-11 12:02:20 +00001325 int cp_offset_;
1326 DeferredAction* actions_;
1327 Label* backtrack_;
1328 RegExpNode* stop_node_;
1329 Label* loop_label_;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001330 int characters_preloaded_;
1331 int bound_checked_up_to_;
1332 QuickCheckDetails quick_check_performed_;
ager@chromium.org381abbb2009-02-25 13:23:22 +00001333 int flush_budget_;
iposva@chromium.org245aa852009-02-10 00:49:54 +00001334 TriBool at_start_;
ager@chromium.org8bb60582008-12-11 12:02:20 +00001335};
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001336
1337
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001338class NodeVisitor {
1339 public:
1340 virtual ~NodeVisitor() { }
1341#define DECLARE_VISIT(Type) \
1342 virtual void Visit##Type(Type##Node* that) = 0;
1343FOR_EACH_NODE_TYPE(DECLARE_VISIT)
1344#undef DECLARE_VISIT
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001345 virtual void VisitLoopChoice(LoopChoiceNode* that) { VisitChoice(that); }
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001346};
1347
1348
1349// Node visitor used to add the start set of the alternatives to the
1350// dispatch table of a choice node.
1351class DispatchTableConstructor: public NodeVisitor {
1352 public:
1353 DispatchTableConstructor(DispatchTable* table, bool ignore_case)
1354 : table_(table),
1355 choice_index_(-1),
1356 ignore_case_(ignore_case) { }
1357
1358 void BuildTable(ChoiceNode* node);
1359
1360 void AddRange(CharacterRange range) {
1361 table()->AddRange(range, choice_index_);
1362 }
1363
1364 void AddInverse(ZoneList<CharacterRange>* ranges);
1365
1366#define DECLARE_VISIT(Type) \
1367 virtual void Visit##Type(Type##Node* that);
1368FOR_EACH_NODE_TYPE(DECLARE_VISIT)
1369#undef DECLARE_VISIT
1370
1371 DispatchTable* table() { return table_; }
1372 void set_choice_index(int value) { choice_index_ = value; }
1373
1374 protected:
ager@chromium.org5ec48922009-05-05 07:25:34 +00001375 DispatchTable* table_;
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001376 int choice_index_;
1377 bool ignore_case_;
1378};
1379
1380
ager@chromium.org8bb60582008-12-11 12:02:20 +00001381// Assertion propagation moves information about assertions such as
1382// \b to the affected nodes. For instance, in /.\b./ information must
1383// be propagated to the first '.' that whatever follows needs to know
1384// if it matched a word or a non-word, and to the second '.' that it
1385// has to check if it succeeds a word or non-word. In this case the
1386// result will be something like:
1387//
1388// +-------+ +------------+
1389// | . | | . |
1390// +-------+ ---> +------------+
1391// | word? | | check word |
1392// +-------+ +------------+
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001393class Analysis: public NodeVisitor {
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001394 public:
ager@chromium.org38e4c712009-11-11 09:11:58 +00001395 Analysis(bool ignore_case, bool is_ascii)
1396 : ignore_case_(ignore_case),
1397 is_ascii_(is_ascii),
1398 error_message_(NULL) { }
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001399 void EnsureAnalyzed(RegExpNode* node);
1400
1401#define DECLARE_VISIT(Type) \
1402 virtual void Visit##Type(Type##Node* that);
1403FOR_EACH_NODE_TYPE(DECLARE_VISIT)
1404#undef DECLARE_VISIT
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001405 virtual void VisitLoopChoice(LoopChoiceNode* that);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001406
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00001407 bool has_failed() { return error_message_ != NULL; }
1408 const char* error_message() {
1409 ASSERT(error_message_ != NULL);
1410 return error_message_;
1411 }
1412 void fail(const char* error_message) {
1413 error_message_ = error_message;
1414 }
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00001415
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001416 private:
1417 bool ignore_case_;
ager@chromium.org38e4c712009-11-11 09:11:58 +00001418 bool is_ascii_;
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00001419 const char* error_message_;
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001420
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001421 DISALLOW_IMPLICIT_CONSTRUCTORS(Analysis);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001422};
1423
1424
ager@chromium.org8bb60582008-12-11 12:02:20 +00001425struct RegExpCompileData {
1426 RegExpCompileData()
1427 : tree(NULL),
1428 node(NULL),
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001429 simple(true),
iposva@chromium.org245aa852009-02-10 00:49:54 +00001430 contains_anchor(false),
ager@chromium.org8bb60582008-12-11 12:02:20 +00001431 capture_count(0) { }
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001432 RegExpTree* tree;
ager@chromium.org8bb60582008-12-11 12:02:20 +00001433 RegExpNode* node;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001434 bool simple;
iposva@chromium.org245aa852009-02-10 00:49:54 +00001435 bool contains_anchor;
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001436 Handle<String> error;
1437 int capture_count;
1438};
1439
1440
1441class RegExpEngine: public AllStatic {
1442 public:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001443 struct CompilationResult {
1444 explicit CompilationResult(const char* error_message)
1445 : error_message(error_message),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001446 code(HEAP->the_hole_value()),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001447 num_registers(0) {}
1448 CompilationResult(Object* code, int registers)
1449 : error_message(NULL),
1450 code(code),
1451 num_registers(registers) {}
1452 const char* error_message;
1453 Object* code;
1454 int num_registers;
1455 };
1456
1457 static CompilationResult Compile(RegExpCompileData* input,
1458 bool ignore_case,
1459 bool multiline,
1460 Handle<String> pattern,
1461 bool is_ascii);
ager@chromium.org8bb60582008-12-11 12:02:20 +00001462
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001463 static void DotPrint(const char* label, RegExpNode* node, bool ignore_case);
1464};
1465
1466
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001467class OffsetsVector {
1468 public:
ulan@chromium.org812308e2012-02-29 15:58:45 +00001469 inline OffsetsVector(int num_registers, Isolate* isolate)
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001470 : offsets_vector_length_(num_registers) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001471 if (offsets_vector_length_ > Isolate::kJSRegexpStaticOffsetsVectorSize) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001472 vector_ = NewArray<int>(offsets_vector_length_);
1473 } else {
ulan@chromium.org812308e2012-02-29 15:58:45 +00001474 vector_ = isolate->jsregexp_static_offsets_vector();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001475 }
1476 }
1477 inline ~OffsetsVector() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001478 if (offsets_vector_length_ > Isolate::kJSRegexpStaticOffsetsVectorSize) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001479 DeleteArray(vector_);
1480 vector_ = NULL;
1481 }
1482 }
1483 inline int* vector() { return vector_; }
1484 inline int length() { return offsets_vector_length_; }
1485
1486 static const int kStaticOffsetsVectorSize = 50;
1487
1488 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001489 static Address static_offsets_vector_address(Isolate* isolate) {
1490 return reinterpret_cast<Address>(isolate->jsregexp_static_offsets_vector());
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001491 }
1492
1493 int* vector_;
1494 int offsets_vector_length_;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001495
1496 friend class ExternalReference;
1497};
1498
1499
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001500} } // namespace v8::internal
1501
1502#endif // V8_JSREGEXP_H_