blob: 46d53c215e043087ae7fae8f1aac81f364ddb887 [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.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000080 static void IrregexpPrepare(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
ager@chromium.orga74f0da2008-12-03 16:05:52 +000096 // Execute an Irregexp bytecode pattern.
ager@chromium.org41826e72009-03-30 13:30:57 +000097 // On a successful match, the result is a JSArray containing
98 // captured positions. On a failure, the result is the null value.
99 // Returns an empty handle in case of an exception.
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000100 static Handle<Object> IrregexpExec(Handle<JSRegExp> regexp,
101 Handle<String> subject,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000102 int index,
103 Handle<JSArray> lastMatchInfo);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000104
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000105 // Array index in the lastMatchInfo array.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000106 static const int kLastCaptureCount = 0;
107 static const int kLastSubject = 1;
108 static const int kLastInput = 2;
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000109 static const int kFirstCapture = 3;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000110 static const int kLastMatchOverhead = 3;
111
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000112 // Direct offset into the lastMatchInfo array.
113 static const int kLastCaptureCountOffset =
114 FixedArray::kHeaderSize + kLastCaptureCount * kPointerSize;
115 static const int kLastSubjectOffset =
116 FixedArray::kHeaderSize + kLastSubject * kPointerSize;
117 static const int kLastInputOffset =
118 FixedArray::kHeaderSize + kLastInput * kPointerSize;
119 static const int kFirstCaptureOffset =
120 FixedArray::kHeaderSize + kFirstCapture * kPointerSize;
121
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000122 // Used to access the lastMatchInfo array.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000123 static int GetCapture(FixedArray* array, int index) {
124 return Smi::cast(array->get(index + kFirstCapture))->value();
125 }
126
127 static void SetLastCaptureCount(FixedArray* array, int to) {
128 array->set(kLastCaptureCount, Smi::FromInt(to));
129 }
130
131 static void SetLastSubject(FixedArray* array, String* to) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000132 array->set(kLastSubject, to);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000133 }
134
135 static void SetLastInput(FixedArray* array, String* to) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000136 array->set(kLastInput, to);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000137 }
138
139 static void SetCapture(FixedArray* array, int index, int to) {
140 array->set(index + kFirstCapture, Smi::FromInt(to));
141 }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000142
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000143 static int GetLastCaptureCount(FixedArray* array) {
144 return Smi::cast(array->get(kLastCaptureCount))->value();
145 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000146
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000147 // For acting on the JSRegExp data FixedArray.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000148 static int IrregexpMaxRegisterCount(FixedArray* re);
149 static void SetIrregexpMaxRegisterCount(FixedArray* re, int value);
150 static int IrregexpNumberOfCaptures(FixedArray* re);
151 static int IrregexpNumberOfRegisters(FixedArray* re);
152 static ByteArray* IrregexpByteCode(FixedArray* re, bool is_ascii);
153 static Code* IrregexpNativeCode(FixedArray* re, bool is_ascii);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000154
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000155 private:
156 static String* last_ascii_string_;
157 static String* two_byte_cached_string_;
158
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000159 static bool CompileIrregexp(Handle<JSRegExp> re, bool is_ascii);
160 static inline bool EnsureCompiledIrregexp(Handle<JSRegExp> re, bool is_ascii);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000161
162
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000163 // Set the subject cache. The previous string buffer is not deleted, so the
164 // caller should ensure that it doesn't leak.
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000165 static void SetSubjectCache(String* subject,
166 char* utf8_subject,
167 int uft8_length,
168 int character_position,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000169 int utf8_position);
170
171 // A one element cache of the last utf8_subject string and its length. The
172 // subject JS String object is cached in the heap. We also cache a
173 // translation between position and utf8 position.
174 static char* utf8_subject_cache_;
175 static int utf8_length_cache_;
176 static int utf8_position_;
177 static int character_position_;
178};
179
180
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000181// Represents the location of one element relative to the intersection of
182// two sets. Corresponds to the four areas of a Venn diagram.
183enum ElementInSetsRelation {
184 kInsideNone = 0,
185 kInsideFirst = 1,
186 kInsideSecond = 2,
187 kInsideBoth = 3
188};
189
190
191// Represents the relation of two sets.
192// Sets can be either disjoint, partially or fully overlapping, or equal.
193class SetRelation BASE_EMBEDDED {
194 public:
195 // Relation is represented by a bit saying whether there are elements in
196 // one set that is not in the other, and a bit saying that there are elements
197 // that are in both sets.
198
199 // Location of an element. Corresponds to the internal areas of
200 // a Venn diagram.
201 enum {
202 kInFirst = 1 << kInsideFirst,
203 kInSecond = 1 << kInsideSecond,
204 kInBoth = 1 << kInsideBoth
205 };
206 SetRelation() : bits_(0) {}
207 ~SetRelation() {}
208 // Add the existence of objects in a particular
209 void SetElementsInFirstSet() { bits_ |= kInFirst; }
210 void SetElementsInSecondSet() { bits_ |= kInSecond; }
211 void SetElementsInBothSets() { bits_ |= kInBoth; }
212 // Check the currently known relation of the sets (common functions only,
213 // for other combinations, use value() to get the bits and check them
214 // manually).
215 // Sets are completely disjoint.
216 bool Disjoint() { return (bits_ & kInBoth) == 0; }
217 // Sets are equal.
218 bool Equals() { return (bits_ & (kInFirst | kInSecond)) == 0; }
219 // First set contains second.
220 bool Contains() { return (bits_ & kInSecond) == 0; }
221 // Second set contains first.
222 bool ContainedIn() { return (bits_ & kInFirst) == 0; }
223 bool NonTrivialIntersection() {
224 return (bits_ == (kInFirst | kInSecond | kInBoth));
225 }
226 int value() { return bits_; }
227 private:
228 int bits_;
229};
230
231
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000232class CharacterRange {
233 public:
234 CharacterRange() : from_(0), to_(0) { }
235 // For compatibility with the CHECK_OK macro
236 CharacterRange(void* null) { ASSERT_EQ(NULL, null); } //NOLINT
237 CharacterRange(uc16 from, uc16 to) : from_(from), to_(to) { }
238 static void AddClassEscape(uc16 type, ZoneList<CharacterRange>* ranges);
239 static Vector<const uc16> GetWordBounds();
240 static inline CharacterRange Singleton(uc16 value) {
241 return CharacterRange(value, value);
242 }
243 static inline CharacterRange Range(uc16 from, uc16 to) {
244 ASSERT(from <= to);
245 return CharacterRange(from, to);
246 }
247 static inline CharacterRange Everything() {
248 return CharacterRange(0, 0xFFFF);
249 }
250 bool Contains(uc16 i) { return from_ <= i && i <= to_; }
251 uc16 from() const { return from_; }
252 void set_from(uc16 value) { from_ = value; }
253 uc16 to() const { return to_; }
254 void set_to(uc16 value) { to_ = value; }
255 bool is_valid() { return from_ <= to_; }
ager@chromium.org8bb60582008-12-11 12:02:20 +0000256 bool IsEverything(uc16 max) { return from_ == 0 && to_ >= max; }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000257 bool IsSingleton() { return (from_ == to_); }
ager@chromium.org38e4c712009-11-11 09:11:58 +0000258 void AddCaseEquivalents(ZoneList<CharacterRange>* ranges, bool is_ascii);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000259 static void Split(ZoneList<CharacterRange>* base,
260 Vector<const uc16> overlay,
261 ZoneList<CharacterRange>** included,
262 ZoneList<CharacterRange>** excluded);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000263 // Whether a range list is in canonical form: Ranges ordered by from value,
264 // and ranges non-overlapping and non-adjacent.
265 static bool IsCanonical(ZoneList<CharacterRange>* ranges);
266 // Convert range list to canonical form. The characters covered by the ranges
267 // will still be the same, but no character is in more than one range, and
268 // adjacent ranges are merged. The resulting list may be shorter than the
269 // original, but cannot be longer.
270 static void Canonicalize(ZoneList<CharacterRange>* ranges);
271 // Check how the set of characters defined by a CharacterRange list relates
272 // to the set of word characters. List must be in canonical form.
273 static SetRelation WordCharacterRelation(ZoneList<CharacterRange>* ranges);
274 // Takes two character range lists (representing character sets) in canonical
275 // form and merges them.
276 // The characters that are only covered by the first set are added to
277 // first_set_only_out. the characters that are only in the second set are
278 // added to second_set_only_out, and the characters that are in both are
279 // added to both_sets_out.
280 // The pointers to first_set_only_out, second_set_only_out and both_sets_out
281 // should be to empty lists, but they need not be distinct, and may be NULL.
282 // If NULL, the characters are dropped, and if two arguments are the same
283 // pointer, the result is the union of the two sets that would be created
284 // if the pointers had been distinct.
285 // This way, the Merge function can compute all the usual set operations:
286 // union (all three out-sets are equal), intersection (only both_sets_out is
287 // non-NULL), and set difference (only first_set is non-NULL).
288 static void Merge(ZoneList<CharacterRange>* first_set,
289 ZoneList<CharacterRange>* second_set,
290 ZoneList<CharacterRange>* first_set_only_out,
291 ZoneList<CharacterRange>* second_set_only_out,
292 ZoneList<CharacterRange>* both_sets_out);
293 // Negate the contents of a character range in canonical form.
294 static void Negate(ZoneList<CharacterRange>* src,
295 ZoneList<CharacterRange>* dst);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000296 static const int kRangeCanonicalizeMax = 0x346;
297 static const int kStartMarker = (1 << 24);
298 static const int kPayloadMask = (1 << 24) - 1;
299
300 private:
301 uc16 from_;
302 uc16 to_;
303};
304
305
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000306// A set of unsigned integers that behaves especially well on small
307// integers (< 32). May do zone-allocation.
308class OutSet: public ZoneObject {
309 public:
310 OutSet() : first_(0), remaining_(NULL), successors_(NULL) { }
311 OutSet* Extend(unsigned value);
312 bool Get(unsigned value);
313 static const unsigned kFirstLimit = 32;
314
315 private:
316 // Destructively set a value in this set. In most cases you want
317 // to use Extend instead to ensure that only one instance exists
318 // that contains the same values.
319 void Set(unsigned value);
320
321 // The successors are a list of sets that contain the same values
322 // as this set and the one more value that is not present in this
323 // set.
324 ZoneList<OutSet*>* successors() { return successors_; }
325
326 OutSet(uint32_t first, ZoneList<unsigned>* remaining)
327 : first_(first), remaining_(remaining), successors_(NULL) { }
328 uint32_t first_;
329 ZoneList<unsigned>* remaining_;
330 ZoneList<OutSet*>* successors_;
ager@chromium.org32912102009-01-16 10:38:43 +0000331 friend class Trace;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000332};
333
334
335// A mapping from integers, specified as ranges, to a set of integers.
336// Used for mapping character ranges to choices.
337class DispatchTable : public ZoneObject {
338 public:
339 class Entry {
340 public:
341 Entry() : from_(0), to_(0), out_set_(NULL) { }
342 Entry(uc16 from, uc16 to, OutSet* out_set)
343 : from_(from), to_(to), out_set_(out_set) { }
344 uc16 from() { return from_; }
345 uc16 to() { return to_; }
346 void set_to(uc16 value) { to_ = value; }
347 void AddValue(int value) { out_set_ = out_set_->Extend(value); }
348 OutSet* out_set() { return out_set_; }
349 private:
350 uc16 from_;
351 uc16 to_;
352 OutSet* out_set_;
353 };
354
355 class Config {
356 public:
357 typedef uc16 Key;
358 typedef Entry Value;
359 static const uc16 kNoKey;
360 static const Entry kNoValue;
361 static inline int Compare(uc16 a, uc16 b) {
362 if (a == b)
363 return 0;
364 else if (a < b)
365 return -1;
366 else
367 return 1;
368 }
369 };
370
371 void AddRange(CharacterRange range, int value);
372 OutSet* Get(uc16 value);
373 void Dump();
374
375 template <typename Callback>
376 void ForEach(Callback* callback) { return tree()->ForEach(callback); }
377 private:
378 // There can't be a static empty set since it allocates its
379 // successors in a zone and caches them.
380 OutSet* empty() { return &empty_; }
381 OutSet empty_;
382 ZoneSplayTree<Config>* tree() { return &tree_; }
383 ZoneSplayTree<Config> tree_;
384};
385
386
387#define FOR_EACH_NODE_TYPE(VISIT) \
388 VISIT(End) \
389 VISIT(Action) \
390 VISIT(Choice) \
391 VISIT(BackReference) \
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000392 VISIT(Assertion) \
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000393 VISIT(Text)
394
395
396#define FOR_EACH_REG_EXP_TREE_TYPE(VISIT) \
397 VISIT(Disjunction) \
398 VISIT(Alternative) \
399 VISIT(Assertion) \
400 VISIT(CharacterClass) \
401 VISIT(Atom) \
402 VISIT(Quantifier) \
403 VISIT(Capture) \
404 VISIT(Lookahead) \
405 VISIT(BackReference) \
406 VISIT(Empty) \
407 VISIT(Text)
408
409
410#define FORWARD_DECLARE(Name) class RegExp##Name;
411FOR_EACH_REG_EXP_TREE_TYPE(FORWARD_DECLARE)
412#undef FORWARD_DECLARE
413
414
415class TextElement {
416 public:
417 enum Type {UNINITIALIZED, ATOM, CHAR_CLASS};
418 TextElement() : type(UNINITIALIZED) { }
ager@chromium.org8bb60582008-12-11 12:02:20 +0000419 explicit TextElement(Type t) : type(t), cp_offset(-1) { }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000420 static TextElement Atom(RegExpAtom* atom);
421 static TextElement CharClass(RegExpCharacterClass* char_class);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000422 int length();
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000423 Type type;
424 union {
425 RegExpAtom* u_atom;
426 RegExpCharacterClass* u_char_class;
427 } data;
ager@chromium.org8bb60582008-12-11 12:02:20 +0000428 int cp_offset;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000429};
430
431
ager@chromium.org32912102009-01-16 10:38:43 +0000432class Trace;
ager@chromium.org8bb60582008-12-11 12:02:20 +0000433
434
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000435struct NodeInfo {
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000436 NodeInfo()
437 : being_analyzed(false),
438 been_analyzed(false),
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000439 follows_word_interest(false),
440 follows_newline_interest(false),
441 follows_start_interest(false),
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000442 at_end(false),
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000443 visited(false) { }
444
445 // Returns true if the interests and assumptions of this node
446 // matches the given one.
447 bool Matches(NodeInfo* that) {
448 return (at_end == that->at_end) &&
449 (follows_word_interest == that->follows_word_interest) &&
450 (follows_newline_interest == that->follows_newline_interest) &&
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000451 (follows_start_interest == that->follows_start_interest);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000452 }
453
454 // Updates the interests of this node given the interests of the
455 // node preceding it.
456 void AddFromPreceding(NodeInfo* that) {
457 at_end |= that->at_end;
458 follows_word_interest |= that->follows_word_interest;
459 follows_newline_interest |= that->follows_newline_interest;
460 follows_start_interest |= that->follows_start_interest;
461 }
462
ager@chromium.org8bb60582008-12-11 12:02:20 +0000463 bool HasLookbehind() {
464 return follows_word_interest ||
465 follows_newline_interest ||
466 follows_start_interest;
467 }
468
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000469 // Sets the interests of this node to include the interests of the
470 // following node.
471 void AddFromFollowing(NodeInfo* that) {
472 follows_word_interest |= that->follows_word_interest;
473 follows_newline_interest |= that->follows_newline_interest;
474 follows_start_interest |= that->follows_start_interest;
475 }
476
477 void ResetCompilationState() {
478 being_analyzed = false;
479 been_analyzed = false;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000480 }
481
482 bool being_analyzed: 1;
483 bool been_analyzed: 1;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000484
485 // These bits are set of this node has to know what the preceding
486 // character was.
487 bool follows_word_interest: 1;
488 bool follows_newline_interest: 1;
489 bool follows_start_interest: 1;
490
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000491 bool at_end: 1;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000492 bool visited: 1;
493};
494
495
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000496class SiblingList {
497 public:
498 SiblingList() : list_(NULL) { }
499 int length() {
500 return list_ == NULL ? 0 : list_->length();
501 }
502 void Ensure(RegExpNode* parent) {
503 if (list_ == NULL) {
504 list_ = new ZoneList<RegExpNode*>(2);
505 list_->Add(parent);
506 }
507 }
508 void Add(RegExpNode* node) { list_->Add(node); }
509 RegExpNode* Get(int index) { return list_->at(index); }
510 private:
511 ZoneList<RegExpNode*>* list_;
512};
513
514
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000515// Details of a quick mask-compare check that can look ahead in the
516// input stream.
517class QuickCheckDetails {
518 public:
519 QuickCheckDetails()
520 : characters_(0),
521 mask_(0),
iposva@chromium.org245aa852009-02-10 00:49:54 +0000522 value_(0),
523 cannot_match_(false) { }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000524 explicit QuickCheckDetails(int characters)
525 : characters_(characters),
526 mask_(0),
iposva@chromium.org245aa852009-02-10 00:49:54 +0000527 value_(0),
528 cannot_match_(false) { }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000529 bool Rationalize(bool ascii);
530 // Merge in the information from another branch of an alternation.
531 void Merge(QuickCheckDetails* other, int from_index);
532 // Advance the current position by some amount.
533 void Advance(int by, bool ascii);
534 void Clear();
iposva@chromium.org245aa852009-02-10 00:49:54 +0000535 bool cannot_match() { return cannot_match_; }
536 void set_cannot_match() { cannot_match_ = true; }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000537 struct Position {
538 Position() : mask(0), value(0), determines_perfectly(false) { }
539 uc16 mask;
540 uc16 value;
541 bool determines_perfectly;
542 };
543 int characters() { return characters_; }
544 void set_characters(int characters) { characters_ = characters; }
545 Position* positions(int index) {
546 ASSERT(index >= 0);
547 ASSERT(index < characters_);
548 return positions_ + index;
549 }
550 uint32_t mask() { return mask_; }
551 uint32_t value() { return value_; }
552
553 private:
554 // How many characters do we have quick check information from. This is
555 // the same for all branches of a choice node.
556 int characters_;
557 Position positions_[4];
558 // These values are the condensate of the above array after Rationalize().
559 uint32_t mask_;
560 uint32_t value_;
iposva@chromium.org245aa852009-02-10 00:49:54 +0000561 // If set to true, there is no way this quick check can match at all.
562 // E.g., if it requires to be at the start of the input, and isn't.
563 bool cannot_match_;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000564};
565
566
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000567class RegExpNode: public ZoneObject {
568 public:
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000569 RegExpNode() : first_character_set_(NULL), trace_count_(0) { }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000570 virtual ~RegExpNode();
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000571 virtual void Accept(NodeVisitor* visitor) = 0;
572 // Generates a goto to this node or actually generates the code at this point.
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000573 virtual void Emit(RegExpCompiler* compiler, Trace* trace) = 0;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000574 // How many characters must this node consume at a minimum in order to
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000575 // succeed. If we have found at least 'still_to_find' characters that
576 // must be consumed there is no need to ask any following nodes whether
577 // they are sure to eat any more characters.
578 virtual int EatsAtLeast(int still_to_find, int recursion_depth) = 0;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000579 // Emits some quick code that checks whether the preloaded characters match.
580 // Falls through on certain failure, jumps to the label on possible success.
581 // If the node cannot make a quick check it does nothing and returns false.
582 bool EmitQuickCheck(RegExpCompiler* compiler,
ager@chromium.org32912102009-01-16 10:38:43 +0000583 Trace* trace,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000584 bool preload_has_checked_bounds,
585 Label* on_possible_success,
586 QuickCheckDetails* details_return,
587 bool fall_through_on_failure);
588 // For a given number of characters this returns a mask and a value. The
589 // next n characters are anded with the mask and compared with the value.
590 // A comparison failure indicates the node cannot match the next n characters.
591 // A comparison success indicates the node may match.
592 virtual void GetQuickCheckDetails(QuickCheckDetails* details,
593 RegExpCompiler* compiler,
iposva@chromium.org245aa852009-02-10 00:49:54 +0000594 int characters_filled_in,
595 bool not_at_start) = 0;
ager@chromium.org8bb60582008-12-11 12:02:20 +0000596 static const int kNodeIsTooComplexForGreedyLoops = -1;
597 virtual int GreedyLoopTextLength() { return kNodeIsTooComplexForGreedyLoops; }
598 Label* label() { return &label_; }
ager@chromium.org32912102009-01-16 10:38:43 +0000599 // If non-generic code is generated for a node (ie the node is not at the
600 // start of the trace) then it cannot be reused. This variable sets a limit
601 // on how often we allow that to happen before we insist on starting a new
602 // trace and generating generic code for a node that can be reused by flushing
603 // the deferred actions in the current trace and generating a goto.
604 static const int kMaxCopiesCodeGenerated = 10;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000605
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000606 NodeInfo* info() { return &info_; }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000607
608 void AddSibling(RegExpNode* node) { siblings_.Add(node); }
609
610 // Static version of EnsureSibling that expresses the fact that the
611 // result has the same type as the input.
612 template <class C>
613 static C* EnsureSibling(C* node, NodeInfo* info, bool* cloned) {
614 return static_cast<C*>(node->EnsureSibling(info, cloned));
615 }
616
617 SiblingList* siblings() { return &siblings_; }
618 void set_siblings(SiblingList* other) { siblings_ = *other; }
619
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000620 // Return the set of possible next characters recognized by the regexp
621 // (or a safe subset, potentially the set of all characters).
622 ZoneList<CharacterRange>* FirstCharacterSet();
623
624 // Compute (if possible within the budget of traversed nodes) the
625 // possible first characters of the input matched by this node and
626 // its continuation. Returns the remaining budget after the computation.
627 // If the budget is spent, the result is negative, and the cached
628 // first_character_set_ value isn't set.
629 virtual int ComputeFirstCharacterSet(int budget);
630
631 // Get and set the cached first character set value.
632 ZoneList<CharacterRange>* first_character_set() {
633 return first_character_set_;
634 }
635 void set_first_character_set(ZoneList<CharacterRange>* character_set) {
636 first_character_set_ = character_set;
637 }
638
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000639 protected:
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000640 enum LimitResult { DONE, CONTINUE };
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000641 static const int kComputeFirstCharacterSetFail = -1;
642
ager@chromium.org32912102009-01-16 10:38:43 +0000643 LimitResult LimitVersions(RegExpCompiler* compiler, Trace* trace);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000644
645 // Returns a sibling of this node whose interests and assumptions
646 // match the ones in the given node info. If no sibling exists NULL
647 // is returned.
648 RegExpNode* TryGetSibling(NodeInfo* info);
649
650 // Returns a sibling of this node whose interests match the ones in
651 // the given node info. The info must not contain any assertions.
652 // If no node exists a new one will be created by cloning the current
653 // node. The result will always be an instance of the same concrete
654 // class as this node.
655 RegExpNode* EnsureSibling(NodeInfo* info, bool* cloned);
656
657 // Returns a clone of this node initialized using the copy constructor
658 // of its concrete class. Note that the node may have to be pre-
ager@chromium.org32912102009-01-16 10:38:43 +0000659 // processed before it is on a usable state.
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000660 virtual RegExpNode* Clone() = 0;
661
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000662 private:
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000663 static const int kFirstCharBudget = 10;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000664 Label label_;
665 NodeInfo info_;
666 SiblingList siblings_;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000667 ZoneList<CharacterRange>* first_character_set_;
ager@chromium.org32912102009-01-16 10:38:43 +0000668 // This variable keeps track of how many times code has been generated for
669 // this node (in different traces). We don't keep track of where the
670 // generated code is located unless the code is generated at the start of
671 // a trace, in which case it is generic and can be reused by flushing the
672 // deferred operations in the current trace and generating a goto.
673 int trace_count_;
674};
675
676
677// A simple closed interval.
678class Interval {
679 public:
680 Interval() : from_(kNone), to_(kNone) { }
681 Interval(int from, int to) : from_(from), to_(to) { }
682 Interval Union(Interval that) {
683 if (that.from_ == kNone)
684 return *this;
685 else if (from_ == kNone)
686 return that;
687 else
688 return Interval(Min(from_, that.from_), Max(to_, that.to_));
689 }
690 bool Contains(int value) {
691 return (from_ <= value) && (value <= to_);
692 }
693 bool is_empty() { return from_ == kNone; }
694 int from() { return from_; }
695 int to() { return to_; }
696 static Interval Empty() { return Interval(); }
697 static const int kNone = -1;
698 private:
699 int from_;
700 int to_;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000701};
702
703
704class SeqRegExpNode: public RegExpNode {
705 public:
706 explicit SeqRegExpNode(RegExpNode* on_success)
707 : on_success_(on_success) { }
708 RegExpNode* on_success() { return on_success_; }
709 void set_on_success(RegExpNode* node) { on_success_ = node; }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000710 private:
711 RegExpNode* on_success_;
712};
713
714
715class ActionNode: public SeqRegExpNode {
716 public:
717 enum Type {
ager@chromium.org8bb60582008-12-11 12:02:20 +0000718 SET_REGISTER,
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000719 INCREMENT_REGISTER,
720 STORE_POSITION,
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000721 BEGIN_SUBMATCH,
ager@chromium.org32912102009-01-16 10:38:43 +0000722 POSITIVE_SUBMATCH_SUCCESS,
723 EMPTY_MATCH_CHECK,
724 CLEAR_CAPTURES
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000725 };
ager@chromium.org8bb60582008-12-11 12:02:20 +0000726 static ActionNode* SetRegister(int reg, int val, RegExpNode* on_success);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000727 static ActionNode* IncrementRegister(int reg, RegExpNode* on_success);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000728 static ActionNode* StorePosition(int reg,
729 bool is_capture,
730 RegExpNode* on_success);
ager@chromium.org32912102009-01-16 10:38:43 +0000731 static ActionNode* ClearCaptures(Interval range, RegExpNode* on_success);
732 static ActionNode* BeginSubmatch(int stack_pointer_reg,
733 int position_reg,
734 RegExpNode* on_success);
735 static ActionNode* PositiveSubmatchSuccess(int stack_pointer_reg,
736 int restore_reg,
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000737 int clear_capture_count,
738 int clear_capture_from,
ager@chromium.org32912102009-01-16 10:38:43 +0000739 RegExpNode* on_success);
740 static ActionNode* EmptyMatchCheck(int start_register,
741 int repetition_register,
742 int repetition_limit,
743 RegExpNode* on_success);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000744 virtual void Accept(NodeVisitor* visitor);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000745 virtual void Emit(RegExpCompiler* compiler, Trace* trace);
746 virtual int EatsAtLeast(int still_to_find, int recursion_depth);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000747 virtual void GetQuickCheckDetails(QuickCheckDetails* details,
748 RegExpCompiler* compiler,
iposva@chromium.org245aa852009-02-10 00:49:54 +0000749 int filled_in,
750 bool not_at_start) {
751 return on_success()->GetQuickCheckDetails(
752 details, compiler, filled_in, not_at_start);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000753 }
ager@chromium.org8bb60582008-12-11 12:02:20 +0000754 Type type() { return type_; }
755 // TODO(erikcorry): We should allow some action nodes in greedy loops.
756 virtual int GreedyLoopTextLength() { return kNodeIsTooComplexForGreedyLoops; }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000757 virtual ActionNode* Clone() { return new ActionNode(*this); }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000758 virtual int ComputeFirstCharacterSet(int budget);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000759 private:
760 union {
761 struct {
762 int reg;
763 int value;
764 } u_store_register;
765 struct {
766 int reg;
767 } u_increment_register;
768 struct {
769 int reg;
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000770 bool is_capture;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000771 } u_position_register;
772 struct {
773 int stack_pointer_register;
774 int current_position_register;
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000775 int clear_register_count;
776 int clear_register_from;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000777 } u_submatch;
ager@chromium.org32912102009-01-16 10:38:43 +0000778 struct {
779 int start_register;
780 int repetition_register;
781 int repetition_limit;
782 } u_empty_match_check;
783 struct {
784 int range_from;
785 int range_to;
786 } u_clear_captures;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000787 } data_;
788 ActionNode(Type type, RegExpNode* on_success)
789 : SeqRegExpNode(on_success),
790 type_(type) { }
791 Type type_;
792 friend class DotPrinter;
793};
794
795
796class TextNode: public SeqRegExpNode {
797 public:
798 TextNode(ZoneList<TextElement>* elms,
ager@chromium.org8bb60582008-12-11 12:02:20 +0000799 RegExpNode* on_success)
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000800 : SeqRegExpNode(on_success),
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000801 elms_(elms) { }
802 TextNode(RegExpCharacterClass* that,
ager@chromium.org8bb60582008-12-11 12:02:20 +0000803 RegExpNode* on_success)
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000804 : SeqRegExpNode(on_success),
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000805 elms_(new ZoneList<TextElement>(1)) {
806 elms_->Add(TextElement::CharClass(that));
807 }
808 virtual void Accept(NodeVisitor* visitor);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000809 virtual void Emit(RegExpCompiler* compiler, Trace* trace);
810 virtual int EatsAtLeast(int still_to_find, int recursion_depth);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000811 virtual void GetQuickCheckDetails(QuickCheckDetails* details,
812 RegExpCompiler* compiler,
iposva@chromium.org245aa852009-02-10 00:49:54 +0000813 int characters_filled_in,
814 bool not_at_start);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000815 ZoneList<TextElement>* elements() { return elms_; }
ager@chromium.org38e4c712009-11-11 09:11:58 +0000816 void MakeCaseIndependent(bool is_ascii);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000817 virtual int GreedyLoopTextLength();
818 virtual TextNode* Clone() {
819 TextNode* result = new TextNode(*this);
820 result->CalculateOffsets();
821 return result;
822 }
823 void CalculateOffsets();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000824 virtual int ComputeFirstCharacterSet(int budget);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000825 private:
826 enum TextEmitPassType {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000827 NON_ASCII_MATCH, // Check for characters that can't match.
828 SIMPLE_CHARACTER_MATCH, // Case-dependent single character check.
829 NON_LETTER_CHARACTER_MATCH, // Check characters that have no case equivs.
830 CASE_CHARACTER_MATCH, // Case-independent single character check.
831 CHARACTER_CLASS_MATCH // Character class.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000832 };
ager@chromium.org381abbb2009-02-25 13:23:22 +0000833 static bool SkipPass(int pass, bool ignore_case);
834 static const int kFirstRealPass = SIMPLE_CHARACTER_MATCH;
835 static const int kLastPass = CHARACTER_CLASS_MATCH;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000836 void TextEmitPass(RegExpCompiler* compiler,
837 TextEmitPassType pass,
838 bool preloaded,
ager@chromium.org32912102009-01-16 10:38:43 +0000839 Trace* trace,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000840 bool first_element_checked,
841 int* checked_up_to);
842 int Length();
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000843 ZoneList<TextElement>* elms_;
844};
845
846
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000847class AssertionNode: public SeqRegExpNode {
848 public:
849 enum AssertionNodeType {
850 AT_END,
851 AT_START,
852 AT_BOUNDARY,
853 AT_NON_BOUNDARY,
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000854 AFTER_NEWLINE,
855 // Types not directly expressible in regexp syntax.
856 // Used for modifying a boundary node if its following character is
857 // known to be word and/or non-word.
858 AFTER_NONWORD_CHARACTER,
859 AFTER_WORD_CHARACTER
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000860 };
861 static AssertionNode* AtEnd(RegExpNode* on_success) {
862 return new AssertionNode(AT_END, on_success);
863 }
864 static AssertionNode* AtStart(RegExpNode* on_success) {
865 return new AssertionNode(AT_START, on_success);
866 }
867 static AssertionNode* AtBoundary(RegExpNode* on_success) {
868 return new AssertionNode(AT_BOUNDARY, on_success);
869 }
870 static AssertionNode* AtNonBoundary(RegExpNode* on_success) {
871 return new AssertionNode(AT_NON_BOUNDARY, on_success);
872 }
873 static AssertionNode* AfterNewline(RegExpNode* on_success) {
874 return new AssertionNode(AFTER_NEWLINE, on_success);
875 }
876 virtual void Accept(NodeVisitor* visitor);
877 virtual void Emit(RegExpCompiler* compiler, Trace* trace);
878 virtual int EatsAtLeast(int still_to_find, int recursion_depth);
879 virtual void GetQuickCheckDetails(QuickCheckDetails* details,
880 RegExpCompiler* compiler,
iposva@chromium.org245aa852009-02-10 00:49:54 +0000881 int filled_in,
882 bool not_at_start);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000883 virtual int ComputeFirstCharacterSet(int budget);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000884 virtual AssertionNode* Clone() { return new AssertionNode(*this); }
885 AssertionNodeType type() { return type_; }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000886 void set_type(AssertionNodeType type) { type_ = type; }
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000887 private:
888 AssertionNode(AssertionNodeType t, RegExpNode* on_success)
889 : SeqRegExpNode(on_success), type_(t) { }
890 AssertionNodeType type_;
891};
892
893
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000894class BackReferenceNode: public SeqRegExpNode {
895 public:
896 BackReferenceNode(int start_reg,
897 int end_reg,
ager@chromium.org8bb60582008-12-11 12:02:20 +0000898 RegExpNode* on_success)
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000899 : SeqRegExpNode(on_success),
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000900 start_reg_(start_reg),
901 end_reg_(end_reg) { }
902 virtual void Accept(NodeVisitor* visitor);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000903 int start_register() { return start_reg_; }
904 int end_register() { return end_reg_; }
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000905 virtual void Emit(RegExpCompiler* compiler, Trace* trace);
906 virtual int EatsAtLeast(int still_to_find, int recursion_depth);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000907 virtual void GetQuickCheckDetails(QuickCheckDetails* details,
908 RegExpCompiler* compiler,
iposva@chromium.org245aa852009-02-10 00:49:54 +0000909 int characters_filled_in,
910 bool not_at_start) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000911 return;
912 }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000913 virtual BackReferenceNode* Clone() { return new BackReferenceNode(*this); }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000914 virtual int ComputeFirstCharacterSet(int budget);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000915 private:
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000916 int start_reg_;
917 int end_reg_;
918};
919
920
921class EndNode: public RegExpNode {
922 public:
ager@chromium.org8bb60582008-12-11 12:02:20 +0000923 enum Action { ACCEPT, BACKTRACK, NEGATIVE_SUBMATCH_SUCCESS };
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000924 explicit EndNode(Action action) : action_(action) { }
925 virtual void Accept(NodeVisitor* visitor);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000926 virtual void Emit(RegExpCompiler* compiler, Trace* trace);
927 virtual int EatsAtLeast(int still_to_find, int recursion_depth) { return 0; }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000928 virtual void GetQuickCheckDetails(QuickCheckDetails* details,
929 RegExpCompiler* compiler,
iposva@chromium.org245aa852009-02-10 00:49:54 +0000930 int characters_filled_in,
931 bool not_at_start) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000932 // Returning 0 from EatsAtLeast should ensure we never get here.
933 UNREACHABLE();
934 }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000935 virtual EndNode* Clone() { return new EndNode(*this); }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000936 private:
937 Action action_;
938};
939
940
ager@chromium.org8bb60582008-12-11 12:02:20 +0000941class NegativeSubmatchSuccess: public EndNode {
942 public:
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000943 NegativeSubmatchSuccess(int stack_pointer_reg,
944 int position_reg,
945 int clear_capture_count,
946 int clear_capture_start)
ager@chromium.org8bb60582008-12-11 12:02:20 +0000947 : EndNode(NEGATIVE_SUBMATCH_SUCCESS),
948 stack_pointer_register_(stack_pointer_reg),
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000949 current_position_register_(position_reg),
950 clear_capture_count_(clear_capture_count),
951 clear_capture_start_(clear_capture_start) { }
952 virtual void Emit(RegExpCompiler* compiler, Trace* trace);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000953
954 private:
955 int stack_pointer_register_;
956 int current_position_register_;
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000957 int clear_capture_count_;
958 int clear_capture_start_;
ager@chromium.org8bb60582008-12-11 12:02:20 +0000959};
960
961
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000962class Guard: public ZoneObject {
963 public:
964 enum Relation { LT, GEQ };
965 Guard(int reg, Relation op, int value)
966 : reg_(reg),
967 op_(op),
968 value_(value) { }
969 int reg() { return reg_; }
970 Relation op() { return op_; }
971 int value() { return value_; }
972
973 private:
974 int reg_;
975 Relation op_;
976 int value_;
977};
978
979
980class GuardedAlternative {
981 public:
982 explicit GuardedAlternative(RegExpNode* node) : node_(node), guards_(NULL) { }
983 void AddGuard(Guard* guard);
984 RegExpNode* node() { return node_; }
985 void set_node(RegExpNode* node) { node_ = node; }
986 ZoneList<Guard*>* guards() { return guards_; }
987
988 private:
989 RegExpNode* node_;
990 ZoneList<Guard*>* guards_;
991};
992
993
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000994class AlternativeGeneration;
995
996
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000997class ChoiceNode: public RegExpNode {
998 public:
ager@chromium.org8bb60582008-12-11 12:02:20 +0000999 explicit ChoiceNode(int expected_size)
1000 : alternatives_(new ZoneList<GuardedAlternative>(expected_size)),
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001001 table_(NULL),
iposva@chromium.org245aa852009-02-10 00:49:54 +00001002 not_at_start_(false),
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001003 being_calculated_(false) { }
1004 virtual void Accept(NodeVisitor* visitor);
1005 void AddAlternative(GuardedAlternative node) { alternatives()->Add(node); }
1006 ZoneList<GuardedAlternative>* alternatives() { return alternatives_; }
1007 DispatchTable* GetTable(bool ignore_case);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001008 virtual void Emit(RegExpCompiler* compiler, Trace* trace);
1009 virtual int EatsAtLeast(int still_to_find, int recursion_depth);
1010 int EatsAtLeastHelper(int still_to_find,
1011 int recursion_depth,
1012 RegExpNode* ignore_this_node);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001013 virtual void GetQuickCheckDetails(QuickCheckDetails* details,
1014 RegExpCompiler* compiler,
iposva@chromium.org245aa852009-02-10 00:49:54 +00001015 int characters_filled_in,
1016 bool not_at_start);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001017 virtual ChoiceNode* Clone() { return new ChoiceNode(*this); }
1018
1019 bool being_calculated() { return being_calculated_; }
iposva@chromium.org245aa852009-02-10 00:49:54 +00001020 bool not_at_start() { return not_at_start_; }
1021 void set_not_at_start() { not_at_start_ = true; }
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001022 void set_being_calculated(bool b) { being_calculated_ = b; }
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001023 virtual bool try_to_emit_quick_check_for_alternative(int i) { return true; }
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001024
ager@chromium.org8bb60582008-12-11 12:02:20 +00001025 protected:
ager@chromium.org5ec48922009-05-05 07:25:34 +00001026 int GreedyLoopTextLength(GuardedAlternative* alternative);
ager@chromium.org8bb60582008-12-11 12:02:20 +00001027 ZoneList<GuardedAlternative>* alternatives_;
1028
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001029 private:
1030 friend class DispatchTableConstructor;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001031 friend class Analysis;
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001032 void GenerateGuard(RegExpMacroAssembler* macro_assembler,
ager@chromium.org5ec48922009-05-05 07:25:34 +00001033 Guard* guard,
ager@chromium.org32912102009-01-16 10:38:43 +00001034 Trace* trace);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001035 int CalculatePreloadCharacters(RegExpCompiler* compiler);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001036 void EmitOutOfLineContinuation(RegExpCompiler* compiler,
ager@chromium.org32912102009-01-16 10:38:43 +00001037 Trace* trace,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001038 GuardedAlternative alternative,
1039 AlternativeGeneration* alt_gen,
1040 int preload_characters,
1041 bool next_expects_preload);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001042 DispatchTable* table_;
iposva@chromium.org245aa852009-02-10 00:49:54 +00001043 // If true, this node is never checked at the start of the input.
1044 // Allows a new trace to start with at_start() set to false.
1045 bool not_at_start_;
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001046 bool being_calculated_;
1047};
1048
1049
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001050class NegativeLookaheadChoiceNode: public ChoiceNode {
1051 public:
1052 explicit NegativeLookaheadChoiceNode(GuardedAlternative this_must_fail,
1053 GuardedAlternative then_do_this)
1054 : ChoiceNode(2) {
1055 AddAlternative(this_must_fail);
1056 AddAlternative(then_do_this);
1057 }
1058 virtual int EatsAtLeast(int still_to_find, int recursion_depth);
1059 virtual void GetQuickCheckDetails(QuickCheckDetails* details,
1060 RegExpCompiler* compiler,
iposva@chromium.org245aa852009-02-10 00:49:54 +00001061 int characters_filled_in,
1062 bool not_at_start);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001063 // For a negative lookahead we don't emit the quick check for the
1064 // alternative that is expected to fail. This is because quick check code
1065 // starts by loading enough characters for the alternative that takes fewest
1066 // characters, but on a negative lookahead the negative branch did not take
1067 // part in that calculation (EatsAtLeast) so the assumptions don't hold.
1068 virtual bool try_to_emit_quick_check_for_alternative(int i) { return i != 0; }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001069 virtual int ComputeFirstCharacterSet(int budget);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001070};
1071
1072
ager@chromium.org8bb60582008-12-11 12:02:20 +00001073class LoopChoiceNode: public ChoiceNode {
1074 public:
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001075 explicit LoopChoiceNode(bool body_can_be_zero_length)
1076 : ChoiceNode(2),
1077 loop_node_(NULL),
1078 continue_node_(NULL),
1079 body_can_be_zero_length_(body_can_be_zero_length) { }
1080 void AddLoopAlternative(GuardedAlternative alt);
1081 void AddContinueAlternative(GuardedAlternative alt);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001082 virtual void Emit(RegExpCompiler* compiler, Trace* trace);
1083 virtual int EatsAtLeast(int still_to_find, int recursion_depth);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001084 virtual void GetQuickCheckDetails(QuickCheckDetails* details,
1085 RegExpCompiler* compiler,
iposva@chromium.org245aa852009-02-10 00:49:54 +00001086 int characters_filled_in,
1087 bool not_at_start);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001088 virtual int ComputeFirstCharacterSet(int budget);
ager@chromium.org8bb60582008-12-11 12:02:20 +00001089 virtual LoopChoiceNode* Clone() { return new LoopChoiceNode(*this); }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001090 RegExpNode* loop_node() { return loop_node_; }
1091 RegExpNode* continue_node() { return continue_node_; }
1092 bool body_can_be_zero_length() { return body_can_be_zero_length_; }
1093 virtual void Accept(NodeVisitor* visitor);
1094
1095 private:
1096 // AddAlternative is made private for loop nodes because alternatives
1097 // should not be added freely, we need to keep track of which node
1098 // goes back to the node itself.
1099 void AddAlternative(GuardedAlternative node) {
1100 ChoiceNode::AddAlternative(node);
1101 }
1102
1103 RegExpNode* loop_node_;
1104 RegExpNode* continue_node_;
1105 bool body_can_be_zero_length_;
ager@chromium.org8bb60582008-12-11 12:02:20 +00001106};
1107
1108
1109// There are many ways to generate code for a node. This class encapsulates
1110// the current way we should be generating. In other words it encapsulates
ager@chromium.org32912102009-01-16 10:38:43 +00001111// the current state of the code generator. The effect of this is that we
1112// generate code for paths that the matcher can take through the regular
1113// expression. A given node in the regexp can be code-generated several times
1114// as it can be part of several traces. For example for the regexp:
1115// /foo(bar|ip)baz/ the code to match baz will be generated twice, once as part
1116// of the foo-bar-baz trace and once as part of the foo-ip-baz trace. The code
1117// to match foo is generated only once (the traces have a common prefix). The
1118// code to store the capture is deferred and generated (twice) after the places
1119// where baz has been matched.
1120class Trace {
ager@chromium.org8bb60582008-12-11 12:02:20 +00001121 public:
iposva@chromium.org245aa852009-02-10 00:49:54 +00001122 // A value for a property that is either known to be true, know to be false,
1123 // or not known.
1124 enum TriBool {
1125 UNKNOWN = -1, FALSE = 0, TRUE = 1
1126 };
1127
ager@chromium.org8bb60582008-12-11 12:02:20 +00001128 class DeferredAction {
1129 public:
1130 DeferredAction(ActionNode::Type type, int reg)
1131 : type_(type), reg_(reg), next_(NULL) { }
1132 DeferredAction* next() { return next_; }
ager@chromium.org32912102009-01-16 10:38:43 +00001133 bool Mentions(int reg);
ager@chromium.org8bb60582008-12-11 12:02:20 +00001134 int reg() { return reg_; }
1135 ActionNode::Type type() { return type_; }
1136 private:
1137 ActionNode::Type type_;
1138 int reg_;
1139 DeferredAction* next_;
ager@chromium.org32912102009-01-16 10:38:43 +00001140 friend class Trace;
ager@chromium.org8bb60582008-12-11 12:02:20 +00001141 };
1142
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001143 class DeferredCapture : public DeferredAction {
ager@chromium.org8bb60582008-12-11 12:02:20 +00001144 public:
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001145 DeferredCapture(int reg, bool is_capture, Trace* trace)
ager@chromium.org8bb60582008-12-11 12:02:20 +00001146 : DeferredAction(ActionNode::STORE_POSITION, reg),
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001147 cp_offset_(trace->cp_offset()),
1148 is_capture_(is_capture) { }
ager@chromium.org8bb60582008-12-11 12:02:20 +00001149 int cp_offset() { return cp_offset_; }
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001150 bool is_capture() { return is_capture_; }
ager@chromium.org8bb60582008-12-11 12:02:20 +00001151 private:
1152 int cp_offset_;
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001153 bool is_capture_;
ager@chromium.org8bb60582008-12-11 12:02:20 +00001154 void set_cp_offset(int cp_offset) { cp_offset_ = cp_offset; }
1155 };
1156
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001157 class DeferredSetRegister : public DeferredAction {
ager@chromium.org8bb60582008-12-11 12:02:20 +00001158 public:
1159 DeferredSetRegister(int reg, int value)
1160 : DeferredAction(ActionNode::SET_REGISTER, reg),
1161 value_(value) { }
1162 int value() { return value_; }
1163 private:
1164 int value_;
1165 };
1166
ager@chromium.org32912102009-01-16 10:38:43 +00001167 class DeferredClearCaptures : public DeferredAction {
1168 public:
1169 explicit DeferredClearCaptures(Interval range)
1170 : DeferredAction(ActionNode::CLEAR_CAPTURES, -1),
1171 range_(range) { }
1172 Interval range() { return range_; }
1173 private:
1174 Interval range_;
1175 };
1176
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001177 class DeferredIncrementRegister : public DeferredAction {
ager@chromium.org8bb60582008-12-11 12:02:20 +00001178 public:
1179 explicit DeferredIncrementRegister(int reg)
1180 : DeferredAction(ActionNode::INCREMENT_REGISTER, reg) { }
1181 };
1182
ager@chromium.org32912102009-01-16 10:38:43 +00001183 Trace()
ager@chromium.org8bb60582008-12-11 12:02:20 +00001184 : cp_offset_(0),
1185 actions_(NULL),
1186 backtrack_(NULL),
1187 stop_node_(NULL),
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001188 loop_label_(NULL),
1189 characters_preloaded_(0),
iposva@chromium.org245aa852009-02-10 00:49:54 +00001190 bound_checked_up_to_(0),
ager@chromium.org381abbb2009-02-25 13:23:22 +00001191 flush_budget_(100),
iposva@chromium.org245aa852009-02-10 00:49:54 +00001192 at_start_(UNKNOWN) { }
1193
ager@chromium.org32912102009-01-16 10:38:43 +00001194 // End the trace. This involves flushing the deferred actions in the trace
1195 // and pushing a backtrack location onto the backtrack stack. Once this is
1196 // done we can start a new trace or go to one that has already been
1197 // generated.
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001198 void Flush(RegExpCompiler* compiler, RegExpNode* successor);
ager@chromium.org8bb60582008-12-11 12:02:20 +00001199 int cp_offset() { return cp_offset_; }
1200 DeferredAction* actions() { return actions_; }
ager@chromium.org32912102009-01-16 10:38:43 +00001201 // A trivial trace is one that has no deferred actions or other state that
1202 // affects the assumptions used when generating code. There is no recorded
1203 // backtrack location in a trivial trace, so with a trivial trace we will
1204 // generate code that, on a failure to match, gets the backtrack location
1205 // from the backtrack stack rather than using a direct jump instruction. We
1206 // always start code generation with a trivial trace and non-trivial traces
1207 // are created as we emit code for nodes or add to the list of deferred
1208 // actions in the trace. The location of the code generated for a node using
1209 // a trivial trace is recorded in a label in the node so that gotos can be
1210 // generated to that code.
ager@chromium.org8bb60582008-12-11 12:02:20 +00001211 bool is_trivial() {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001212 return backtrack_ == NULL &&
1213 actions_ == NULL &&
1214 cp_offset_ == 0 &&
1215 characters_preloaded_ == 0 &&
1216 bound_checked_up_to_ == 0 &&
iposva@chromium.org245aa852009-02-10 00:49:54 +00001217 quick_check_performed_.characters() == 0 &&
1218 at_start_ == UNKNOWN;
ager@chromium.org8bb60582008-12-11 12:02:20 +00001219 }
iposva@chromium.org245aa852009-02-10 00:49:54 +00001220 TriBool at_start() { return at_start_; }
1221 void set_at_start(bool at_start) { at_start_ = at_start ? TRUE : FALSE; }
ager@chromium.org8bb60582008-12-11 12:02:20 +00001222 Label* backtrack() { return backtrack_; }
1223 Label* loop_label() { return loop_label_; }
1224 RegExpNode* stop_node() { return stop_node_; }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001225 int characters_preloaded() { return characters_preloaded_; }
1226 int bound_checked_up_to() { return bound_checked_up_to_; }
ager@chromium.org381abbb2009-02-25 13:23:22 +00001227 int flush_budget() { return flush_budget_; }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001228 QuickCheckDetails* quick_check_performed() { return &quick_check_performed_; }
1229 bool mentions_reg(int reg);
ager@chromium.org32912102009-01-16 10:38:43 +00001230 // Returns true if a deferred position store exists to the specified
1231 // register and stores the offset in the out-parameter. Otherwise
1232 // returns false.
1233 bool GetStoredPosition(int reg, int* cp_offset);
1234 // These set methods and AdvanceCurrentPositionInTrace should be used only on
1235 // new traces - the intention is that traces are immutable after creation.
ager@chromium.org8bb60582008-12-11 12:02:20 +00001236 void add_action(DeferredAction* new_action) {
1237 ASSERT(new_action->next_ == NULL);
1238 new_action->next_ = actions_;
1239 actions_ = new_action;
1240 }
ager@chromium.org8bb60582008-12-11 12:02:20 +00001241 void set_backtrack(Label* backtrack) { backtrack_ = backtrack; }
1242 void set_stop_node(RegExpNode* node) { stop_node_ = node; }
1243 void set_loop_label(Label* label) { loop_label_ = label; }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001244 void set_characters_preloaded(int count) { characters_preloaded_ = count; }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001245 void set_bound_checked_up_to(int to) { bound_checked_up_to_ = to; }
ager@chromium.org381abbb2009-02-25 13:23:22 +00001246 void set_flush_budget(int to) { flush_budget_ = to; }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001247 void set_quick_check_performed(QuickCheckDetails* d) {
1248 quick_check_performed_ = *d;
1249 }
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001250 void InvalidateCurrentCharacter();
1251 void AdvanceCurrentPositionInTrace(int by, RegExpCompiler* compiler);
ager@chromium.org8bb60582008-12-11 12:02:20 +00001252 private:
1253 int FindAffectedRegisters(OutSet* affected_registers);
1254 void PerformDeferredActions(RegExpMacroAssembler* macro,
1255 int max_register,
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001256 OutSet& affected_registers,
1257 OutSet* registers_to_pop,
1258 OutSet* registers_to_clear);
ager@chromium.org8bb60582008-12-11 12:02:20 +00001259 void RestoreAffectedRegisters(RegExpMacroAssembler* macro,
1260 int max_register,
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001261 OutSet& registers_to_pop,
1262 OutSet& registers_to_clear);
ager@chromium.org8bb60582008-12-11 12:02:20 +00001263 int cp_offset_;
1264 DeferredAction* actions_;
1265 Label* backtrack_;
1266 RegExpNode* stop_node_;
1267 Label* loop_label_;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001268 int characters_preloaded_;
1269 int bound_checked_up_to_;
1270 QuickCheckDetails quick_check_performed_;
ager@chromium.org381abbb2009-02-25 13:23:22 +00001271 int flush_budget_;
iposva@chromium.org245aa852009-02-10 00:49:54 +00001272 TriBool at_start_;
ager@chromium.org8bb60582008-12-11 12:02:20 +00001273};
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001274
1275
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001276class NodeVisitor {
1277 public:
1278 virtual ~NodeVisitor() { }
1279#define DECLARE_VISIT(Type) \
1280 virtual void Visit##Type(Type##Node* that) = 0;
1281FOR_EACH_NODE_TYPE(DECLARE_VISIT)
1282#undef DECLARE_VISIT
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001283 virtual void VisitLoopChoice(LoopChoiceNode* that) { VisitChoice(that); }
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001284};
1285
1286
1287// Node visitor used to add the start set of the alternatives to the
1288// dispatch table of a choice node.
1289class DispatchTableConstructor: public NodeVisitor {
1290 public:
1291 DispatchTableConstructor(DispatchTable* table, bool ignore_case)
1292 : table_(table),
1293 choice_index_(-1),
1294 ignore_case_(ignore_case) { }
1295
1296 void BuildTable(ChoiceNode* node);
1297
1298 void AddRange(CharacterRange range) {
1299 table()->AddRange(range, choice_index_);
1300 }
1301
1302 void AddInverse(ZoneList<CharacterRange>* ranges);
1303
1304#define DECLARE_VISIT(Type) \
1305 virtual void Visit##Type(Type##Node* that);
1306FOR_EACH_NODE_TYPE(DECLARE_VISIT)
1307#undef DECLARE_VISIT
1308
1309 DispatchTable* table() { return table_; }
1310 void set_choice_index(int value) { choice_index_ = value; }
1311
1312 protected:
ager@chromium.org5ec48922009-05-05 07:25:34 +00001313 DispatchTable* table_;
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001314 int choice_index_;
1315 bool ignore_case_;
1316};
1317
1318
ager@chromium.org8bb60582008-12-11 12:02:20 +00001319// Assertion propagation moves information about assertions such as
1320// \b to the affected nodes. For instance, in /.\b./ information must
1321// be propagated to the first '.' that whatever follows needs to know
1322// if it matched a word or a non-word, and to the second '.' that it
1323// has to check if it succeeds a word or non-word. In this case the
1324// result will be something like:
1325//
1326// +-------+ +------------+
1327// | . | | . |
1328// +-------+ ---> +------------+
1329// | word? | | check word |
1330// +-------+ +------------+
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001331class Analysis: public NodeVisitor {
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001332 public:
ager@chromium.org38e4c712009-11-11 09:11:58 +00001333 Analysis(bool ignore_case, bool is_ascii)
1334 : ignore_case_(ignore_case),
1335 is_ascii_(is_ascii),
1336 error_message_(NULL) { }
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001337 void EnsureAnalyzed(RegExpNode* node);
1338
1339#define DECLARE_VISIT(Type) \
1340 virtual void Visit##Type(Type##Node* that);
1341FOR_EACH_NODE_TYPE(DECLARE_VISIT)
1342#undef DECLARE_VISIT
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001343 virtual void VisitLoopChoice(LoopChoiceNode* that);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001344
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00001345 bool has_failed() { return error_message_ != NULL; }
1346 const char* error_message() {
1347 ASSERT(error_message_ != NULL);
1348 return error_message_;
1349 }
1350 void fail(const char* error_message) {
1351 error_message_ = error_message;
1352 }
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001353 private:
1354 bool ignore_case_;
ager@chromium.org38e4c712009-11-11 09:11:58 +00001355 bool is_ascii_;
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00001356 const char* error_message_;
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001357
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001358 DISALLOW_IMPLICIT_CONSTRUCTORS(Analysis);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001359};
1360
1361
ager@chromium.org8bb60582008-12-11 12:02:20 +00001362struct RegExpCompileData {
1363 RegExpCompileData()
1364 : tree(NULL),
1365 node(NULL),
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001366 simple(true),
iposva@chromium.org245aa852009-02-10 00:49:54 +00001367 contains_anchor(false),
ager@chromium.org8bb60582008-12-11 12:02:20 +00001368 capture_count(0) { }
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001369 RegExpTree* tree;
ager@chromium.org8bb60582008-12-11 12:02:20 +00001370 RegExpNode* node;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001371 bool simple;
iposva@chromium.org245aa852009-02-10 00:49:54 +00001372 bool contains_anchor;
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001373 Handle<String> error;
1374 int capture_count;
1375};
1376
1377
1378class RegExpEngine: public AllStatic {
1379 public:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001380 struct CompilationResult {
1381 explicit CompilationResult(const char* error_message)
1382 : error_message(error_message),
1383 code(Heap::the_hole_value()),
1384 num_registers(0) {}
1385 CompilationResult(Object* code, int registers)
1386 : error_message(NULL),
1387 code(code),
1388 num_registers(registers) {}
1389 const char* error_message;
1390 Object* code;
1391 int num_registers;
1392 };
1393
1394 static CompilationResult Compile(RegExpCompileData* input,
1395 bool ignore_case,
1396 bool multiline,
1397 Handle<String> pattern,
1398 bool is_ascii);
ager@chromium.org8bb60582008-12-11 12:02:20 +00001399
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001400 static void DotPrint(const char* label, RegExpNode* node, bool ignore_case);
1401};
1402
1403
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001404class OffsetsVector {
1405 public:
1406 inline OffsetsVector(int num_registers)
1407 : offsets_vector_length_(num_registers) {
1408 if (offsets_vector_length_ > kStaticOffsetsVectorSize) {
1409 vector_ = NewArray<int>(offsets_vector_length_);
1410 } else {
1411 vector_ = static_offsets_vector_;
1412 }
1413 }
1414 inline ~OffsetsVector() {
1415 if (offsets_vector_length_ > kStaticOffsetsVectorSize) {
1416 DeleteArray(vector_);
1417 vector_ = NULL;
1418 }
1419 }
1420 inline int* vector() { return vector_; }
1421 inline int length() { return offsets_vector_length_; }
1422
1423 static const int kStaticOffsetsVectorSize = 50;
1424
1425 private:
1426 static Address static_offsets_vector_address() {
1427 return reinterpret_cast<Address>(&static_offsets_vector_);
1428 }
1429
1430 int* vector_;
1431 int offsets_vector_length_;
1432 static int static_offsets_vector_[kStaticOffsetsVectorSize];
1433
1434 friend class ExternalReference;
1435};
1436
1437
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001438} } // namespace v8::internal
1439
1440#endif // V8_JSREGEXP_H_