blob: 8f01342616582068672537a01eccd328128fda2a [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2006-2008 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
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +000031#include "macro-assembler.h"
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000032#include "zone-inl.h"
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +000033
kasperl@chromium.org71affb52009-05-26 05:44:31 +000034namespace v8 {
35namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000036
ager@chromium.orga74f0da2008-12-03 16:05:52 +000037
38class RegExpMacroAssembler;
39
40
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000041class RegExpImpl {
42 public:
kasperl@chromium.org68ac0092009-07-09 06:00:35 +000043 // Whether V8 is compiled with native regexp support or not.
44 static bool UsesNativeRegExp() {
45#ifdef V8_NATIVE_REGEXP
46 return true;
ager@chromium.org9085a012009-05-11 19:22:57 +000047#else
kasperl@chromium.org68ac0092009-07-09 06:00:35 +000048 return false;
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000049#endif
50 }
kasperl@chromium.org68ac0092009-07-09 06:00:35 +000051
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000052 // Creates a regular expression literal in the old space.
53 // This function calls the garbage collector if necessary.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000054 static Handle<Object> CreateRegExpLiteral(Handle<JSFunction> constructor,
55 Handle<String> pattern,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000056 Handle<String> flags,
57 bool* has_pending_exception);
58
59 // Returns a string representation of a regular expression.
60 // Implements RegExp.prototype.toString, see ECMA-262 section 15.10.6.4.
61 // This function calls the garbage collector if necessary.
62 static Handle<String> ToString(Handle<Object> value);
63
ager@chromium.org8bb60582008-12-11 12:02:20 +000064 // Parses the RegExp pattern and prepares the JSRegExp object with
65 // generic data and choice of implementation - as well as what
66 // the implementation wants to store in the data field.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000067 // Returns false if compilation fails.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000068 static Handle<Object> Compile(Handle<JSRegExp> re,
69 Handle<String> pattern,
70 Handle<String> flags);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000071
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000072 // See ECMA-262 section 15.10.6.2.
73 // This function calls the garbage collector if necessary.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000074 static Handle<Object> Exec(Handle<JSRegExp> regexp,
75 Handle<String> subject,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000076 int index,
77 Handle<JSArray> lastMatchInfo);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000078
ager@chromium.org8bb60582008-12-11 12:02:20 +000079 // Prepares a JSRegExp object with Irregexp-specific data.
whesse@chromium.orgcec079d2010-03-22 14:44:04 +000080 static void IrregexpInitialize(Handle<JSRegExp> re,
81 Handle<String> pattern,
82 JSRegExp::Flags flags,
83 int capture_register_count);
ager@chromium.orga74f0da2008-12-03 16:05:52 +000084
85
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000086 static void AtomCompile(Handle<JSRegExp> re,
87 Handle<String> pattern,
88 JSRegExp::Flags flags,
89 Handle<String> match_pattern);
90
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000091 static Handle<Object> AtomExec(Handle<JSRegExp> regexp,
92 Handle<String> subject,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000093 int index,
94 Handle<JSArray> lastMatchInfo);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000095
whesse@chromium.orgcec079d2010-03-22 14:44:04 +000096 enum IrregexpResult { RE_FAILURE = 0, RE_SUCCESS = 1, RE_EXCEPTION = -1 };
97
98 // Prepare a RegExp for being executed one or more times (using
99 // IrregexpExecOnce) on the subject.
100 // This ensures that the regexp is compiled for the subject, and that
101 // the subject is flat.
102 // Returns the number of integer spaces required by IrregexpExecOnce
103 // as its "registers" argument. If the regexp cannot be compiled,
104 // an exception is set as pending, and this function returns negative.
105 static int IrregexpPrepare(Handle<JSRegExp> regexp,
106 Handle<String> subject);
107
108 // Execute a regular expression once on the subject, starting from
109 // character "index".
110 // If successful, returns RE_SUCCESS and set the capture positions
111 // in the first registers.
112 // If matching fails, returns RE_FAILURE.
113 // If execution fails, sets a pending exception and returns RE_EXCEPTION.
114 static IrregexpResult IrregexpExecOnce(Handle<JSRegExp> regexp,
115 Handle<String> subject,
116 int index,
117 Vector<int32_t> registers);
118
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000119 // Execute an Irregexp bytecode pattern.
ager@chromium.org41826e72009-03-30 13:30:57 +0000120 // On a successful match, the result is a JSArray containing
121 // captured positions. On a failure, the result is the null value.
122 // Returns an empty handle in case of an exception.
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000123 static Handle<Object> IrregexpExec(Handle<JSRegExp> regexp,
124 Handle<String> subject,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000125 int index,
126 Handle<JSArray> lastMatchInfo);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000127
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000128 // Array index in the lastMatchInfo array.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000129 static const int kLastCaptureCount = 0;
130 static const int kLastSubject = 1;
131 static const int kLastInput = 2;
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000132 static const int kFirstCapture = 3;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000133 static const int kLastMatchOverhead = 3;
134
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000135 // Direct offset into the lastMatchInfo array.
136 static const int kLastCaptureCountOffset =
137 FixedArray::kHeaderSize + kLastCaptureCount * kPointerSize;
138 static const int kLastSubjectOffset =
139 FixedArray::kHeaderSize + kLastSubject * kPointerSize;
140 static const int kLastInputOffset =
141 FixedArray::kHeaderSize + kLastInput * kPointerSize;
142 static const int kFirstCaptureOffset =
143 FixedArray::kHeaderSize + kFirstCapture * kPointerSize;
144
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000145 // Used to access the lastMatchInfo array.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000146 static int GetCapture(FixedArray* array, int index) {
147 return Smi::cast(array->get(index + kFirstCapture))->value();
148 }
149
150 static void SetLastCaptureCount(FixedArray* array, int to) {
151 array->set(kLastCaptureCount, Smi::FromInt(to));
152 }
153
154 static void SetLastSubject(FixedArray* array, String* to) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000155 array->set(kLastSubject, to);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000156 }
157
158 static void SetLastInput(FixedArray* array, String* to) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000159 array->set(kLastInput, to);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000160 }
161
162 static void SetCapture(FixedArray* array, int index, int to) {
163 array->set(index + kFirstCapture, Smi::FromInt(to));
164 }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000165
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000166 static int GetLastCaptureCount(FixedArray* array) {
167 return Smi::cast(array->get(kLastCaptureCount))->value();
168 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000169
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000170 // For acting on the JSRegExp data FixedArray.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000171 static int IrregexpMaxRegisterCount(FixedArray* re);
172 static void SetIrregexpMaxRegisterCount(FixedArray* re, int value);
173 static int IrregexpNumberOfCaptures(FixedArray* re);
174 static int IrregexpNumberOfRegisters(FixedArray* re);
175 static ByteArray* IrregexpByteCode(FixedArray* re, bool is_ascii);
176 static Code* IrregexpNativeCode(FixedArray* re, bool is_ascii);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000177
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000178 private:
179 static String* last_ascii_string_;
180 static String* two_byte_cached_string_;
181
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000182 static bool CompileIrregexp(Handle<JSRegExp> re, bool is_ascii);
183 static inline bool EnsureCompiledIrregexp(Handle<JSRegExp> re, bool is_ascii);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000184
185
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000186 // Set the subject cache. The previous string buffer is not deleted, so the
187 // caller should ensure that it doesn't leak.
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000188 static void SetSubjectCache(String* subject,
189 char* utf8_subject,
190 int uft8_length,
191 int character_position,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000192 int utf8_position);
193
194 // A one element cache of the last utf8_subject string and its length. The
195 // subject JS String object is cached in the heap. We also cache a
196 // translation between position and utf8 position.
197 static char* utf8_subject_cache_;
198 static int utf8_length_cache_;
199 static int utf8_position_;
200 static int character_position_;
201};
202
203
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000204// Represents the location of one element relative to the intersection of
205// two sets. Corresponds to the four areas of a Venn diagram.
206enum ElementInSetsRelation {
207 kInsideNone = 0,
208 kInsideFirst = 1,
209 kInsideSecond = 2,
210 kInsideBoth = 3
211};
212
213
214// Represents the relation of two sets.
215// Sets can be either disjoint, partially or fully overlapping, or equal.
216class SetRelation BASE_EMBEDDED {
217 public:
218 // Relation is represented by a bit saying whether there are elements in
219 // one set that is not in the other, and a bit saying that there are elements
220 // that are in both sets.
221
222 // Location of an element. Corresponds to the internal areas of
223 // a Venn diagram.
224 enum {
225 kInFirst = 1 << kInsideFirst,
226 kInSecond = 1 << kInsideSecond,
227 kInBoth = 1 << kInsideBoth
228 };
229 SetRelation() : bits_(0) {}
230 ~SetRelation() {}
231 // Add the existence of objects in a particular
232 void SetElementsInFirstSet() { bits_ |= kInFirst; }
233 void SetElementsInSecondSet() { bits_ |= kInSecond; }
234 void SetElementsInBothSets() { bits_ |= kInBoth; }
235 // Check the currently known relation of the sets (common functions only,
236 // for other combinations, use value() to get the bits and check them
237 // manually).
238 // Sets are completely disjoint.
239 bool Disjoint() { return (bits_ & kInBoth) == 0; }
240 // Sets are equal.
241 bool Equals() { return (bits_ & (kInFirst | kInSecond)) == 0; }
242 // First set contains second.
243 bool Contains() { return (bits_ & kInSecond) == 0; }
244 // Second set contains first.
245 bool ContainedIn() { return (bits_ & kInFirst) == 0; }
246 bool NonTrivialIntersection() {
247 return (bits_ == (kInFirst | kInSecond | kInBoth));
248 }
249 int value() { return bits_; }
250 private:
251 int bits_;
252};
253
254
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000255class CharacterRange {
256 public:
257 CharacterRange() : from_(0), to_(0) { }
258 // For compatibility with the CHECK_OK macro
259 CharacterRange(void* null) { ASSERT_EQ(NULL, null); } //NOLINT
260 CharacterRange(uc16 from, uc16 to) : from_(from), to_(to) { }
261 static void AddClassEscape(uc16 type, ZoneList<CharacterRange>* ranges);
262 static Vector<const uc16> GetWordBounds();
263 static inline CharacterRange Singleton(uc16 value) {
264 return CharacterRange(value, value);
265 }
266 static inline CharacterRange Range(uc16 from, uc16 to) {
267 ASSERT(from <= to);
268 return CharacterRange(from, to);
269 }
270 static inline CharacterRange Everything() {
271 return CharacterRange(0, 0xFFFF);
272 }
273 bool Contains(uc16 i) { return from_ <= i && i <= to_; }
274 uc16 from() const { return from_; }
275 void set_from(uc16 value) { from_ = value; }
276 uc16 to() const { return to_; }
277 void set_to(uc16 value) { to_ = value; }
278 bool is_valid() { return from_ <= to_; }
ager@chromium.org8bb60582008-12-11 12:02:20 +0000279 bool IsEverything(uc16 max) { return from_ == 0 && to_ >= max; }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000280 bool IsSingleton() { return (from_ == to_); }
ager@chromium.org38e4c712009-11-11 09:11:58 +0000281 void AddCaseEquivalents(ZoneList<CharacterRange>* ranges, bool is_ascii);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000282 static void Split(ZoneList<CharacterRange>* base,
283 Vector<const uc16> overlay,
284 ZoneList<CharacterRange>** included,
285 ZoneList<CharacterRange>** excluded);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000286 // Whether a range list is in canonical form: Ranges ordered by from value,
287 // and ranges non-overlapping and non-adjacent.
288 static bool IsCanonical(ZoneList<CharacterRange>* ranges);
289 // Convert range list to canonical form. The characters covered by the ranges
290 // will still be the same, but no character is in more than one range, and
291 // adjacent ranges are merged. The resulting list may be shorter than the
292 // original, but cannot be longer.
293 static void Canonicalize(ZoneList<CharacterRange>* ranges);
294 // Check how the set of characters defined by a CharacterRange list relates
295 // to the set of word characters. List must be in canonical form.
296 static SetRelation WordCharacterRelation(ZoneList<CharacterRange>* ranges);
297 // Takes two character range lists (representing character sets) in canonical
298 // form and merges them.
299 // The characters that are only covered by the first set are added to
300 // first_set_only_out. the characters that are only in the second set are
301 // added to second_set_only_out, and the characters that are in both are
302 // added to both_sets_out.
303 // The pointers to first_set_only_out, second_set_only_out and both_sets_out
304 // should be to empty lists, but they need not be distinct, and may be NULL.
305 // If NULL, the characters are dropped, and if two arguments are the same
306 // pointer, the result is the union of the two sets that would be created
307 // if the pointers had been distinct.
308 // This way, the Merge function can compute all the usual set operations:
309 // union (all three out-sets are equal), intersection (only both_sets_out is
310 // non-NULL), and set difference (only first_set is non-NULL).
311 static void Merge(ZoneList<CharacterRange>* first_set,
312 ZoneList<CharacterRange>* second_set,
313 ZoneList<CharacterRange>* first_set_only_out,
314 ZoneList<CharacterRange>* second_set_only_out,
315 ZoneList<CharacterRange>* both_sets_out);
316 // Negate the contents of a character range in canonical form.
317 static void Negate(ZoneList<CharacterRange>* src,
318 ZoneList<CharacterRange>* dst);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000319 static const int kRangeCanonicalizeMax = 0x346;
320 static const int kStartMarker = (1 << 24);
321 static const int kPayloadMask = (1 << 24) - 1;
322
323 private:
324 uc16 from_;
325 uc16 to_;
326};
327
328
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000329// A set of unsigned integers that behaves especially well on small
330// integers (< 32). May do zone-allocation.
331class OutSet: public ZoneObject {
332 public:
333 OutSet() : first_(0), remaining_(NULL), successors_(NULL) { }
334 OutSet* Extend(unsigned value);
335 bool Get(unsigned value);
336 static const unsigned kFirstLimit = 32;
337
338 private:
339 // Destructively set a value in this set. In most cases you want
340 // to use Extend instead to ensure that only one instance exists
341 // that contains the same values.
342 void Set(unsigned value);
343
344 // The successors are a list of sets that contain the same values
345 // as this set and the one more value that is not present in this
346 // set.
347 ZoneList<OutSet*>* successors() { return successors_; }
348
349 OutSet(uint32_t first, ZoneList<unsigned>* remaining)
350 : first_(first), remaining_(remaining), successors_(NULL) { }
351 uint32_t first_;
352 ZoneList<unsigned>* remaining_;
353 ZoneList<OutSet*>* successors_;
ager@chromium.org32912102009-01-16 10:38:43 +0000354 friend class Trace;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000355};
356
357
358// A mapping from integers, specified as ranges, to a set of integers.
359// Used for mapping character ranges to choices.
360class DispatchTable : public ZoneObject {
361 public:
362 class Entry {
363 public:
364 Entry() : from_(0), to_(0), out_set_(NULL) { }
365 Entry(uc16 from, uc16 to, OutSet* out_set)
366 : from_(from), to_(to), out_set_(out_set) { }
367 uc16 from() { return from_; }
368 uc16 to() { return to_; }
369 void set_to(uc16 value) { to_ = value; }
370 void AddValue(int value) { out_set_ = out_set_->Extend(value); }
371 OutSet* out_set() { return out_set_; }
372 private:
373 uc16 from_;
374 uc16 to_;
375 OutSet* out_set_;
376 };
377
378 class Config {
379 public:
380 typedef uc16 Key;
381 typedef Entry Value;
382 static const uc16 kNoKey;
383 static const Entry kNoValue;
384 static inline int Compare(uc16 a, uc16 b) {
385 if (a == b)
386 return 0;
387 else if (a < b)
388 return -1;
389 else
390 return 1;
391 }
392 };
393
394 void AddRange(CharacterRange range, int value);
395 OutSet* Get(uc16 value);
396 void Dump();
397
398 template <typename Callback>
399 void ForEach(Callback* callback) { return tree()->ForEach(callback); }
400 private:
401 // There can't be a static empty set since it allocates its
402 // successors in a zone and caches them.
403 OutSet* empty() { return &empty_; }
404 OutSet empty_;
405 ZoneSplayTree<Config>* tree() { return &tree_; }
406 ZoneSplayTree<Config> tree_;
407};
408
409
410#define FOR_EACH_NODE_TYPE(VISIT) \
411 VISIT(End) \
412 VISIT(Action) \
413 VISIT(Choice) \
414 VISIT(BackReference) \
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000415 VISIT(Assertion) \
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000416 VISIT(Text)
417
418
419#define FOR_EACH_REG_EXP_TREE_TYPE(VISIT) \
420 VISIT(Disjunction) \
421 VISIT(Alternative) \
422 VISIT(Assertion) \
423 VISIT(CharacterClass) \
424 VISIT(Atom) \
425 VISIT(Quantifier) \
426 VISIT(Capture) \
427 VISIT(Lookahead) \
428 VISIT(BackReference) \
429 VISIT(Empty) \
430 VISIT(Text)
431
432
433#define FORWARD_DECLARE(Name) class RegExp##Name;
434FOR_EACH_REG_EXP_TREE_TYPE(FORWARD_DECLARE)
435#undef FORWARD_DECLARE
436
437
438class TextElement {
439 public:
440 enum Type {UNINITIALIZED, ATOM, CHAR_CLASS};
441 TextElement() : type(UNINITIALIZED) { }
ager@chromium.org8bb60582008-12-11 12:02:20 +0000442 explicit TextElement(Type t) : type(t), cp_offset(-1) { }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000443 static TextElement Atom(RegExpAtom* atom);
444 static TextElement CharClass(RegExpCharacterClass* char_class);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000445 int length();
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000446 Type type;
447 union {
448 RegExpAtom* u_atom;
449 RegExpCharacterClass* u_char_class;
450 } data;
ager@chromium.org8bb60582008-12-11 12:02:20 +0000451 int cp_offset;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000452};
453
454
ager@chromium.org32912102009-01-16 10:38:43 +0000455class Trace;
ager@chromium.org8bb60582008-12-11 12:02:20 +0000456
457
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000458struct NodeInfo {
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000459 NodeInfo()
460 : being_analyzed(false),
461 been_analyzed(false),
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000462 follows_word_interest(false),
463 follows_newline_interest(false),
464 follows_start_interest(false),
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000465 at_end(false),
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000466 visited(false) { }
467
468 // Returns true if the interests and assumptions of this node
469 // matches the given one.
470 bool Matches(NodeInfo* that) {
471 return (at_end == that->at_end) &&
472 (follows_word_interest == that->follows_word_interest) &&
473 (follows_newline_interest == that->follows_newline_interest) &&
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000474 (follows_start_interest == that->follows_start_interest);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000475 }
476
477 // Updates the interests of this node given the interests of the
478 // node preceding it.
479 void AddFromPreceding(NodeInfo* that) {
480 at_end |= that->at_end;
481 follows_word_interest |= that->follows_word_interest;
482 follows_newline_interest |= that->follows_newline_interest;
483 follows_start_interest |= that->follows_start_interest;
484 }
485
ager@chromium.org8bb60582008-12-11 12:02:20 +0000486 bool HasLookbehind() {
487 return follows_word_interest ||
488 follows_newline_interest ||
489 follows_start_interest;
490 }
491
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000492 // Sets the interests of this node to include the interests of the
493 // following node.
494 void AddFromFollowing(NodeInfo* that) {
495 follows_word_interest |= that->follows_word_interest;
496 follows_newline_interest |= that->follows_newline_interest;
497 follows_start_interest |= that->follows_start_interest;
498 }
499
500 void ResetCompilationState() {
501 being_analyzed = false;
502 been_analyzed = false;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000503 }
504
505 bool being_analyzed: 1;
506 bool been_analyzed: 1;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000507
508 // These bits are set of this node has to know what the preceding
509 // character was.
510 bool follows_word_interest: 1;
511 bool follows_newline_interest: 1;
512 bool follows_start_interest: 1;
513
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000514 bool at_end: 1;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000515 bool visited: 1;
516};
517
518
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000519class SiblingList {
520 public:
521 SiblingList() : list_(NULL) { }
522 int length() {
523 return list_ == NULL ? 0 : list_->length();
524 }
525 void Ensure(RegExpNode* parent) {
526 if (list_ == NULL) {
527 list_ = new ZoneList<RegExpNode*>(2);
528 list_->Add(parent);
529 }
530 }
531 void Add(RegExpNode* node) { list_->Add(node); }
532 RegExpNode* Get(int index) { return list_->at(index); }
533 private:
534 ZoneList<RegExpNode*>* list_;
535};
536
537
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000538// Details of a quick mask-compare check that can look ahead in the
539// input stream.
540class QuickCheckDetails {
541 public:
542 QuickCheckDetails()
543 : characters_(0),
544 mask_(0),
iposva@chromium.org245aa852009-02-10 00:49:54 +0000545 value_(0),
546 cannot_match_(false) { }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000547 explicit QuickCheckDetails(int characters)
548 : characters_(characters),
549 mask_(0),
iposva@chromium.org245aa852009-02-10 00:49:54 +0000550 value_(0),
551 cannot_match_(false) { }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000552 bool Rationalize(bool ascii);
553 // Merge in the information from another branch of an alternation.
554 void Merge(QuickCheckDetails* other, int from_index);
555 // Advance the current position by some amount.
556 void Advance(int by, bool ascii);
557 void Clear();
iposva@chromium.org245aa852009-02-10 00:49:54 +0000558 bool cannot_match() { return cannot_match_; }
559 void set_cannot_match() { cannot_match_ = true; }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000560 struct Position {
561 Position() : mask(0), value(0), determines_perfectly(false) { }
562 uc16 mask;
563 uc16 value;
564 bool determines_perfectly;
565 };
566 int characters() { return characters_; }
567 void set_characters(int characters) { characters_ = characters; }
568 Position* positions(int index) {
569 ASSERT(index >= 0);
570 ASSERT(index < characters_);
571 return positions_ + index;
572 }
573 uint32_t mask() { return mask_; }
574 uint32_t value() { return value_; }
575
576 private:
577 // How many characters do we have quick check information from. This is
578 // the same for all branches of a choice node.
579 int characters_;
580 Position positions_[4];
581 // These values are the condensate of the above array after Rationalize().
582 uint32_t mask_;
583 uint32_t value_;
iposva@chromium.org245aa852009-02-10 00:49:54 +0000584 // If set to true, there is no way this quick check can match at all.
585 // E.g., if it requires to be at the start of the input, and isn't.
586 bool cannot_match_;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000587};
588
589
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000590class RegExpNode: public ZoneObject {
591 public:
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000592 RegExpNode() : first_character_set_(NULL), trace_count_(0) { }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000593 virtual ~RegExpNode();
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000594 virtual void Accept(NodeVisitor* visitor) = 0;
595 // Generates a goto to this node or actually generates the code at this point.
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000596 virtual void Emit(RegExpCompiler* compiler, Trace* trace) = 0;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000597 // How many characters must this node consume at a minimum in order to
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000598 // succeed. If we have found at least 'still_to_find' characters that
599 // must be consumed there is no need to ask any following nodes whether
600 // they are sure to eat any more characters.
601 virtual int EatsAtLeast(int still_to_find, int recursion_depth) = 0;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000602 // Emits some quick code that checks whether the preloaded characters match.
603 // Falls through on certain failure, jumps to the label on possible success.
604 // If the node cannot make a quick check it does nothing and returns false.
605 bool EmitQuickCheck(RegExpCompiler* compiler,
ager@chromium.org32912102009-01-16 10:38:43 +0000606 Trace* trace,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000607 bool preload_has_checked_bounds,
608 Label* on_possible_success,
609 QuickCheckDetails* details_return,
610 bool fall_through_on_failure);
611 // For a given number of characters this returns a mask and a value. The
612 // next n characters are anded with the mask and compared with the value.
613 // A comparison failure indicates the node cannot match the next n characters.
614 // A comparison success indicates the node may match.
615 virtual void GetQuickCheckDetails(QuickCheckDetails* details,
616 RegExpCompiler* compiler,
iposva@chromium.org245aa852009-02-10 00:49:54 +0000617 int characters_filled_in,
618 bool not_at_start) = 0;
ager@chromium.org8bb60582008-12-11 12:02:20 +0000619 static const int kNodeIsTooComplexForGreedyLoops = -1;
620 virtual int GreedyLoopTextLength() { return kNodeIsTooComplexForGreedyLoops; }
621 Label* label() { return &label_; }
ager@chromium.org32912102009-01-16 10:38:43 +0000622 // If non-generic code is generated for a node (ie the node is not at the
623 // start of the trace) then it cannot be reused. This variable sets a limit
624 // on how often we allow that to happen before we insist on starting a new
625 // trace and generating generic code for a node that can be reused by flushing
626 // the deferred actions in the current trace and generating a goto.
627 static const int kMaxCopiesCodeGenerated = 10;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000628
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000629 NodeInfo* info() { return &info_; }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000630
631 void AddSibling(RegExpNode* node) { siblings_.Add(node); }
632
633 // Static version of EnsureSibling that expresses the fact that the
634 // result has the same type as the input.
635 template <class C>
636 static C* EnsureSibling(C* node, NodeInfo* info, bool* cloned) {
637 return static_cast<C*>(node->EnsureSibling(info, cloned));
638 }
639
640 SiblingList* siblings() { return &siblings_; }
641 void set_siblings(SiblingList* other) { siblings_ = *other; }
642
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000643 // Return the set of possible next characters recognized by the regexp
644 // (or a safe subset, potentially the set of all characters).
645 ZoneList<CharacterRange>* FirstCharacterSet();
646
647 // Compute (if possible within the budget of traversed nodes) the
648 // possible first characters of the input matched by this node and
649 // its continuation. Returns the remaining budget after the computation.
650 // If the budget is spent, the result is negative, and the cached
651 // first_character_set_ value isn't set.
652 virtual int ComputeFirstCharacterSet(int budget);
653
654 // Get and set the cached first character set value.
655 ZoneList<CharacterRange>* first_character_set() {
656 return first_character_set_;
657 }
658 void set_first_character_set(ZoneList<CharacterRange>* character_set) {
659 first_character_set_ = character_set;
660 }
661
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000662 protected:
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000663 enum LimitResult { DONE, CONTINUE };
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000664 static const int kComputeFirstCharacterSetFail = -1;
665
ager@chromium.org32912102009-01-16 10:38:43 +0000666 LimitResult LimitVersions(RegExpCompiler* compiler, Trace* trace);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000667
668 // Returns a sibling of this node whose interests and assumptions
669 // match the ones in the given node info. If no sibling exists NULL
670 // is returned.
671 RegExpNode* TryGetSibling(NodeInfo* info);
672
673 // Returns a sibling of this node whose interests match the ones in
674 // the given node info. The info must not contain any assertions.
675 // If no node exists a new one will be created by cloning the current
676 // node. The result will always be an instance of the same concrete
677 // class as this node.
678 RegExpNode* EnsureSibling(NodeInfo* info, bool* cloned);
679
680 // Returns a clone of this node initialized using the copy constructor
681 // of its concrete class. Note that the node may have to be pre-
ager@chromium.org32912102009-01-16 10:38:43 +0000682 // processed before it is on a usable state.
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000683 virtual RegExpNode* Clone() = 0;
684
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000685 private:
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000686 static const int kFirstCharBudget = 10;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000687 Label label_;
688 NodeInfo info_;
689 SiblingList siblings_;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000690 ZoneList<CharacterRange>* first_character_set_;
ager@chromium.org32912102009-01-16 10:38:43 +0000691 // This variable keeps track of how many times code has been generated for
692 // this node (in different traces). We don't keep track of where the
693 // generated code is located unless the code is generated at the start of
694 // a trace, in which case it is generic and can be reused by flushing the
695 // deferred operations in the current trace and generating a goto.
696 int trace_count_;
697};
698
699
700// A simple closed interval.
701class Interval {
702 public:
703 Interval() : from_(kNone), to_(kNone) { }
704 Interval(int from, int to) : from_(from), to_(to) { }
705 Interval Union(Interval that) {
706 if (that.from_ == kNone)
707 return *this;
708 else if (from_ == kNone)
709 return that;
710 else
711 return Interval(Min(from_, that.from_), Max(to_, that.to_));
712 }
713 bool Contains(int value) {
714 return (from_ <= value) && (value <= to_);
715 }
716 bool is_empty() { return from_ == kNone; }
717 int from() { return from_; }
718 int to() { return to_; }
719 static Interval Empty() { return Interval(); }
720 static const int kNone = -1;
721 private:
722 int from_;
723 int to_;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000724};
725
726
727class SeqRegExpNode: public RegExpNode {
728 public:
729 explicit SeqRegExpNode(RegExpNode* on_success)
730 : on_success_(on_success) { }
731 RegExpNode* on_success() { return on_success_; }
732 void set_on_success(RegExpNode* node) { on_success_ = node; }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000733 private:
734 RegExpNode* on_success_;
735};
736
737
738class ActionNode: public SeqRegExpNode {
739 public:
740 enum Type {
ager@chromium.org8bb60582008-12-11 12:02:20 +0000741 SET_REGISTER,
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000742 INCREMENT_REGISTER,
743 STORE_POSITION,
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000744 BEGIN_SUBMATCH,
ager@chromium.org32912102009-01-16 10:38:43 +0000745 POSITIVE_SUBMATCH_SUCCESS,
746 EMPTY_MATCH_CHECK,
747 CLEAR_CAPTURES
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000748 };
ager@chromium.org8bb60582008-12-11 12:02:20 +0000749 static ActionNode* SetRegister(int reg, int val, RegExpNode* on_success);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000750 static ActionNode* IncrementRegister(int reg, RegExpNode* on_success);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000751 static ActionNode* StorePosition(int reg,
752 bool is_capture,
753 RegExpNode* on_success);
ager@chromium.org32912102009-01-16 10:38:43 +0000754 static ActionNode* ClearCaptures(Interval range, RegExpNode* on_success);
755 static ActionNode* BeginSubmatch(int stack_pointer_reg,
756 int position_reg,
757 RegExpNode* on_success);
758 static ActionNode* PositiveSubmatchSuccess(int stack_pointer_reg,
759 int restore_reg,
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000760 int clear_capture_count,
761 int clear_capture_from,
ager@chromium.org32912102009-01-16 10:38:43 +0000762 RegExpNode* on_success);
763 static ActionNode* EmptyMatchCheck(int start_register,
764 int repetition_register,
765 int repetition_limit,
766 RegExpNode* on_success);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000767 virtual void Accept(NodeVisitor* visitor);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000768 virtual void Emit(RegExpCompiler* compiler, Trace* trace);
769 virtual int EatsAtLeast(int still_to_find, int recursion_depth);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000770 virtual void GetQuickCheckDetails(QuickCheckDetails* details,
771 RegExpCompiler* compiler,
iposva@chromium.org245aa852009-02-10 00:49:54 +0000772 int filled_in,
773 bool not_at_start) {
774 return on_success()->GetQuickCheckDetails(
775 details, compiler, filled_in, not_at_start);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000776 }
ager@chromium.org8bb60582008-12-11 12:02:20 +0000777 Type type() { return type_; }
778 // TODO(erikcorry): We should allow some action nodes in greedy loops.
779 virtual int GreedyLoopTextLength() { return kNodeIsTooComplexForGreedyLoops; }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000780 virtual ActionNode* Clone() { return new ActionNode(*this); }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000781 virtual int ComputeFirstCharacterSet(int budget);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000782 private:
783 union {
784 struct {
785 int reg;
786 int value;
787 } u_store_register;
788 struct {
789 int reg;
790 } u_increment_register;
791 struct {
792 int reg;
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000793 bool is_capture;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000794 } u_position_register;
795 struct {
796 int stack_pointer_register;
797 int current_position_register;
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000798 int clear_register_count;
799 int clear_register_from;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000800 } u_submatch;
ager@chromium.org32912102009-01-16 10:38:43 +0000801 struct {
802 int start_register;
803 int repetition_register;
804 int repetition_limit;
805 } u_empty_match_check;
806 struct {
807 int range_from;
808 int range_to;
809 } u_clear_captures;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000810 } data_;
811 ActionNode(Type type, RegExpNode* on_success)
812 : SeqRegExpNode(on_success),
813 type_(type) { }
814 Type type_;
815 friend class DotPrinter;
816};
817
818
819class TextNode: public SeqRegExpNode {
820 public:
821 TextNode(ZoneList<TextElement>* elms,
ager@chromium.org8bb60582008-12-11 12:02:20 +0000822 RegExpNode* on_success)
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000823 : SeqRegExpNode(on_success),
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000824 elms_(elms) { }
825 TextNode(RegExpCharacterClass* that,
ager@chromium.org8bb60582008-12-11 12:02:20 +0000826 RegExpNode* on_success)
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000827 : SeqRegExpNode(on_success),
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000828 elms_(new ZoneList<TextElement>(1)) {
829 elms_->Add(TextElement::CharClass(that));
830 }
831 virtual void Accept(NodeVisitor* visitor);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000832 virtual void Emit(RegExpCompiler* compiler, Trace* trace);
833 virtual int EatsAtLeast(int still_to_find, int recursion_depth);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000834 virtual void GetQuickCheckDetails(QuickCheckDetails* details,
835 RegExpCompiler* compiler,
iposva@chromium.org245aa852009-02-10 00:49:54 +0000836 int characters_filled_in,
837 bool not_at_start);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000838 ZoneList<TextElement>* elements() { return elms_; }
ager@chromium.org38e4c712009-11-11 09:11:58 +0000839 void MakeCaseIndependent(bool is_ascii);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000840 virtual int GreedyLoopTextLength();
841 virtual TextNode* Clone() {
842 TextNode* result = new TextNode(*this);
843 result->CalculateOffsets();
844 return result;
845 }
846 void CalculateOffsets();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000847 virtual int ComputeFirstCharacterSet(int budget);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000848 private:
849 enum TextEmitPassType {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000850 NON_ASCII_MATCH, // Check for characters that can't match.
851 SIMPLE_CHARACTER_MATCH, // Case-dependent single character check.
852 NON_LETTER_CHARACTER_MATCH, // Check characters that have no case equivs.
853 CASE_CHARACTER_MATCH, // Case-independent single character check.
854 CHARACTER_CLASS_MATCH // Character class.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000855 };
ager@chromium.org381abbb2009-02-25 13:23:22 +0000856 static bool SkipPass(int pass, bool ignore_case);
857 static const int kFirstRealPass = SIMPLE_CHARACTER_MATCH;
858 static const int kLastPass = CHARACTER_CLASS_MATCH;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000859 void TextEmitPass(RegExpCompiler* compiler,
860 TextEmitPassType pass,
861 bool preloaded,
ager@chromium.org32912102009-01-16 10:38:43 +0000862 Trace* trace,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000863 bool first_element_checked,
864 int* checked_up_to);
865 int Length();
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000866 ZoneList<TextElement>* elms_;
867};
868
869
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000870class AssertionNode: public SeqRegExpNode {
871 public:
872 enum AssertionNodeType {
873 AT_END,
874 AT_START,
875 AT_BOUNDARY,
876 AT_NON_BOUNDARY,
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000877 AFTER_NEWLINE,
878 // Types not directly expressible in regexp syntax.
879 // Used for modifying a boundary node if its following character is
880 // known to be word and/or non-word.
881 AFTER_NONWORD_CHARACTER,
882 AFTER_WORD_CHARACTER
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000883 };
884 static AssertionNode* AtEnd(RegExpNode* on_success) {
885 return new AssertionNode(AT_END, on_success);
886 }
887 static AssertionNode* AtStart(RegExpNode* on_success) {
888 return new AssertionNode(AT_START, on_success);
889 }
890 static AssertionNode* AtBoundary(RegExpNode* on_success) {
891 return new AssertionNode(AT_BOUNDARY, on_success);
892 }
893 static AssertionNode* AtNonBoundary(RegExpNode* on_success) {
894 return new AssertionNode(AT_NON_BOUNDARY, on_success);
895 }
896 static AssertionNode* AfterNewline(RegExpNode* on_success) {
897 return new AssertionNode(AFTER_NEWLINE, on_success);
898 }
899 virtual void Accept(NodeVisitor* visitor);
900 virtual void Emit(RegExpCompiler* compiler, Trace* trace);
901 virtual int EatsAtLeast(int still_to_find, int recursion_depth);
902 virtual void GetQuickCheckDetails(QuickCheckDetails* details,
903 RegExpCompiler* compiler,
iposva@chromium.org245aa852009-02-10 00:49:54 +0000904 int filled_in,
905 bool not_at_start);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000906 virtual int ComputeFirstCharacterSet(int budget);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000907 virtual AssertionNode* Clone() { return new AssertionNode(*this); }
908 AssertionNodeType type() { return type_; }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000909 void set_type(AssertionNodeType type) { type_ = type; }
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000910 private:
911 AssertionNode(AssertionNodeType t, RegExpNode* on_success)
912 : SeqRegExpNode(on_success), type_(t) { }
913 AssertionNodeType type_;
914};
915
916
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000917class BackReferenceNode: public SeqRegExpNode {
918 public:
919 BackReferenceNode(int start_reg,
920 int end_reg,
ager@chromium.org8bb60582008-12-11 12:02:20 +0000921 RegExpNode* on_success)
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000922 : SeqRegExpNode(on_success),
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000923 start_reg_(start_reg),
924 end_reg_(end_reg) { }
925 virtual void Accept(NodeVisitor* visitor);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000926 int start_register() { return start_reg_; }
927 int end_register() { return end_reg_; }
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000928 virtual void Emit(RegExpCompiler* compiler, Trace* trace);
929 virtual int EatsAtLeast(int still_to_find, int recursion_depth);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000930 virtual void GetQuickCheckDetails(QuickCheckDetails* details,
931 RegExpCompiler* compiler,
iposva@chromium.org245aa852009-02-10 00:49:54 +0000932 int characters_filled_in,
933 bool not_at_start) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000934 return;
935 }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000936 virtual BackReferenceNode* Clone() { return new BackReferenceNode(*this); }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000937 virtual int ComputeFirstCharacterSet(int budget);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000938 private:
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000939 int start_reg_;
940 int end_reg_;
941};
942
943
944class EndNode: public RegExpNode {
945 public:
ager@chromium.org8bb60582008-12-11 12:02:20 +0000946 enum Action { ACCEPT, BACKTRACK, NEGATIVE_SUBMATCH_SUCCESS };
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000947 explicit EndNode(Action action) : action_(action) { }
948 virtual void Accept(NodeVisitor* visitor);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000949 virtual void Emit(RegExpCompiler* compiler, Trace* trace);
950 virtual int EatsAtLeast(int still_to_find, int recursion_depth) { return 0; }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000951 virtual void GetQuickCheckDetails(QuickCheckDetails* details,
952 RegExpCompiler* compiler,
iposva@chromium.org245aa852009-02-10 00:49:54 +0000953 int characters_filled_in,
954 bool not_at_start) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000955 // Returning 0 from EatsAtLeast should ensure we never get here.
956 UNREACHABLE();
957 }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000958 virtual EndNode* Clone() { return new EndNode(*this); }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000959 private:
960 Action action_;
961};
962
963
ager@chromium.org8bb60582008-12-11 12:02:20 +0000964class NegativeSubmatchSuccess: public EndNode {
965 public:
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000966 NegativeSubmatchSuccess(int stack_pointer_reg,
967 int position_reg,
968 int clear_capture_count,
969 int clear_capture_start)
ager@chromium.org8bb60582008-12-11 12:02:20 +0000970 : EndNode(NEGATIVE_SUBMATCH_SUCCESS),
971 stack_pointer_register_(stack_pointer_reg),
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000972 current_position_register_(position_reg),
973 clear_capture_count_(clear_capture_count),
974 clear_capture_start_(clear_capture_start) { }
975 virtual void Emit(RegExpCompiler* compiler, Trace* trace);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000976
977 private:
978 int stack_pointer_register_;
979 int current_position_register_;
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000980 int clear_capture_count_;
981 int clear_capture_start_;
ager@chromium.org8bb60582008-12-11 12:02:20 +0000982};
983
984
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000985class Guard: public ZoneObject {
986 public:
987 enum Relation { LT, GEQ };
988 Guard(int reg, Relation op, int value)
989 : reg_(reg),
990 op_(op),
991 value_(value) { }
992 int reg() { return reg_; }
993 Relation op() { return op_; }
994 int value() { return value_; }
995
996 private:
997 int reg_;
998 Relation op_;
999 int value_;
1000};
1001
1002
1003class GuardedAlternative {
1004 public:
1005 explicit GuardedAlternative(RegExpNode* node) : node_(node), guards_(NULL) { }
1006 void AddGuard(Guard* guard);
1007 RegExpNode* node() { return node_; }
1008 void set_node(RegExpNode* node) { node_ = node; }
1009 ZoneList<Guard*>* guards() { return guards_; }
1010
1011 private:
1012 RegExpNode* node_;
1013 ZoneList<Guard*>* guards_;
1014};
1015
1016
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001017class AlternativeGeneration;
1018
1019
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001020class ChoiceNode: public RegExpNode {
1021 public:
ager@chromium.org8bb60582008-12-11 12:02:20 +00001022 explicit ChoiceNode(int expected_size)
1023 : alternatives_(new ZoneList<GuardedAlternative>(expected_size)),
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001024 table_(NULL),
iposva@chromium.org245aa852009-02-10 00:49:54 +00001025 not_at_start_(false),
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001026 being_calculated_(false) { }
1027 virtual void Accept(NodeVisitor* visitor);
1028 void AddAlternative(GuardedAlternative node) { alternatives()->Add(node); }
1029 ZoneList<GuardedAlternative>* alternatives() { return alternatives_; }
1030 DispatchTable* GetTable(bool ignore_case);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001031 virtual void Emit(RegExpCompiler* compiler, Trace* trace);
1032 virtual int EatsAtLeast(int still_to_find, int recursion_depth);
1033 int EatsAtLeastHelper(int still_to_find,
1034 int recursion_depth,
1035 RegExpNode* ignore_this_node);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001036 virtual void GetQuickCheckDetails(QuickCheckDetails* details,
1037 RegExpCompiler* compiler,
iposva@chromium.org245aa852009-02-10 00:49:54 +00001038 int characters_filled_in,
1039 bool not_at_start);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001040 virtual ChoiceNode* Clone() { return new ChoiceNode(*this); }
1041
1042 bool being_calculated() { return being_calculated_; }
iposva@chromium.org245aa852009-02-10 00:49:54 +00001043 bool not_at_start() { return not_at_start_; }
1044 void set_not_at_start() { not_at_start_ = true; }
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001045 void set_being_calculated(bool b) { being_calculated_ = b; }
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001046 virtual bool try_to_emit_quick_check_for_alternative(int i) { return true; }
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001047
ager@chromium.org8bb60582008-12-11 12:02:20 +00001048 protected:
ager@chromium.org5ec48922009-05-05 07:25:34 +00001049 int GreedyLoopTextLength(GuardedAlternative* alternative);
ager@chromium.org8bb60582008-12-11 12:02:20 +00001050 ZoneList<GuardedAlternative>* alternatives_;
1051
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001052 private:
1053 friend class DispatchTableConstructor;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001054 friend class Analysis;
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001055 void GenerateGuard(RegExpMacroAssembler* macro_assembler,
ager@chromium.org5ec48922009-05-05 07:25:34 +00001056 Guard* guard,
ager@chromium.org32912102009-01-16 10:38:43 +00001057 Trace* trace);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001058 int CalculatePreloadCharacters(RegExpCompiler* compiler);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001059 void EmitOutOfLineContinuation(RegExpCompiler* compiler,
ager@chromium.org32912102009-01-16 10:38:43 +00001060 Trace* trace,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001061 GuardedAlternative alternative,
1062 AlternativeGeneration* alt_gen,
1063 int preload_characters,
1064 bool next_expects_preload);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001065 DispatchTable* table_;
iposva@chromium.org245aa852009-02-10 00:49:54 +00001066 // If true, this node is never checked at the start of the input.
1067 // Allows a new trace to start with at_start() set to false.
1068 bool not_at_start_;
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001069 bool being_calculated_;
1070};
1071
1072
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001073class NegativeLookaheadChoiceNode: public ChoiceNode {
1074 public:
1075 explicit NegativeLookaheadChoiceNode(GuardedAlternative this_must_fail,
1076 GuardedAlternative then_do_this)
1077 : ChoiceNode(2) {
1078 AddAlternative(this_must_fail);
1079 AddAlternative(then_do_this);
1080 }
1081 virtual int EatsAtLeast(int still_to_find, int recursion_depth);
1082 virtual void GetQuickCheckDetails(QuickCheckDetails* details,
1083 RegExpCompiler* compiler,
iposva@chromium.org245aa852009-02-10 00:49:54 +00001084 int characters_filled_in,
1085 bool not_at_start);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001086 // For a negative lookahead we don't emit the quick check for the
1087 // alternative that is expected to fail. This is because quick check code
1088 // starts by loading enough characters for the alternative that takes fewest
1089 // characters, but on a negative lookahead the negative branch did not take
1090 // part in that calculation (EatsAtLeast) so the assumptions don't hold.
1091 virtual bool try_to_emit_quick_check_for_alternative(int i) { return i != 0; }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001092 virtual int ComputeFirstCharacterSet(int budget);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001093};
1094
1095
ager@chromium.org8bb60582008-12-11 12:02:20 +00001096class LoopChoiceNode: public ChoiceNode {
1097 public:
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001098 explicit LoopChoiceNode(bool body_can_be_zero_length)
1099 : ChoiceNode(2),
1100 loop_node_(NULL),
1101 continue_node_(NULL),
1102 body_can_be_zero_length_(body_can_be_zero_length) { }
1103 void AddLoopAlternative(GuardedAlternative alt);
1104 void AddContinueAlternative(GuardedAlternative alt);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001105 virtual void Emit(RegExpCompiler* compiler, Trace* trace);
1106 virtual int EatsAtLeast(int still_to_find, int recursion_depth);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001107 virtual void GetQuickCheckDetails(QuickCheckDetails* details,
1108 RegExpCompiler* compiler,
iposva@chromium.org245aa852009-02-10 00:49:54 +00001109 int characters_filled_in,
1110 bool not_at_start);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001111 virtual int ComputeFirstCharacterSet(int budget);
ager@chromium.org8bb60582008-12-11 12:02:20 +00001112 virtual LoopChoiceNode* Clone() { return new LoopChoiceNode(*this); }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001113 RegExpNode* loop_node() { return loop_node_; }
1114 RegExpNode* continue_node() { return continue_node_; }
1115 bool body_can_be_zero_length() { return body_can_be_zero_length_; }
1116 virtual void Accept(NodeVisitor* visitor);
1117
1118 private:
1119 // AddAlternative is made private for loop nodes because alternatives
1120 // should not be added freely, we need to keep track of which node
1121 // goes back to the node itself.
1122 void AddAlternative(GuardedAlternative node) {
1123 ChoiceNode::AddAlternative(node);
1124 }
1125
1126 RegExpNode* loop_node_;
1127 RegExpNode* continue_node_;
1128 bool body_can_be_zero_length_;
ager@chromium.org8bb60582008-12-11 12:02:20 +00001129};
1130
1131
1132// There are many ways to generate code for a node. This class encapsulates
1133// the current way we should be generating. In other words it encapsulates
ager@chromium.org32912102009-01-16 10:38:43 +00001134// the current state of the code generator. The effect of this is that we
1135// generate code for paths that the matcher can take through the regular
1136// expression. A given node in the regexp can be code-generated several times
1137// as it can be part of several traces. For example for the regexp:
1138// /foo(bar|ip)baz/ the code to match baz will be generated twice, once as part
1139// of the foo-bar-baz trace and once as part of the foo-ip-baz trace. The code
1140// to match foo is generated only once (the traces have a common prefix). The
1141// code to store the capture is deferred and generated (twice) after the places
1142// where baz has been matched.
1143class Trace {
ager@chromium.org8bb60582008-12-11 12:02:20 +00001144 public:
iposva@chromium.org245aa852009-02-10 00:49:54 +00001145 // A value for a property that is either known to be true, know to be false,
1146 // or not known.
1147 enum TriBool {
1148 UNKNOWN = -1, FALSE = 0, TRUE = 1
1149 };
1150
ager@chromium.org8bb60582008-12-11 12:02:20 +00001151 class DeferredAction {
1152 public:
1153 DeferredAction(ActionNode::Type type, int reg)
1154 : type_(type), reg_(reg), next_(NULL) { }
1155 DeferredAction* next() { return next_; }
ager@chromium.org32912102009-01-16 10:38:43 +00001156 bool Mentions(int reg);
ager@chromium.org8bb60582008-12-11 12:02:20 +00001157 int reg() { return reg_; }
1158 ActionNode::Type type() { return type_; }
1159 private:
1160 ActionNode::Type type_;
1161 int reg_;
1162 DeferredAction* next_;
ager@chromium.org32912102009-01-16 10:38:43 +00001163 friend class Trace;
ager@chromium.org8bb60582008-12-11 12:02:20 +00001164 };
1165
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001166 class DeferredCapture : public DeferredAction {
ager@chromium.org8bb60582008-12-11 12:02:20 +00001167 public:
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001168 DeferredCapture(int reg, bool is_capture, Trace* trace)
ager@chromium.org8bb60582008-12-11 12:02:20 +00001169 : DeferredAction(ActionNode::STORE_POSITION, reg),
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001170 cp_offset_(trace->cp_offset()),
1171 is_capture_(is_capture) { }
ager@chromium.org8bb60582008-12-11 12:02:20 +00001172 int cp_offset() { return cp_offset_; }
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001173 bool is_capture() { return is_capture_; }
ager@chromium.org8bb60582008-12-11 12:02:20 +00001174 private:
1175 int cp_offset_;
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001176 bool is_capture_;
ager@chromium.org8bb60582008-12-11 12:02:20 +00001177 void set_cp_offset(int cp_offset) { cp_offset_ = cp_offset; }
1178 };
1179
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001180 class DeferredSetRegister : public DeferredAction {
ager@chromium.org8bb60582008-12-11 12:02:20 +00001181 public:
1182 DeferredSetRegister(int reg, int value)
1183 : DeferredAction(ActionNode::SET_REGISTER, reg),
1184 value_(value) { }
1185 int value() { return value_; }
1186 private:
1187 int value_;
1188 };
1189
ager@chromium.org32912102009-01-16 10:38:43 +00001190 class DeferredClearCaptures : public DeferredAction {
1191 public:
1192 explicit DeferredClearCaptures(Interval range)
1193 : DeferredAction(ActionNode::CLEAR_CAPTURES, -1),
1194 range_(range) { }
1195 Interval range() { return range_; }
1196 private:
1197 Interval range_;
1198 };
1199
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001200 class DeferredIncrementRegister : public DeferredAction {
ager@chromium.org8bb60582008-12-11 12:02:20 +00001201 public:
1202 explicit DeferredIncrementRegister(int reg)
1203 : DeferredAction(ActionNode::INCREMENT_REGISTER, reg) { }
1204 };
1205
ager@chromium.org32912102009-01-16 10:38:43 +00001206 Trace()
ager@chromium.org8bb60582008-12-11 12:02:20 +00001207 : cp_offset_(0),
1208 actions_(NULL),
1209 backtrack_(NULL),
1210 stop_node_(NULL),
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001211 loop_label_(NULL),
1212 characters_preloaded_(0),
iposva@chromium.org245aa852009-02-10 00:49:54 +00001213 bound_checked_up_to_(0),
ager@chromium.org381abbb2009-02-25 13:23:22 +00001214 flush_budget_(100),
iposva@chromium.org245aa852009-02-10 00:49:54 +00001215 at_start_(UNKNOWN) { }
1216
ager@chromium.org32912102009-01-16 10:38:43 +00001217 // End the trace. This involves flushing the deferred actions in the trace
1218 // and pushing a backtrack location onto the backtrack stack. Once this is
1219 // done we can start a new trace or go to one that has already been
1220 // generated.
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001221 void Flush(RegExpCompiler* compiler, RegExpNode* successor);
ager@chromium.org8bb60582008-12-11 12:02:20 +00001222 int cp_offset() { return cp_offset_; }
1223 DeferredAction* actions() { return actions_; }
ager@chromium.org32912102009-01-16 10:38:43 +00001224 // A trivial trace is one that has no deferred actions or other state that
1225 // affects the assumptions used when generating code. There is no recorded
1226 // backtrack location in a trivial trace, so with a trivial trace we will
1227 // generate code that, on a failure to match, gets the backtrack location
1228 // from the backtrack stack rather than using a direct jump instruction. We
1229 // always start code generation with a trivial trace and non-trivial traces
1230 // are created as we emit code for nodes or add to the list of deferred
1231 // actions in the trace. The location of the code generated for a node using
1232 // a trivial trace is recorded in a label in the node so that gotos can be
1233 // generated to that code.
ager@chromium.org8bb60582008-12-11 12:02:20 +00001234 bool is_trivial() {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001235 return backtrack_ == NULL &&
1236 actions_ == NULL &&
1237 cp_offset_ == 0 &&
1238 characters_preloaded_ == 0 &&
1239 bound_checked_up_to_ == 0 &&
iposva@chromium.org245aa852009-02-10 00:49:54 +00001240 quick_check_performed_.characters() == 0 &&
1241 at_start_ == UNKNOWN;
ager@chromium.org8bb60582008-12-11 12:02:20 +00001242 }
iposva@chromium.org245aa852009-02-10 00:49:54 +00001243 TriBool at_start() { return at_start_; }
1244 void set_at_start(bool at_start) { at_start_ = at_start ? TRUE : FALSE; }
ager@chromium.org8bb60582008-12-11 12:02:20 +00001245 Label* backtrack() { return backtrack_; }
1246 Label* loop_label() { return loop_label_; }
1247 RegExpNode* stop_node() { return stop_node_; }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001248 int characters_preloaded() { return characters_preloaded_; }
1249 int bound_checked_up_to() { return bound_checked_up_to_; }
ager@chromium.org381abbb2009-02-25 13:23:22 +00001250 int flush_budget() { return flush_budget_; }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001251 QuickCheckDetails* quick_check_performed() { return &quick_check_performed_; }
1252 bool mentions_reg(int reg);
ager@chromium.org32912102009-01-16 10:38:43 +00001253 // Returns true if a deferred position store exists to the specified
1254 // register and stores the offset in the out-parameter. Otherwise
1255 // returns false.
1256 bool GetStoredPosition(int reg, int* cp_offset);
1257 // These set methods and AdvanceCurrentPositionInTrace should be used only on
1258 // new traces - the intention is that traces are immutable after creation.
ager@chromium.org8bb60582008-12-11 12:02:20 +00001259 void add_action(DeferredAction* new_action) {
1260 ASSERT(new_action->next_ == NULL);
1261 new_action->next_ = actions_;
1262 actions_ = new_action;
1263 }
ager@chromium.org8bb60582008-12-11 12:02:20 +00001264 void set_backtrack(Label* backtrack) { backtrack_ = backtrack; }
1265 void set_stop_node(RegExpNode* node) { stop_node_ = node; }
1266 void set_loop_label(Label* label) { loop_label_ = label; }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001267 void set_characters_preloaded(int count) { characters_preloaded_ = count; }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001268 void set_bound_checked_up_to(int to) { bound_checked_up_to_ = to; }
ager@chromium.org381abbb2009-02-25 13:23:22 +00001269 void set_flush_budget(int to) { flush_budget_ = to; }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001270 void set_quick_check_performed(QuickCheckDetails* d) {
1271 quick_check_performed_ = *d;
1272 }
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001273 void InvalidateCurrentCharacter();
1274 void AdvanceCurrentPositionInTrace(int by, RegExpCompiler* compiler);
ager@chromium.org8bb60582008-12-11 12:02:20 +00001275 private:
1276 int FindAffectedRegisters(OutSet* affected_registers);
1277 void PerformDeferredActions(RegExpMacroAssembler* macro,
1278 int max_register,
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001279 OutSet& affected_registers,
1280 OutSet* registers_to_pop,
1281 OutSet* registers_to_clear);
ager@chromium.org8bb60582008-12-11 12:02:20 +00001282 void RestoreAffectedRegisters(RegExpMacroAssembler* macro,
1283 int max_register,
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001284 OutSet& registers_to_pop,
1285 OutSet& registers_to_clear);
ager@chromium.org8bb60582008-12-11 12:02:20 +00001286 int cp_offset_;
1287 DeferredAction* actions_;
1288 Label* backtrack_;
1289 RegExpNode* stop_node_;
1290 Label* loop_label_;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001291 int characters_preloaded_;
1292 int bound_checked_up_to_;
1293 QuickCheckDetails quick_check_performed_;
ager@chromium.org381abbb2009-02-25 13:23:22 +00001294 int flush_budget_;
iposva@chromium.org245aa852009-02-10 00:49:54 +00001295 TriBool at_start_;
ager@chromium.org8bb60582008-12-11 12:02:20 +00001296};
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001297
1298
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001299class NodeVisitor {
1300 public:
1301 virtual ~NodeVisitor() { }
1302#define DECLARE_VISIT(Type) \
1303 virtual void Visit##Type(Type##Node* that) = 0;
1304FOR_EACH_NODE_TYPE(DECLARE_VISIT)
1305#undef DECLARE_VISIT
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001306 virtual void VisitLoopChoice(LoopChoiceNode* that) { VisitChoice(that); }
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001307};
1308
1309
1310// Node visitor used to add the start set of the alternatives to the
1311// dispatch table of a choice node.
1312class DispatchTableConstructor: public NodeVisitor {
1313 public:
1314 DispatchTableConstructor(DispatchTable* table, bool ignore_case)
1315 : table_(table),
1316 choice_index_(-1),
1317 ignore_case_(ignore_case) { }
1318
1319 void BuildTable(ChoiceNode* node);
1320
1321 void AddRange(CharacterRange range) {
1322 table()->AddRange(range, choice_index_);
1323 }
1324
1325 void AddInverse(ZoneList<CharacterRange>* ranges);
1326
1327#define DECLARE_VISIT(Type) \
1328 virtual void Visit##Type(Type##Node* that);
1329FOR_EACH_NODE_TYPE(DECLARE_VISIT)
1330#undef DECLARE_VISIT
1331
1332 DispatchTable* table() { return table_; }
1333 void set_choice_index(int value) { choice_index_ = value; }
1334
1335 protected:
ager@chromium.org5ec48922009-05-05 07:25:34 +00001336 DispatchTable* table_;
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001337 int choice_index_;
1338 bool ignore_case_;
1339};
1340
1341
ager@chromium.org8bb60582008-12-11 12:02:20 +00001342// Assertion propagation moves information about assertions such as
1343// \b to the affected nodes. For instance, in /.\b./ information must
1344// be propagated to the first '.' that whatever follows needs to know
1345// if it matched a word or a non-word, and to the second '.' that it
1346// has to check if it succeeds a word or non-word. In this case the
1347// result will be something like:
1348//
1349// +-------+ +------------+
1350// | . | | . |
1351// +-------+ ---> +------------+
1352// | word? | | check word |
1353// +-------+ +------------+
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001354class Analysis: public NodeVisitor {
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001355 public:
ager@chromium.org38e4c712009-11-11 09:11:58 +00001356 Analysis(bool ignore_case, bool is_ascii)
1357 : ignore_case_(ignore_case),
1358 is_ascii_(is_ascii),
1359 error_message_(NULL) { }
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001360 void EnsureAnalyzed(RegExpNode* node);
1361
1362#define DECLARE_VISIT(Type) \
1363 virtual void Visit##Type(Type##Node* that);
1364FOR_EACH_NODE_TYPE(DECLARE_VISIT)
1365#undef DECLARE_VISIT
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001366 virtual void VisitLoopChoice(LoopChoiceNode* that);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001367
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00001368 bool has_failed() { return error_message_ != NULL; }
1369 const char* error_message() {
1370 ASSERT(error_message_ != NULL);
1371 return error_message_;
1372 }
1373 void fail(const char* error_message) {
1374 error_message_ = error_message;
1375 }
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001376 private:
1377 bool ignore_case_;
ager@chromium.org38e4c712009-11-11 09:11:58 +00001378 bool is_ascii_;
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00001379 const char* error_message_;
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001380
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001381 DISALLOW_IMPLICIT_CONSTRUCTORS(Analysis);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001382};
1383
1384
ager@chromium.org8bb60582008-12-11 12:02:20 +00001385struct RegExpCompileData {
1386 RegExpCompileData()
1387 : tree(NULL),
1388 node(NULL),
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001389 simple(true),
iposva@chromium.org245aa852009-02-10 00:49:54 +00001390 contains_anchor(false),
ager@chromium.org8bb60582008-12-11 12:02:20 +00001391 capture_count(0) { }
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001392 RegExpTree* tree;
ager@chromium.org8bb60582008-12-11 12:02:20 +00001393 RegExpNode* node;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001394 bool simple;
iposva@chromium.org245aa852009-02-10 00:49:54 +00001395 bool contains_anchor;
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001396 Handle<String> error;
1397 int capture_count;
1398};
1399
1400
1401class RegExpEngine: public AllStatic {
1402 public:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001403 struct CompilationResult {
1404 explicit CompilationResult(const char* error_message)
1405 : error_message(error_message),
1406 code(Heap::the_hole_value()),
1407 num_registers(0) {}
1408 CompilationResult(Object* code, int registers)
1409 : error_message(NULL),
1410 code(code),
1411 num_registers(registers) {}
1412 const char* error_message;
1413 Object* code;
1414 int num_registers;
1415 };
1416
1417 static CompilationResult Compile(RegExpCompileData* input,
1418 bool ignore_case,
1419 bool multiline,
1420 Handle<String> pattern,
1421 bool is_ascii);
ager@chromium.org8bb60582008-12-11 12:02:20 +00001422
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001423 static void DotPrint(const char* label, RegExpNode* node, bool ignore_case);
1424};
1425
1426
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001427class OffsetsVector {
1428 public:
1429 inline OffsetsVector(int num_registers)
1430 : offsets_vector_length_(num_registers) {
1431 if (offsets_vector_length_ > kStaticOffsetsVectorSize) {
1432 vector_ = NewArray<int>(offsets_vector_length_);
1433 } else {
1434 vector_ = static_offsets_vector_;
1435 }
1436 }
1437 inline ~OffsetsVector() {
1438 if (offsets_vector_length_ > kStaticOffsetsVectorSize) {
1439 DeleteArray(vector_);
1440 vector_ = NULL;
1441 }
1442 }
1443 inline int* vector() { return vector_; }
1444 inline int length() { return offsets_vector_length_; }
1445
1446 static const int kStaticOffsetsVectorSize = 50;
1447
1448 private:
1449 static Address static_offsets_vector_address() {
1450 return reinterpret_cast<Address>(&static_offsets_vector_);
1451 }
1452
1453 int* vector_;
1454 int offsets_vector_length_;
1455 static int static_offsets_vector_[kStaticOffsetsVectorSize];
1456
1457 friend class ExternalReference;
1458};
1459
1460
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001461} } // namespace v8::internal
1462
1463#endif // V8_JSREGEXP_H_