blob: 4053799161a6ae193eb4fbe41f40d5583456e3d7 [file] [log] [blame]
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001// Copyright 2012 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#ifndef V8_JSREGEXP_H_
29#define V8_JSREGEXP_H_
30
lrn@chromium.org1c092762011-05-09 09:42:16 +000031#include "allocation.h"
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000032#include "assembler.h"
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000033#include "zone-inl.h"
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +000034
kasperl@chromium.org71affb52009-05-26 05:44:31 +000035namespace v8 {
36namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000037
yangguo@chromium.org659ceec2012-01-26 07:37:54 +000038class NodeVisitor;
39class RegExpCompiler;
ager@chromium.orga74f0da2008-12-03 16:05:52 +000040class RegExpMacroAssembler;
yangguo@chromium.org659ceec2012-01-26 07:37:54 +000041class RegExpNode;
42class RegExpTree;
erik.corry@gmail.comed49e962012-04-17 11:57:53 +000043class BoyerMooreLookahead;
ager@chromium.orga74f0da2008-12-03 16:05:52 +000044
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000045class RegExpImpl {
46 public:
kasperl@chromium.org68ac0092009-07-09 06:00:35 +000047 // Whether V8 is compiled with native regexp support or not.
48 static bool UsesNativeRegExp() {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000049#ifdef V8_INTERPRETED_REGEXP
kasperl@chromium.org68ac0092009-07-09 06:00:35 +000050 return false;
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000051#else
52 return true;
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000053#endif
54 }
kasperl@chromium.org68ac0092009-07-09 06:00:35 +000055
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000056 // Creates a regular expression literal in the old space.
57 // This function calls the garbage collector if necessary.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000058 static Handle<Object> CreateRegExpLiteral(Handle<JSFunction> constructor,
59 Handle<String> pattern,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000060 Handle<String> flags,
61 bool* has_pending_exception);
62
63 // Returns a string representation of a regular expression.
64 // Implements RegExp.prototype.toString, see ECMA-262 section 15.10.6.4.
65 // This function calls the garbage collector if necessary.
66 static Handle<String> ToString(Handle<Object> value);
67
ager@chromium.org8bb60582008-12-11 12:02:20 +000068 // Parses the RegExp pattern and prepares the JSRegExp object with
69 // generic data and choice of implementation - as well as what
70 // the implementation wants to store in the data field.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000071 // Returns false if compilation fails.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000072 static Handle<Object> Compile(Handle<JSRegExp> re,
73 Handle<String> pattern,
74 Handle<String> flags);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000075
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000076 // See ECMA-262 section 15.10.6.2.
77 // This function calls the garbage collector if necessary.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000078 static Handle<Object> Exec(Handle<JSRegExp> regexp,
79 Handle<String> subject,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000080 int index,
81 Handle<JSArray> lastMatchInfo);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000082
ager@chromium.org8bb60582008-12-11 12:02:20 +000083 // Prepares a JSRegExp object with Irregexp-specific data.
whesse@chromium.orgcec079d2010-03-22 14:44:04 +000084 static void IrregexpInitialize(Handle<JSRegExp> re,
85 Handle<String> pattern,
86 JSRegExp::Flags flags,
87 int capture_register_count);
ager@chromium.orga74f0da2008-12-03 16:05:52 +000088
89
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000090 static void AtomCompile(Handle<JSRegExp> re,
91 Handle<String> pattern,
92 JSRegExp::Flags flags,
93 Handle<String> match_pattern);
94
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000095 static Handle<Object> AtomExec(Handle<JSRegExp> regexp,
96 Handle<String> subject,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000097 int index,
98 Handle<JSArray> lastMatchInfo);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000099
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000100 enum IrregexpResult { RE_FAILURE = 0, RE_SUCCESS = 1, RE_EXCEPTION = -1 };
101
102 // Prepare a RegExp for being executed one or more times (using
103 // IrregexpExecOnce) on the subject.
104 // This ensures that the regexp is compiled for the subject, and that
105 // the subject is flat.
106 // Returns the number of integer spaces required by IrregexpExecOnce
107 // as its "registers" argument. If the regexp cannot be compiled,
108 // an exception is set as pending, and this function returns negative.
109 static int IrregexpPrepare(Handle<JSRegExp> regexp,
110 Handle<String> subject);
111
112 // Execute a regular expression once on the subject, starting from
113 // character "index".
114 // If successful, returns RE_SUCCESS and set the capture positions
115 // in the first registers.
116 // If matching fails, returns RE_FAILURE.
117 // If execution fails, sets a pending exception and returns RE_EXCEPTION.
118 static IrregexpResult IrregexpExecOnce(Handle<JSRegExp> regexp,
119 Handle<String> subject,
120 int index,
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000121 Vector<int> registers);
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000122
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000123 // Execute an Irregexp bytecode pattern.
ager@chromium.org41826e72009-03-30 13:30:57 +0000124 // On a successful match, the result is a JSArray containing
125 // captured positions. On a failure, the result is the null value.
126 // Returns an empty handle in case of an exception.
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000127 static Handle<Object> IrregexpExec(Handle<JSRegExp> regexp,
128 Handle<String> subject,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000129 int index,
130 Handle<JSArray> lastMatchInfo);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000131
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000132 // Array index in the lastMatchInfo array.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000133 static const int kLastCaptureCount = 0;
134 static const int kLastSubject = 1;
135 static const int kLastInput = 2;
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000136 static const int kFirstCapture = 3;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000137 static const int kLastMatchOverhead = 3;
138
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000139 // Direct offset into the lastMatchInfo array.
140 static const int kLastCaptureCountOffset =
141 FixedArray::kHeaderSize + kLastCaptureCount * kPointerSize;
142 static const int kLastSubjectOffset =
143 FixedArray::kHeaderSize + kLastSubject * kPointerSize;
144 static const int kLastInputOffset =
145 FixedArray::kHeaderSize + kLastInput * kPointerSize;
146 static const int kFirstCaptureOffset =
147 FixedArray::kHeaderSize + kFirstCapture * kPointerSize;
148
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000149 // Used to access the lastMatchInfo array.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000150 static int GetCapture(FixedArray* array, int index) {
151 return Smi::cast(array->get(index + kFirstCapture))->value();
152 }
153
154 static void SetLastCaptureCount(FixedArray* array, int to) {
155 array->set(kLastCaptureCount, Smi::FromInt(to));
156 }
157
158 static void SetLastSubject(FixedArray* array, String* to) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000159 array->set(kLastSubject, to);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000160 }
161
162 static void SetLastInput(FixedArray* array, String* to) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000163 array->set(kLastInput, to);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000164 }
165
166 static void SetCapture(FixedArray* array, int index, int to) {
167 array->set(index + kFirstCapture, Smi::FromInt(to));
168 }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000169
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000170 static int GetLastCaptureCount(FixedArray* array) {
171 return Smi::cast(array->get(kLastCaptureCount))->value();
172 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000173
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000174 // For acting on the JSRegExp data FixedArray.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000175 static int IrregexpMaxRegisterCount(FixedArray* re);
176 static void SetIrregexpMaxRegisterCount(FixedArray* re, int value);
177 static int IrregexpNumberOfCaptures(FixedArray* re);
178 static int IrregexpNumberOfRegisters(FixedArray* re);
179 static ByteArray* IrregexpByteCode(FixedArray* re, bool is_ascii);
180 static Code* IrregexpNativeCode(FixedArray* re, bool is_ascii);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000181
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000182 // Limit the space regexps take up on the heap. In order to limit this we
183 // would like to keep track of the amount of regexp code on the heap. This
184 // is not tracked, however. As a conservative approximation we track the
185 // total regexp code compiled including code that has subsequently been freed
186 // and the total executable memory at any point.
187 static const int kRegExpExecutableMemoryLimit = 16 * MB;
188 static const int kRegWxpCompiledLimit = 1 * MB;
189
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000190 private:
191 static String* last_ascii_string_;
192 static String* two_byte_cached_string_;
193
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000194 static bool CompileIrregexp(
195 Handle<JSRegExp> re, Handle<String> sample_subject, bool is_ascii);
196 static inline bool EnsureCompiledIrregexp(
197 Handle<JSRegExp> re, Handle<String> sample_subject, bool is_ascii);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000198
199
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000200 // Set the subject cache. The previous string buffer is not deleted, so the
201 // caller should ensure that it doesn't leak.
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000202 static void SetSubjectCache(String* subject,
203 char* utf8_subject,
204 int uft8_length,
205 int character_position,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000206 int utf8_position);
207
208 // A one element cache of the last utf8_subject string and its length. The
209 // subject JS String object is cached in the heap. We also cache a
210 // translation between position and utf8 position.
211 static char* utf8_subject_cache_;
212 static int utf8_length_cache_;
213 static int utf8_position_;
214 static int character_position_;
215};
216
217
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000218// Represents the location of one element relative to the intersection of
219// two sets. Corresponds to the four areas of a Venn diagram.
220enum ElementInSetsRelation {
221 kInsideNone = 0,
222 kInsideFirst = 1,
223 kInsideSecond = 2,
224 kInsideBoth = 3
225};
226
227
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000228class CharacterRange {
229 public:
230 CharacterRange() : from_(0), to_(0) { }
231 // For compatibility with the CHECK_OK macro
232 CharacterRange(void* null) { ASSERT_EQ(NULL, null); } //NOLINT
233 CharacterRange(uc16 from, uc16 to) : from_(from), to_(to) { }
234 static void AddClassEscape(uc16 type, ZoneList<CharacterRange>* ranges);
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000235 static Vector<const int> GetWordBounds();
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000236 static inline CharacterRange Singleton(uc16 value) {
237 return CharacterRange(value, value);
238 }
239 static inline CharacterRange Range(uc16 from, uc16 to) {
240 ASSERT(from <= to);
241 return CharacterRange(from, to);
242 }
243 static inline CharacterRange Everything() {
244 return CharacterRange(0, 0xFFFF);
245 }
246 bool Contains(uc16 i) { return from_ <= i && i <= to_; }
247 uc16 from() const { return from_; }
248 void set_from(uc16 value) { from_ = value; }
249 uc16 to() const { return to_; }
250 void set_to(uc16 value) { to_ = value; }
251 bool is_valid() { return from_ <= to_; }
ager@chromium.org8bb60582008-12-11 12:02:20 +0000252 bool IsEverything(uc16 max) { return from_ == 0 && to_ >= max; }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000253 bool IsSingleton() { return (from_ == to_); }
ager@chromium.org38e4c712009-11-11 09:11:58 +0000254 void AddCaseEquivalents(ZoneList<CharacterRange>* ranges, bool is_ascii);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000255 static void Split(ZoneList<CharacterRange>* base,
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000256 Vector<const int> overlay,
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000257 ZoneList<CharacterRange>** included,
258 ZoneList<CharacterRange>** excluded);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000259 // Whether a range list is in canonical form: Ranges ordered by from value,
260 // and ranges non-overlapping and non-adjacent.
261 static bool IsCanonical(ZoneList<CharacterRange>* ranges);
262 // Convert range list to canonical form. The characters covered by the ranges
263 // will still be the same, but no character is in more than one range, and
264 // adjacent ranges are merged. The resulting list may be shorter than the
265 // original, but cannot be longer.
266 static void Canonicalize(ZoneList<CharacterRange>* ranges);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000267 // Negate the contents of a character range in canonical form.
268 static void Negate(ZoneList<CharacterRange>* src,
269 ZoneList<CharacterRange>* dst);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000270 static const int kStartMarker = (1 << 24);
271 static const int kPayloadMask = (1 << 24) - 1;
272
273 private:
274 uc16 from_;
275 uc16 to_;
276};
277
278
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000279// A set of unsigned integers that behaves especially well on small
280// integers (< 32). May do zone-allocation.
281class OutSet: public ZoneObject {
282 public:
283 OutSet() : first_(0), remaining_(NULL), successors_(NULL) { }
284 OutSet* Extend(unsigned value);
285 bool Get(unsigned value);
286 static const unsigned kFirstLimit = 32;
287
288 private:
289 // Destructively set a value in this set. In most cases you want
290 // to use Extend instead to ensure that only one instance exists
291 // that contains the same values.
292 void Set(unsigned value);
293
294 // The successors are a list of sets that contain the same values
295 // as this set and the one more value that is not present in this
296 // set.
297 ZoneList<OutSet*>* successors() { return successors_; }
298
299 OutSet(uint32_t first, ZoneList<unsigned>* remaining)
300 : first_(first), remaining_(remaining), successors_(NULL) { }
301 uint32_t first_;
302 ZoneList<unsigned>* remaining_;
303 ZoneList<OutSet*>* successors_;
ager@chromium.org32912102009-01-16 10:38:43 +0000304 friend class Trace;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000305};
306
307
308// A mapping from integers, specified as ranges, to a set of integers.
309// Used for mapping character ranges to choices.
310class DispatchTable : public ZoneObject {
311 public:
312 class Entry {
313 public:
314 Entry() : from_(0), to_(0), out_set_(NULL) { }
315 Entry(uc16 from, uc16 to, OutSet* out_set)
316 : from_(from), to_(to), out_set_(out_set) { }
317 uc16 from() { return from_; }
318 uc16 to() { return to_; }
319 void set_to(uc16 value) { to_ = value; }
320 void AddValue(int value) { out_set_ = out_set_->Extend(value); }
321 OutSet* out_set() { return out_set_; }
322 private:
323 uc16 from_;
324 uc16 to_;
325 OutSet* out_set_;
326 };
327
328 class Config {
329 public:
330 typedef uc16 Key;
331 typedef Entry Value;
332 static const uc16 kNoKey;
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000333 static const Entry NoValue() { return Value(); }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000334 static inline int Compare(uc16 a, uc16 b) {
335 if (a == b)
336 return 0;
337 else if (a < b)
338 return -1;
339 else
340 return 1;
341 }
342 };
343
344 void AddRange(CharacterRange range, int value);
345 OutSet* Get(uc16 value);
346 void Dump();
347
348 template <typename Callback>
349 void ForEach(Callback* callback) { return tree()->ForEach(callback); }
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000350
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000351 private:
352 // There can't be a static empty set since it allocates its
353 // successors in a zone and caches them.
354 OutSet* empty() { return &empty_; }
355 OutSet empty_;
356 ZoneSplayTree<Config>* tree() { return &tree_; }
357 ZoneSplayTree<Config> tree_;
358};
359
360
361#define FOR_EACH_NODE_TYPE(VISIT) \
362 VISIT(End) \
363 VISIT(Action) \
364 VISIT(Choice) \
365 VISIT(BackReference) \
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000366 VISIT(Assertion) \
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000367 VISIT(Text)
368
369
370#define FOR_EACH_REG_EXP_TREE_TYPE(VISIT) \
371 VISIT(Disjunction) \
372 VISIT(Alternative) \
373 VISIT(Assertion) \
374 VISIT(CharacterClass) \
375 VISIT(Atom) \
376 VISIT(Quantifier) \
377 VISIT(Capture) \
378 VISIT(Lookahead) \
379 VISIT(BackReference) \
380 VISIT(Empty) \
381 VISIT(Text)
382
383
384#define FORWARD_DECLARE(Name) class RegExp##Name;
385FOR_EACH_REG_EXP_TREE_TYPE(FORWARD_DECLARE)
386#undef FORWARD_DECLARE
387
388
389class TextElement {
390 public:
391 enum Type {UNINITIALIZED, ATOM, CHAR_CLASS};
392 TextElement() : type(UNINITIALIZED) { }
ager@chromium.org8bb60582008-12-11 12:02:20 +0000393 explicit TextElement(Type t) : type(t), cp_offset(-1) { }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000394 static TextElement Atom(RegExpAtom* atom);
395 static TextElement CharClass(RegExpCharacterClass* char_class);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000396 int length();
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000397 Type type;
398 union {
399 RegExpAtom* u_atom;
400 RegExpCharacterClass* u_char_class;
401 } data;
ager@chromium.org8bb60582008-12-11 12:02:20 +0000402 int cp_offset;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000403};
404
405
ager@chromium.org32912102009-01-16 10:38:43 +0000406class Trace;
ager@chromium.org8bb60582008-12-11 12:02:20 +0000407
408
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000409struct NodeInfo {
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000410 NodeInfo()
411 : being_analyzed(false),
412 been_analyzed(false),
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000413 follows_word_interest(false),
414 follows_newline_interest(false),
415 follows_start_interest(false),
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000416 at_end(false),
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000417 visited(false) { }
418
419 // Returns true if the interests and assumptions of this node
420 // matches the given one.
421 bool Matches(NodeInfo* that) {
422 return (at_end == that->at_end) &&
423 (follows_word_interest == that->follows_word_interest) &&
424 (follows_newline_interest == that->follows_newline_interest) &&
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000425 (follows_start_interest == that->follows_start_interest);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000426 }
427
428 // Updates the interests of this node given the interests of the
429 // node preceding it.
430 void AddFromPreceding(NodeInfo* that) {
431 at_end |= that->at_end;
432 follows_word_interest |= that->follows_word_interest;
433 follows_newline_interest |= that->follows_newline_interest;
434 follows_start_interest |= that->follows_start_interest;
435 }
436
ager@chromium.org8bb60582008-12-11 12:02:20 +0000437 bool HasLookbehind() {
438 return follows_word_interest ||
439 follows_newline_interest ||
440 follows_start_interest;
441 }
442
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000443 // Sets the interests of this node to include the interests of the
444 // following node.
445 void AddFromFollowing(NodeInfo* that) {
446 follows_word_interest |= that->follows_word_interest;
447 follows_newline_interest |= that->follows_newline_interest;
448 follows_start_interest |= that->follows_start_interest;
449 }
450
451 void ResetCompilationState() {
452 being_analyzed = false;
453 been_analyzed = false;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000454 }
455
456 bool being_analyzed: 1;
457 bool been_analyzed: 1;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000458
459 // These bits are set of this node has to know what the preceding
460 // character was.
461 bool follows_word_interest: 1;
462 bool follows_newline_interest: 1;
463 bool follows_start_interest: 1;
464
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000465 bool at_end: 1;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000466 bool visited: 1;
467};
468
469
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000470// Details of a quick mask-compare check that can look ahead in the
471// input stream.
472class QuickCheckDetails {
473 public:
474 QuickCheckDetails()
475 : characters_(0),
476 mask_(0),
iposva@chromium.org245aa852009-02-10 00:49:54 +0000477 value_(0),
478 cannot_match_(false) { }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000479 explicit QuickCheckDetails(int characters)
480 : characters_(characters),
481 mask_(0),
iposva@chromium.org245aa852009-02-10 00:49:54 +0000482 value_(0),
483 cannot_match_(false) { }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000484 bool Rationalize(bool ascii);
485 // Merge in the information from another branch of an alternation.
486 void Merge(QuickCheckDetails* other, int from_index);
487 // Advance the current position by some amount.
488 void Advance(int by, bool ascii);
489 void Clear();
iposva@chromium.org245aa852009-02-10 00:49:54 +0000490 bool cannot_match() { return cannot_match_; }
491 void set_cannot_match() { cannot_match_ = true; }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000492 struct Position {
493 Position() : mask(0), value(0), determines_perfectly(false) { }
494 uc16 mask;
495 uc16 value;
496 bool determines_perfectly;
497 };
498 int characters() { return characters_; }
499 void set_characters(int characters) { characters_ = characters; }
500 Position* positions(int index) {
501 ASSERT(index >= 0);
502 ASSERT(index < characters_);
503 return positions_ + index;
504 }
505 uint32_t mask() { return mask_; }
506 uint32_t value() { return value_; }
507
508 private:
509 // How many characters do we have quick check information from. This is
510 // the same for all branches of a choice node.
511 int characters_;
512 Position positions_[4];
513 // These values are the condensate of the above array after Rationalize().
514 uint32_t mask_;
515 uint32_t value_;
iposva@chromium.org245aa852009-02-10 00:49:54 +0000516 // If set to true, there is no way this quick check can match at all.
517 // E.g., if it requires to be at the start of the input, and isn't.
518 bool cannot_match_;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000519};
520
521
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000522class RegExpNode: public ZoneObject {
523 public:
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +0000524 RegExpNode() : trace_count_(0) {
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000525 bm_info_[0] = bm_info_[1] = NULL;
526 }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000527 virtual ~RegExpNode();
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000528 virtual void Accept(NodeVisitor* visitor) = 0;
529 // Generates a goto to this node or actually generates the code at this point.
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000530 virtual void Emit(RegExpCompiler* compiler, Trace* trace) = 0;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000531 // How many characters must this node consume at a minimum in order to
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000532 // succeed. If we have found at least 'still_to_find' characters that
533 // must be consumed there is no need to ask any following nodes whether
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000534 // they are sure to eat any more characters. The not_at_start argument is
535 // used to indicate that we know we are not at the start of the input. In
536 // this case anchored branches will always fail and can be ignored when
537 // determining how many characters are consumed on success.
538 virtual int EatsAtLeast(int still_to_find,
539 int recursion_depth,
540 bool not_at_start) = 0;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000541 // Emits some quick code that checks whether the preloaded characters match.
542 // Falls through on certain failure, jumps to the label on possible success.
543 // If the node cannot make a quick check it does nothing and returns false.
544 bool EmitQuickCheck(RegExpCompiler* compiler,
ager@chromium.org32912102009-01-16 10:38:43 +0000545 Trace* trace,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000546 bool preload_has_checked_bounds,
547 Label* on_possible_success,
548 QuickCheckDetails* details_return,
549 bool fall_through_on_failure);
550 // For a given number of characters this returns a mask and a value. The
551 // next n characters are anded with the mask and compared with the value.
552 // A comparison failure indicates the node cannot match the next n characters.
553 // A comparison success indicates the node may match.
554 virtual void GetQuickCheckDetails(QuickCheckDetails* details,
555 RegExpCompiler* compiler,
iposva@chromium.org245aa852009-02-10 00:49:54 +0000556 int characters_filled_in,
557 bool not_at_start) = 0;
ager@chromium.org8bb60582008-12-11 12:02:20 +0000558 static const int kNodeIsTooComplexForGreedyLoops = -1;
559 virtual int GreedyLoopTextLength() { return kNodeIsTooComplexForGreedyLoops; }
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000560 // Only returns the successor for a text node of length 1 that matches any
561 // character and that has no guards on it.
562 virtual RegExpNode* GetSuccessorOfOmnivorousTextNode(
563 RegExpCompiler* compiler) {
564 return NULL;
565 }
566
567 // Collects information on the possible code units (mod 128) that can match if
568 // we look forward. This is used for a Boyer-Moore-like string searching
569 // implementation. TODO(erikcorry): This should share more code with
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000570 // EatsAtLeast, GetQuickCheckDetails.
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000571 virtual void FillInBMInfo(
572 int offset, BoyerMooreLookahead* bm, bool not_at_start) {
573 UNREACHABLE();
574 }
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000575 // We want to avoid recalculating the lookahead info, so we store it on the
576 // node. Only info that is for this node is stored. We can tell that the
577 // info is for this node when offset == 0, so the information is calculated
578 // relative to this node.
579 void SaveBMInfo(BoyerMooreLookahead* bm, bool not_at_start, int offset) {
580 if (offset == 0) set_bm_info(not_at_start, bm);
581 }
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000582
ager@chromium.org8bb60582008-12-11 12:02:20 +0000583 Label* label() { return &label_; }
ulan@chromium.org2efb9002012-01-19 15:36:35 +0000584 // If non-generic code is generated for a node (i.e. the node is not at the
ager@chromium.org32912102009-01-16 10:38:43 +0000585 // start of the trace) then it cannot be reused. This variable sets a limit
586 // on how often we allow that to happen before we insist on starting a new
587 // trace and generating generic code for a node that can be reused by flushing
588 // the deferred actions in the current trace and generating a goto.
589 static const int kMaxCopiesCodeGenerated = 10;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000590
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000591 NodeInfo* info() { return &info_; }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000592
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000593 BoyerMooreLookahead* bm_info(bool not_at_start) {
594 return bm_info_[not_at_start ? 1 : 0];
595 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000596
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000597 protected:
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000598 enum LimitResult { DONE, CONTINUE };
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000599
ager@chromium.org32912102009-01-16 10:38:43 +0000600 LimitResult LimitVersions(RegExpCompiler* compiler, Trace* trace);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000601
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000602 // Returns a clone of this node initialized using the copy constructor
603 // of its concrete class. Note that the node may have to be pre-
ager@chromium.org32912102009-01-16 10:38:43 +0000604 // processed before it is on a usable state.
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000605 virtual RegExpNode* Clone() = 0;
606
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000607 void set_bm_info(bool not_at_start, BoyerMooreLookahead* bm) {
608 bm_info_[not_at_start ? 1 : 0] = bm;
609 }
610
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000611 private:
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000612 static const int kFirstCharBudget = 10;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000613 Label label_;
614 NodeInfo info_;
ager@chromium.org32912102009-01-16 10:38:43 +0000615 // This variable keeps track of how many times code has been generated for
616 // this node (in different traces). We don't keep track of where the
617 // generated code is located unless the code is generated at the start of
618 // a trace, in which case it is generic and can be reused by flushing the
619 // deferred operations in the current trace and generating a goto.
620 int trace_count_;
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000621 BoyerMooreLookahead* bm_info_[2];
ager@chromium.org32912102009-01-16 10:38:43 +0000622};
623
624
625// A simple closed interval.
626class Interval {
627 public:
628 Interval() : from_(kNone), to_(kNone) { }
629 Interval(int from, int to) : from_(from), to_(to) { }
630 Interval Union(Interval that) {
631 if (that.from_ == kNone)
632 return *this;
633 else if (from_ == kNone)
634 return that;
635 else
636 return Interval(Min(from_, that.from_), Max(to_, that.to_));
637 }
638 bool Contains(int value) {
639 return (from_ <= value) && (value <= to_);
640 }
641 bool is_empty() { return from_ == kNone; }
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000642 int from() const { return from_; }
643 int to() const { return to_; }
ager@chromium.org32912102009-01-16 10:38:43 +0000644 static Interval Empty() { return Interval(); }
645 static const int kNone = -1;
646 private:
647 int from_;
648 int to_;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000649};
650
651
652class SeqRegExpNode: public RegExpNode {
653 public:
654 explicit SeqRegExpNode(RegExpNode* on_success)
655 : on_success_(on_success) { }
656 RegExpNode* on_success() { return on_success_; }
657 void set_on_success(RegExpNode* node) { on_success_ = node; }
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000658 virtual void FillInBMInfo(
659 int offset, BoyerMooreLookahead* bm, bool not_at_start) {
660 on_success_->FillInBMInfo(offset, bm, not_at_start);
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000661 if (offset == 0) set_bm_info(not_at_start, bm);
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000662 }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000663 private:
664 RegExpNode* on_success_;
665};
666
667
668class ActionNode: public SeqRegExpNode {
669 public:
670 enum Type {
ager@chromium.org8bb60582008-12-11 12:02:20 +0000671 SET_REGISTER,
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000672 INCREMENT_REGISTER,
673 STORE_POSITION,
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000674 BEGIN_SUBMATCH,
ager@chromium.org32912102009-01-16 10:38:43 +0000675 POSITIVE_SUBMATCH_SUCCESS,
676 EMPTY_MATCH_CHECK,
677 CLEAR_CAPTURES
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000678 };
ager@chromium.org8bb60582008-12-11 12:02:20 +0000679 static ActionNode* SetRegister(int reg, int val, RegExpNode* on_success);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000680 static ActionNode* IncrementRegister(int reg, RegExpNode* on_success);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000681 static ActionNode* StorePosition(int reg,
682 bool is_capture,
683 RegExpNode* on_success);
ager@chromium.org32912102009-01-16 10:38:43 +0000684 static ActionNode* ClearCaptures(Interval range, RegExpNode* on_success);
685 static ActionNode* BeginSubmatch(int stack_pointer_reg,
686 int position_reg,
687 RegExpNode* on_success);
688 static ActionNode* PositiveSubmatchSuccess(int stack_pointer_reg,
689 int restore_reg,
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000690 int clear_capture_count,
691 int clear_capture_from,
ager@chromium.org32912102009-01-16 10:38:43 +0000692 RegExpNode* on_success);
693 static ActionNode* EmptyMatchCheck(int start_register,
694 int repetition_register,
695 int repetition_limit,
696 RegExpNode* on_success);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000697 virtual void Accept(NodeVisitor* visitor);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000698 virtual void Emit(RegExpCompiler* compiler, Trace* trace);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000699 virtual int EatsAtLeast(int still_to_find,
700 int recursion_depth,
701 bool not_at_start);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000702 virtual void GetQuickCheckDetails(QuickCheckDetails* details,
703 RegExpCompiler* compiler,
iposva@chromium.org245aa852009-02-10 00:49:54 +0000704 int filled_in,
705 bool not_at_start) {
706 return on_success()->GetQuickCheckDetails(
707 details, compiler, filled_in, not_at_start);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000708 }
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000709 virtual void FillInBMInfo(
710 int offset, BoyerMooreLookahead* bm, bool not_at_start);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000711 Type type() { return type_; }
712 // TODO(erikcorry): We should allow some action nodes in greedy loops.
713 virtual int GreedyLoopTextLength() { return kNodeIsTooComplexForGreedyLoops; }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000714 virtual ActionNode* Clone() { return new ActionNode(*this); }
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000715
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000716 private:
717 union {
718 struct {
719 int reg;
720 int value;
721 } u_store_register;
722 struct {
723 int reg;
724 } u_increment_register;
725 struct {
726 int reg;
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000727 bool is_capture;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000728 } u_position_register;
729 struct {
730 int stack_pointer_register;
731 int current_position_register;
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000732 int clear_register_count;
733 int clear_register_from;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000734 } u_submatch;
ager@chromium.org32912102009-01-16 10:38:43 +0000735 struct {
736 int start_register;
737 int repetition_register;
738 int repetition_limit;
739 } u_empty_match_check;
740 struct {
741 int range_from;
742 int range_to;
743 } u_clear_captures;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000744 } data_;
745 ActionNode(Type type, RegExpNode* on_success)
746 : SeqRegExpNode(on_success),
747 type_(type) { }
748 Type type_;
749 friend class DotPrinter;
750};
751
752
753class TextNode: public SeqRegExpNode {
754 public:
755 TextNode(ZoneList<TextElement>* elms,
ager@chromium.org8bb60582008-12-11 12:02:20 +0000756 RegExpNode* on_success)
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000757 : SeqRegExpNode(on_success),
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000758 elms_(elms) { }
759 TextNode(RegExpCharacterClass* that,
ager@chromium.org8bb60582008-12-11 12:02:20 +0000760 RegExpNode* on_success)
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000761 : SeqRegExpNode(on_success),
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000762 elms_(new ZoneList<TextElement>(1)) {
763 elms_->Add(TextElement::CharClass(that));
764 }
765 virtual void Accept(NodeVisitor* visitor);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000766 virtual void Emit(RegExpCompiler* compiler, Trace* trace);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000767 virtual int EatsAtLeast(int still_to_find,
768 int recursion_depth,
769 bool not_at_start);
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 characters_filled_in,
773 bool not_at_start);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000774 ZoneList<TextElement>* elements() { return elms_; }
ager@chromium.org38e4c712009-11-11 09:11:58 +0000775 void MakeCaseIndependent(bool is_ascii);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000776 virtual int GreedyLoopTextLength();
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000777 virtual RegExpNode* GetSuccessorOfOmnivorousTextNode(
778 RegExpCompiler* compiler);
779 virtual void FillInBMInfo(
780 int offset, BoyerMooreLookahead* bm, bool not_at_start);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000781 virtual TextNode* Clone() {
782 TextNode* result = new TextNode(*this);
783 result->CalculateOffsets();
784 return result;
785 }
786 void CalculateOffsets();
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000787
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000788 private:
789 enum TextEmitPassType {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000790 NON_ASCII_MATCH, // Check for characters that can't match.
791 SIMPLE_CHARACTER_MATCH, // Case-dependent single character check.
792 NON_LETTER_CHARACTER_MATCH, // Check characters that have no case equivs.
793 CASE_CHARACTER_MATCH, // Case-independent single character check.
794 CHARACTER_CLASS_MATCH // Character class.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000795 };
ager@chromium.org381abbb2009-02-25 13:23:22 +0000796 static bool SkipPass(int pass, bool ignore_case);
797 static const int kFirstRealPass = SIMPLE_CHARACTER_MATCH;
798 static const int kLastPass = CHARACTER_CLASS_MATCH;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000799 void TextEmitPass(RegExpCompiler* compiler,
800 TextEmitPassType pass,
801 bool preloaded,
ager@chromium.org32912102009-01-16 10:38:43 +0000802 Trace* trace,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000803 bool first_element_checked,
804 int* checked_up_to);
805 int Length();
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000806 ZoneList<TextElement>* elms_;
807};
808
809
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000810class AssertionNode: public SeqRegExpNode {
811 public:
812 enum AssertionNodeType {
813 AT_END,
814 AT_START,
815 AT_BOUNDARY,
816 AT_NON_BOUNDARY,
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000817 AFTER_NEWLINE
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000818 };
819 static AssertionNode* AtEnd(RegExpNode* on_success) {
820 return new AssertionNode(AT_END, on_success);
821 }
822 static AssertionNode* AtStart(RegExpNode* on_success) {
823 return new AssertionNode(AT_START, on_success);
824 }
825 static AssertionNode* AtBoundary(RegExpNode* on_success) {
826 return new AssertionNode(AT_BOUNDARY, on_success);
827 }
828 static AssertionNode* AtNonBoundary(RegExpNode* on_success) {
829 return new AssertionNode(AT_NON_BOUNDARY, on_success);
830 }
831 static AssertionNode* AfterNewline(RegExpNode* on_success) {
832 return new AssertionNode(AFTER_NEWLINE, on_success);
833 }
834 virtual void Accept(NodeVisitor* visitor);
835 virtual void Emit(RegExpCompiler* compiler, Trace* trace);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000836 virtual int EatsAtLeast(int still_to_find,
837 int recursion_depth,
838 bool not_at_start);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000839 virtual void GetQuickCheckDetails(QuickCheckDetails* details,
840 RegExpCompiler* compiler,
iposva@chromium.org245aa852009-02-10 00:49:54 +0000841 int filled_in,
842 bool not_at_start);
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000843 virtual void FillInBMInfo(
844 int offset, BoyerMooreLookahead* bm, bool not_at_start);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000845 virtual AssertionNode* Clone() { return new AssertionNode(*this); }
846 AssertionNodeType type() { return type_; }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000847 void set_type(AssertionNodeType type) { type_ = type; }
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000848
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000849 private:
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000850 void EmitBoundaryCheck(RegExpCompiler* compiler, Trace* trace);
851 enum IfPrevious { kIsNonWord, kIsWord };
852 void BacktrackIfPrevious(RegExpCompiler* compiler,
853 Trace* trace,
854 IfPrevious backtrack_if_previous);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000855 AssertionNode(AssertionNodeType t, RegExpNode* on_success)
856 : SeqRegExpNode(on_success), type_(t) { }
857 AssertionNodeType type_;
858};
859
860
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000861class BackReferenceNode: public SeqRegExpNode {
862 public:
863 BackReferenceNode(int start_reg,
864 int end_reg,
ager@chromium.org8bb60582008-12-11 12:02:20 +0000865 RegExpNode* on_success)
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000866 : SeqRegExpNode(on_success),
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000867 start_reg_(start_reg),
868 end_reg_(end_reg) { }
869 virtual void Accept(NodeVisitor* visitor);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000870 int start_register() { return start_reg_; }
871 int end_register() { return end_reg_; }
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000872 virtual void Emit(RegExpCompiler* compiler, Trace* trace);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000873 virtual int EatsAtLeast(int still_to_find,
874 int recursion_depth,
875 bool not_at_start);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000876 virtual void GetQuickCheckDetails(QuickCheckDetails* details,
877 RegExpCompiler* compiler,
iposva@chromium.org245aa852009-02-10 00:49:54 +0000878 int characters_filled_in,
879 bool not_at_start) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000880 return;
881 }
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000882 virtual void FillInBMInfo(
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000883 int offset, BoyerMooreLookahead* bm, bool not_at_start);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000884 virtual BackReferenceNode* Clone() { return new BackReferenceNode(*this); }
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000885
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000886 private:
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000887 int start_reg_;
888 int end_reg_;
889};
890
891
892class EndNode: public RegExpNode {
893 public:
ager@chromium.org8bb60582008-12-11 12:02:20 +0000894 enum Action { ACCEPT, BACKTRACK, NEGATIVE_SUBMATCH_SUCCESS };
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000895 explicit EndNode(Action action) : action_(action) { }
896 virtual void Accept(NodeVisitor* visitor);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000897 virtual void Emit(RegExpCompiler* compiler, Trace* trace);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000898 virtual int EatsAtLeast(int still_to_find,
899 int recursion_depth,
900 bool not_at_start) { return 0; }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000901 virtual void GetQuickCheckDetails(QuickCheckDetails* details,
902 RegExpCompiler* compiler,
iposva@chromium.org245aa852009-02-10 00:49:54 +0000903 int characters_filled_in,
904 bool not_at_start) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000905 // Returning 0 from EatsAtLeast should ensure we never get here.
906 UNREACHABLE();
907 }
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000908 virtual void FillInBMInfo(
909 int offset, BoyerMooreLookahead* bm, bool not_at_start) {
910 // Returning 0 from EatsAtLeast should ensure we never get here.
911 UNREACHABLE();
912 }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000913 virtual EndNode* Clone() { return new EndNode(*this); }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000914 private:
915 Action action_;
916};
917
918
ager@chromium.org8bb60582008-12-11 12:02:20 +0000919class NegativeSubmatchSuccess: public EndNode {
920 public:
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000921 NegativeSubmatchSuccess(int stack_pointer_reg,
922 int position_reg,
923 int clear_capture_count,
924 int clear_capture_start)
ager@chromium.org8bb60582008-12-11 12:02:20 +0000925 : EndNode(NEGATIVE_SUBMATCH_SUCCESS),
926 stack_pointer_register_(stack_pointer_reg),
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000927 current_position_register_(position_reg),
928 clear_capture_count_(clear_capture_count),
929 clear_capture_start_(clear_capture_start) { }
930 virtual void Emit(RegExpCompiler* compiler, Trace* trace);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000931
932 private:
933 int stack_pointer_register_;
934 int current_position_register_;
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000935 int clear_capture_count_;
936 int clear_capture_start_;
ager@chromium.org8bb60582008-12-11 12:02:20 +0000937};
938
939
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000940class Guard: public ZoneObject {
941 public:
942 enum Relation { LT, GEQ };
943 Guard(int reg, Relation op, int value)
944 : reg_(reg),
945 op_(op),
946 value_(value) { }
947 int reg() { return reg_; }
948 Relation op() { return op_; }
949 int value() { return value_; }
950
951 private:
952 int reg_;
953 Relation op_;
954 int value_;
955};
956
957
958class GuardedAlternative {
959 public:
960 explicit GuardedAlternative(RegExpNode* node) : node_(node), guards_(NULL) { }
961 void AddGuard(Guard* guard);
962 RegExpNode* node() { return node_; }
963 void set_node(RegExpNode* node) { node_ = node; }
964 ZoneList<Guard*>* guards() { return guards_; }
965
966 private:
967 RegExpNode* node_;
968 ZoneList<Guard*>* guards_;
969};
970
971
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000972class AlternativeGeneration;
973
974
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000975class ChoiceNode: public RegExpNode {
976 public:
ager@chromium.org8bb60582008-12-11 12:02:20 +0000977 explicit ChoiceNode(int expected_size)
978 : alternatives_(new ZoneList<GuardedAlternative>(expected_size)),
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000979 table_(NULL),
iposva@chromium.org245aa852009-02-10 00:49:54 +0000980 not_at_start_(false),
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000981 being_calculated_(false) { }
982 virtual void Accept(NodeVisitor* visitor);
983 void AddAlternative(GuardedAlternative node) { alternatives()->Add(node); }
984 ZoneList<GuardedAlternative>* alternatives() { return alternatives_; }
985 DispatchTable* GetTable(bool ignore_case);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000986 virtual void Emit(RegExpCompiler* compiler, Trace* trace);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000987 virtual int EatsAtLeast(int still_to_find,
988 int recursion_depth,
989 bool not_at_start);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000990 int EatsAtLeastHelper(int still_to_find,
991 int recursion_depth,
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000992 RegExpNode* ignore_this_node,
993 bool not_at_start);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000994 virtual void GetQuickCheckDetails(QuickCheckDetails* details,
995 RegExpCompiler* compiler,
iposva@chromium.org245aa852009-02-10 00:49:54 +0000996 int characters_filled_in,
997 bool not_at_start);
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000998 virtual void FillInBMInfo(
999 int offset, BoyerMooreLookahead* bm, bool not_at_start);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001000 virtual ChoiceNode* Clone() { return new ChoiceNode(*this); }
1001
1002 bool being_calculated() { return being_calculated_; }
iposva@chromium.org245aa852009-02-10 00:49:54 +00001003 bool not_at_start() { return not_at_start_; }
1004 void set_not_at_start() { not_at_start_ = true; }
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001005 void set_being_calculated(bool b) { being_calculated_ = b; }
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001006 virtual bool try_to_emit_quick_check_for_alternative(int i) { return true; }
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001007
ager@chromium.org8bb60582008-12-11 12:02:20 +00001008 protected:
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001009 int GreedyLoopTextLengthForAlternative(GuardedAlternative* alternative);
ager@chromium.org8bb60582008-12-11 12:02:20 +00001010 ZoneList<GuardedAlternative>* alternatives_;
1011
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001012 private:
1013 friend class DispatchTableConstructor;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001014 friend class Analysis;
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001015 void GenerateGuard(RegExpMacroAssembler* macro_assembler,
ager@chromium.org5ec48922009-05-05 07:25:34 +00001016 Guard* guard,
ager@chromium.org32912102009-01-16 10:38:43 +00001017 Trace* trace);
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00001018 int CalculatePreloadCharacters(RegExpCompiler* compiler, int eats_at_least);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001019 void EmitOutOfLineContinuation(RegExpCompiler* compiler,
ager@chromium.org32912102009-01-16 10:38:43 +00001020 Trace* trace,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001021 GuardedAlternative alternative,
1022 AlternativeGeneration* alt_gen,
1023 int preload_characters,
1024 bool next_expects_preload);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001025 DispatchTable* table_;
iposva@chromium.org245aa852009-02-10 00:49:54 +00001026 // If true, this node is never checked at the start of the input.
1027 // Allows a new trace to start with at_start() set to false.
1028 bool not_at_start_;
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001029 bool being_calculated_;
1030};
1031
1032
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001033class NegativeLookaheadChoiceNode: public ChoiceNode {
1034 public:
1035 explicit NegativeLookaheadChoiceNode(GuardedAlternative this_must_fail,
1036 GuardedAlternative then_do_this)
1037 : ChoiceNode(2) {
1038 AddAlternative(this_must_fail);
1039 AddAlternative(then_do_this);
1040 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001041 virtual int EatsAtLeast(int still_to_find,
1042 int recursion_depth,
1043 bool not_at_start);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001044 virtual void GetQuickCheckDetails(QuickCheckDetails* details,
1045 RegExpCompiler* compiler,
iposva@chromium.org245aa852009-02-10 00:49:54 +00001046 int characters_filled_in,
1047 bool not_at_start);
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00001048 virtual void FillInBMInfo(
1049 int offset, BoyerMooreLookahead* bm, bool not_at_start) {
1050 alternatives_->at(1).node()->FillInBMInfo(offset, bm, not_at_start);
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00001051 if (offset == 0) set_bm_info(not_at_start, bm);
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00001052 }
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001053 // For a negative lookahead we don't emit the quick check for the
1054 // alternative that is expected to fail. This is because quick check code
1055 // starts by loading enough characters for the alternative that takes fewest
1056 // characters, but on a negative lookahead the negative branch did not take
1057 // part in that calculation (EatsAtLeast) so the assumptions don't hold.
1058 virtual bool try_to_emit_quick_check_for_alternative(int i) { return i != 0; }
1059};
1060
1061
ager@chromium.org8bb60582008-12-11 12:02:20 +00001062class LoopChoiceNode: public ChoiceNode {
1063 public:
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001064 explicit LoopChoiceNode(bool body_can_be_zero_length)
1065 : ChoiceNode(2),
1066 loop_node_(NULL),
1067 continue_node_(NULL),
1068 body_can_be_zero_length_(body_can_be_zero_length) { }
1069 void AddLoopAlternative(GuardedAlternative alt);
1070 void AddContinueAlternative(GuardedAlternative alt);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001071 virtual void Emit(RegExpCompiler* compiler, Trace* trace);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001072 virtual int EatsAtLeast(int still_to_find,
1073 int recursion_depth,
1074 bool not_at_start);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001075 virtual void GetQuickCheckDetails(QuickCheckDetails* details,
1076 RegExpCompiler* compiler,
iposva@chromium.org245aa852009-02-10 00:49:54 +00001077 int characters_filled_in,
1078 bool not_at_start);
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00001079 virtual void FillInBMInfo(
1080 int offset, BoyerMooreLookahead* bm, bool not_at_start);
ager@chromium.org8bb60582008-12-11 12:02:20 +00001081 virtual LoopChoiceNode* Clone() { return new LoopChoiceNode(*this); }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001082 RegExpNode* loop_node() { return loop_node_; }
1083 RegExpNode* continue_node() { return continue_node_; }
1084 bool body_can_be_zero_length() { return body_can_be_zero_length_; }
1085 virtual void Accept(NodeVisitor* visitor);
1086
1087 private:
1088 // AddAlternative is made private for loop nodes because alternatives
1089 // should not be added freely, we need to keep track of which node
1090 // goes back to the node itself.
1091 void AddAlternative(GuardedAlternative node) {
1092 ChoiceNode::AddAlternative(node);
1093 }
1094
1095 RegExpNode* loop_node_;
1096 RegExpNode* continue_node_;
1097 bool body_can_be_zero_length_;
ager@chromium.org8bb60582008-12-11 12:02:20 +00001098};
1099
1100
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00001101// Improve the speed that we scan for an initial point where a non-anchored
1102// regexp can match by using a Boyer-Moore-like table. This is done by
1103// identifying non-greedy non-capturing loops in the nodes that eat any
1104// character one at a time. For example in the middle of the regexp
1105// /foo[\s\S]*?bar/ we find such a loop. There is also such a loop implicitly
1106// inserted at the start of any non-anchored regexp.
1107//
1108// When we have found such a loop we look ahead in the nodes to find the set of
1109// characters that can come at given distances. For example for the regexp
1110// /.?foo/ we know that there are at least 3 characters ahead of us, and the
1111// sets of characters that can occur are [any, [f, o], [o]]. We find a range in
1112// the lookahead info where the set of characters is reasonably constrained. In
1113// our example this is from index 1 to 2 (0 is not constrained). We can now
1114// look 3 characters ahead and if we don't find one of [f, o] (the union of
1115// [f, o] and [o]) then we can skip forwards by the range size (in this case 2).
1116//
1117// For Unicode input strings we do the same, but modulo 128.
1118//
1119// We also look at the first string fed to the regexp and use that to get a hint
1120// of the character frequencies in the inputs. This affects the assessment of
1121// whether the set of characters is 'reasonably constrained'.
1122//
1123// We also have another lookahead mechanism (called quick check in the code),
1124// which uses a wide load of multiple characters followed by a mask and compare
1125// to determine whether a match is possible at this point.
1126enum ContainedInLattice {
1127 kNotYet = 0,
1128 kLatticeIn = 1,
1129 kLatticeOut = 2,
1130 kLatticeUnknown = 3 // Can also mean both in and out.
1131};
1132
1133
1134inline ContainedInLattice Combine(ContainedInLattice a, ContainedInLattice b) {
1135 return static_cast<ContainedInLattice>(a | b);
1136}
1137
1138
1139ContainedInLattice AddRange(ContainedInLattice a,
1140 const int* ranges,
1141 int ranges_size,
1142 Interval new_range);
1143
1144
1145class BoyerMoorePositionInfo : public ZoneObject {
1146 public:
1147 BoyerMoorePositionInfo()
1148 : map_(new ZoneList<bool>(kMapSize)),
1149 map_count_(0),
1150 w_(kNotYet),
1151 s_(kNotYet),
1152 d_(kNotYet),
1153 surrogate_(kNotYet) {
1154 for (int i = 0; i < kMapSize; i++) {
1155 map_->Add(false);
1156 }
1157 }
1158
1159 bool& at(int i) { return map_->at(i); }
1160
1161 static const int kMapSize = 128;
1162 static const int kMask = kMapSize - 1;
1163
1164 int map_count() const { return map_count_; }
1165
1166 void Set(int character);
1167 void SetInterval(const Interval& interval);
1168 void SetAll();
1169 bool is_non_word() { return w_ == kLatticeOut; }
1170 bool is_word() { return w_ == kLatticeIn; }
1171
1172 private:
1173 ZoneList<bool>* map_;
1174 int map_count_; // Number of set bits in the map.
1175 ContainedInLattice w_; // The \w character class.
1176 ContainedInLattice s_; // The \s character class.
1177 ContainedInLattice d_; // The \d character class.
1178 ContainedInLattice surrogate_; // Surrogate UTF-16 code units.
1179};
1180
1181
1182class BoyerMooreLookahead : public ZoneObject {
1183 public:
1184 BoyerMooreLookahead(int length, RegExpCompiler* compiler);
1185
1186 int length() { return length_; }
1187 int max_char() { return max_char_; }
1188 RegExpCompiler* compiler() { return compiler_; }
1189
1190 int Count(int map_number) {
1191 return bitmaps_->at(map_number)->map_count();
1192 }
1193
1194 BoyerMoorePositionInfo* at(int i) { return bitmaps_->at(i); }
1195
1196 void Set(int map_number, int character) {
1197 if (character > max_char_) return;
1198 BoyerMoorePositionInfo* info = bitmaps_->at(map_number);
1199 info->Set(character);
1200 }
1201
1202 void SetInterval(int map_number, const Interval& interval) {
1203 if (interval.from() > max_char_) return;
1204 BoyerMoorePositionInfo* info = bitmaps_->at(map_number);
1205 if (interval.to() > max_char_) {
1206 info->SetInterval(Interval(interval.from(), max_char_));
1207 } else {
1208 info->SetInterval(interval);
1209 }
1210 }
1211
1212 void SetAll(int map_number) {
1213 bitmaps_->at(map_number)->SetAll();
1214 }
1215
1216 void SetRest(int from_map) {
1217 for (int i = from_map; i < length_; i++) SetAll(i);
1218 }
1219 bool EmitSkipInstructions(RegExpMacroAssembler* masm);
1220
1221 private:
1222 // This is the value obtained by EatsAtLeast. If we do not have at least this
1223 // many characters left in the sample string then the match is bound to fail.
1224 // Therefore it is OK to read a character this far ahead of the current match
1225 // point.
1226 int length_;
1227 RegExpCompiler* compiler_;
1228 // 0x7f for ASCII, 0xffff for UTF-16.
1229 int max_char_;
1230 ZoneList<BoyerMoorePositionInfo*>* bitmaps_;
1231
1232 int GetSkipTable(int min_lookahead,
1233 int max_lookahead,
1234 Handle<ByteArray> boolean_skip_table);
1235 bool FindWorthwhileInterval(int* from, int* to);
1236 int FindBestInterval(
1237 int max_number_of_chars, int old_biggest_points, int* from, int* to);
1238};
1239
1240
ager@chromium.org8bb60582008-12-11 12:02:20 +00001241// There are many ways to generate code for a node. This class encapsulates
1242// the current way we should be generating. In other words it encapsulates
ager@chromium.org32912102009-01-16 10:38:43 +00001243// the current state of the code generator. The effect of this is that we
1244// generate code for paths that the matcher can take through the regular
1245// expression. A given node in the regexp can be code-generated several times
1246// as it can be part of several traces. For example for the regexp:
1247// /foo(bar|ip)baz/ the code to match baz will be generated twice, once as part
1248// of the foo-bar-baz trace and once as part of the foo-ip-baz trace. The code
1249// to match foo is generated only once (the traces have a common prefix). The
1250// code to store the capture is deferred and generated (twice) after the places
1251// where baz has been matched.
1252class Trace {
ager@chromium.org8bb60582008-12-11 12:02:20 +00001253 public:
iposva@chromium.org245aa852009-02-10 00:49:54 +00001254 // A value for a property that is either known to be true, know to be false,
1255 // or not known.
1256 enum TriBool {
1257 UNKNOWN = -1, FALSE = 0, TRUE = 1
1258 };
1259
ager@chromium.org8bb60582008-12-11 12:02:20 +00001260 class DeferredAction {
1261 public:
1262 DeferredAction(ActionNode::Type type, int reg)
1263 : type_(type), reg_(reg), next_(NULL) { }
1264 DeferredAction* next() { return next_; }
ager@chromium.org32912102009-01-16 10:38:43 +00001265 bool Mentions(int reg);
ager@chromium.org8bb60582008-12-11 12:02:20 +00001266 int reg() { return reg_; }
1267 ActionNode::Type type() { return type_; }
1268 private:
1269 ActionNode::Type type_;
1270 int reg_;
1271 DeferredAction* next_;
ager@chromium.org32912102009-01-16 10:38:43 +00001272 friend class Trace;
ager@chromium.org8bb60582008-12-11 12:02:20 +00001273 };
1274
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001275 class DeferredCapture : public DeferredAction {
ager@chromium.org8bb60582008-12-11 12:02:20 +00001276 public:
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001277 DeferredCapture(int reg, bool is_capture, Trace* trace)
ager@chromium.org8bb60582008-12-11 12:02:20 +00001278 : DeferredAction(ActionNode::STORE_POSITION, reg),
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001279 cp_offset_(trace->cp_offset()),
1280 is_capture_(is_capture) { }
ager@chromium.org8bb60582008-12-11 12:02:20 +00001281 int cp_offset() { return cp_offset_; }
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001282 bool is_capture() { return is_capture_; }
ager@chromium.org8bb60582008-12-11 12:02:20 +00001283 private:
1284 int cp_offset_;
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001285 bool is_capture_;
ager@chromium.org8bb60582008-12-11 12:02:20 +00001286 void set_cp_offset(int cp_offset) { cp_offset_ = cp_offset; }
1287 };
1288
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001289 class DeferredSetRegister : public DeferredAction {
ager@chromium.org8bb60582008-12-11 12:02:20 +00001290 public:
1291 DeferredSetRegister(int reg, int value)
1292 : DeferredAction(ActionNode::SET_REGISTER, reg),
1293 value_(value) { }
1294 int value() { return value_; }
1295 private:
1296 int value_;
1297 };
1298
ager@chromium.org32912102009-01-16 10:38:43 +00001299 class DeferredClearCaptures : public DeferredAction {
1300 public:
1301 explicit DeferredClearCaptures(Interval range)
1302 : DeferredAction(ActionNode::CLEAR_CAPTURES, -1),
1303 range_(range) { }
1304 Interval range() { return range_; }
1305 private:
1306 Interval range_;
1307 };
1308
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001309 class DeferredIncrementRegister : public DeferredAction {
ager@chromium.org8bb60582008-12-11 12:02:20 +00001310 public:
1311 explicit DeferredIncrementRegister(int reg)
1312 : DeferredAction(ActionNode::INCREMENT_REGISTER, reg) { }
1313 };
1314
ager@chromium.org32912102009-01-16 10:38:43 +00001315 Trace()
ager@chromium.org8bb60582008-12-11 12:02:20 +00001316 : cp_offset_(0),
1317 actions_(NULL),
1318 backtrack_(NULL),
1319 stop_node_(NULL),
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001320 loop_label_(NULL),
1321 characters_preloaded_(0),
iposva@chromium.org245aa852009-02-10 00:49:54 +00001322 bound_checked_up_to_(0),
ager@chromium.org381abbb2009-02-25 13:23:22 +00001323 flush_budget_(100),
iposva@chromium.org245aa852009-02-10 00:49:54 +00001324 at_start_(UNKNOWN) { }
1325
ager@chromium.org32912102009-01-16 10:38:43 +00001326 // End the trace. This involves flushing the deferred actions in the trace
1327 // and pushing a backtrack location onto the backtrack stack. Once this is
1328 // done we can start a new trace or go to one that has already been
1329 // generated.
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001330 void Flush(RegExpCompiler* compiler, RegExpNode* successor);
ager@chromium.org8bb60582008-12-11 12:02:20 +00001331 int cp_offset() { return cp_offset_; }
1332 DeferredAction* actions() { return actions_; }
ager@chromium.org32912102009-01-16 10:38:43 +00001333 // A trivial trace is one that has no deferred actions or other state that
1334 // affects the assumptions used when generating code. There is no recorded
1335 // backtrack location in a trivial trace, so with a trivial trace we will
1336 // generate code that, on a failure to match, gets the backtrack location
1337 // from the backtrack stack rather than using a direct jump instruction. We
1338 // always start code generation with a trivial trace and non-trivial traces
1339 // are created as we emit code for nodes or add to the list of deferred
1340 // actions in the trace. The location of the code generated for a node using
1341 // a trivial trace is recorded in a label in the node so that gotos can be
1342 // generated to that code.
ager@chromium.org8bb60582008-12-11 12:02:20 +00001343 bool is_trivial() {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001344 return backtrack_ == NULL &&
1345 actions_ == NULL &&
1346 cp_offset_ == 0 &&
1347 characters_preloaded_ == 0 &&
1348 bound_checked_up_to_ == 0 &&
iposva@chromium.org245aa852009-02-10 00:49:54 +00001349 quick_check_performed_.characters() == 0 &&
1350 at_start_ == UNKNOWN;
ager@chromium.org8bb60582008-12-11 12:02:20 +00001351 }
iposva@chromium.org245aa852009-02-10 00:49:54 +00001352 TriBool at_start() { return at_start_; }
1353 void set_at_start(bool at_start) { at_start_ = at_start ? TRUE : FALSE; }
ager@chromium.org8bb60582008-12-11 12:02:20 +00001354 Label* backtrack() { return backtrack_; }
1355 Label* loop_label() { return loop_label_; }
1356 RegExpNode* stop_node() { return stop_node_; }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001357 int characters_preloaded() { return characters_preloaded_; }
1358 int bound_checked_up_to() { return bound_checked_up_to_; }
ager@chromium.org381abbb2009-02-25 13:23:22 +00001359 int flush_budget() { return flush_budget_; }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001360 QuickCheckDetails* quick_check_performed() { return &quick_check_performed_; }
1361 bool mentions_reg(int reg);
ager@chromium.org32912102009-01-16 10:38:43 +00001362 // Returns true if a deferred position store exists to the specified
1363 // register and stores the offset in the out-parameter. Otherwise
1364 // returns false.
1365 bool GetStoredPosition(int reg, int* cp_offset);
1366 // These set methods and AdvanceCurrentPositionInTrace should be used only on
1367 // new traces - the intention is that traces are immutable after creation.
ager@chromium.org8bb60582008-12-11 12:02:20 +00001368 void add_action(DeferredAction* new_action) {
1369 ASSERT(new_action->next_ == NULL);
1370 new_action->next_ = actions_;
1371 actions_ = new_action;
1372 }
ager@chromium.org8bb60582008-12-11 12:02:20 +00001373 void set_backtrack(Label* backtrack) { backtrack_ = backtrack; }
1374 void set_stop_node(RegExpNode* node) { stop_node_ = node; }
1375 void set_loop_label(Label* label) { loop_label_ = label; }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001376 void set_characters_preloaded(int count) { characters_preloaded_ = count; }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001377 void set_bound_checked_up_to(int to) { bound_checked_up_to_ = to; }
ager@chromium.org381abbb2009-02-25 13:23:22 +00001378 void set_flush_budget(int to) { flush_budget_ = to; }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001379 void set_quick_check_performed(QuickCheckDetails* d) {
1380 quick_check_performed_ = *d;
1381 }
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001382 void InvalidateCurrentCharacter();
1383 void AdvanceCurrentPositionInTrace(int by, RegExpCompiler* compiler);
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00001384
ager@chromium.org8bb60582008-12-11 12:02:20 +00001385 private:
1386 int FindAffectedRegisters(OutSet* affected_registers);
1387 void PerformDeferredActions(RegExpMacroAssembler* macro,
1388 int max_register,
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001389 OutSet& affected_registers,
1390 OutSet* registers_to_pop,
1391 OutSet* registers_to_clear);
ager@chromium.org8bb60582008-12-11 12:02:20 +00001392 void RestoreAffectedRegisters(RegExpMacroAssembler* macro,
1393 int max_register,
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001394 OutSet& registers_to_pop,
1395 OutSet& registers_to_clear);
ager@chromium.org8bb60582008-12-11 12:02:20 +00001396 int cp_offset_;
1397 DeferredAction* actions_;
1398 Label* backtrack_;
1399 RegExpNode* stop_node_;
1400 Label* loop_label_;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001401 int characters_preloaded_;
1402 int bound_checked_up_to_;
1403 QuickCheckDetails quick_check_performed_;
ager@chromium.org381abbb2009-02-25 13:23:22 +00001404 int flush_budget_;
iposva@chromium.org245aa852009-02-10 00:49:54 +00001405 TriBool at_start_;
ager@chromium.org8bb60582008-12-11 12:02:20 +00001406};
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001407
1408
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001409class NodeVisitor {
1410 public:
1411 virtual ~NodeVisitor() { }
1412#define DECLARE_VISIT(Type) \
1413 virtual void Visit##Type(Type##Node* that) = 0;
1414FOR_EACH_NODE_TYPE(DECLARE_VISIT)
1415#undef DECLARE_VISIT
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001416 virtual void VisitLoopChoice(LoopChoiceNode* that) { VisitChoice(that); }
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001417};
1418
1419
1420// Node visitor used to add the start set of the alternatives to the
1421// dispatch table of a choice node.
1422class DispatchTableConstructor: public NodeVisitor {
1423 public:
1424 DispatchTableConstructor(DispatchTable* table, bool ignore_case)
1425 : table_(table),
1426 choice_index_(-1),
1427 ignore_case_(ignore_case) { }
1428
1429 void BuildTable(ChoiceNode* node);
1430
1431 void AddRange(CharacterRange range) {
1432 table()->AddRange(range, choice_index_);
1433 }
1434
1435 void AddInverse(ZoneList<CharacterRange>* ranges);
1436
1437#define DECLARE_VISIT(Type) \
1438 virtual void Visit##Type(Type##Node* that);
1439FOR_EACH_NODE_TYPE(DECLARE_VISIT)
1440#undef DECLARE_VISIT
1441
1442 DispatchTable* table() { return table_; }
1443 void set_choice_index(int value) { choice_index_ = value; }
1444
1445 protected:
ager@chromium.org5ec48922009-05-05 07:25:34 +00001446 DispatchTable* table_;
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001447 int choice_index_;
1448 bool ignore_case_;
1449};
1450
1451
ager@chromium.org8bb60582008-12-11 12:02:20 +00001452// Assertion propagation moves information about assertions such as
1453// \b to the affected nodes. For instance, in /.\b./ information must
1454// be propagated to the first '.' that whatever follows needs to know
1455// if it matched a word or a non-word, and to the second '.' that it
1456// has to check if it succeeds a word or non-word. In this case the
1457// result will be something like:
1458//
1459// +-------+ +------------+
1460// | . | | . |
1461// +-------+ ---> +------------+
1462// | word? | | check word |
1463// +-------+ +------------+
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001464class Analysis: public NodeVisitor {
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001465 public:
ager@chromium.org38e4c712009-11-11 09:11:58 +00001466 Analysis(bool ignore_case, bool is_ascii)
1467 : ignore_case_(ignore_case),
1468 is_ascii_(is_ascii),
1469 error_message_(NULL) { }
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001470 void EnsureAnalyzed(RegExpNode* node);
1471
1472#define DECLARE_VISIT(Type) \
1473 virtual void Visit##Type(Type##Node* that);
1474FOR_EACH_NODE_TYPE(DECLARE_VISIT)
1475#undef DECLARE_VISIT
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001476 virtual void VisitLoopChoice(LoopChoiceNode* that);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001477
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00001478 bool has_failed() { return error_message_ != NULL; }
1479 const char* error_message() {
1480 ASSERT(error_message_ != NULL);
1481 return error_message_;
1482 }
1483 void fail(const char* error_message) {
1484 error_message_ = error_message;
1485 }
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00001486
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001487 private:
1488 bool ignore_case_;
ager@chromium.org38e4c712009-11-11 09:11:58 +00001489 bool is_ascii_;
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00001490 const char* error_message_;
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001491
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001492 DISALLOW_IMPLICIT_CONSTRUCTORS(Analysis);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001493};
1494
1495
ager@chromium.org8bb60582008-12-11 12:02:20 +00001496struct RegExpCompileData {
1497 RegExpCompileData()
1498 : tree(NULL),
1499 node(NULL),
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001500 simple(true),
iposva@chromium.org245aa852009-02-10 00:49:54 +00001501 contains_anchor(false),
ager@chromium.org8bb60582008-12-11 12:02:20 +00001502 capture_count(0) { }
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001503 RegExpTree* tree;
ager@chromium.org8bb60582008-12-11 12:02:20 +00001504 RegExpNode* node;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001505 bool simple;
iposva@chromium.org245aa852009-02-10 00:49:54 +00001506 bool contains_anchor;
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001507 Handle<String> error;
1508 int capture_count;
1509};
1510
1511
1512class RegExpEngine: public AllStatic {
1513 public:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001514 struct CompilationResult {
1515 explicit CompilationResult(const char* error_message)
1516 : error_message(error_message),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001517 code(HEAP->the_hole_value()),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001518 num_registers(0) {}
1519 CompilationResult(Object* code, int registers)
1520 : error_message(NULL),
1521 code(code),
1522 num_registers(registers) {}
1523 const char* error_message;
1524 Object* code;
1525 int num_registers;
1526 };
1527
1528 static CompilationResult Compile(RegExpCompileData* input,
1529 bool ignore_case,
1530 bool multiline,
1531 Handle<String> pattern,
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00001532 Handle<String> sample_subject,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001533 bool is_ascii);
ager@chromium.org8bb60582008-12-11 12:02:20 +00001534
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001535 static void DotPrint(const char* label, RegExpNode* node, bool ignore_case);
1536};
1537
1538
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001539class OffsetsVector {
1540 public:
ulan@chromium.org812308e2012-02-29 15:58:45 +00001541 inline OffsetsVector(int num_registers, Isolate* isolate)
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001542 : offsets_vector_length_(num_registers) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001543 if (offsets_vector_length_ > Isolate::kJSRegexpStaticOffsetsVectorSize) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001544 vector_ = NewArray<int>(offsets_vector_length_);
1545 } else {
ulan@chromium.org812308e2012-02-29 15:58:45 +00001546 vector_ = isolate->jsregexp_static_offsets_vector();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001547 }
1548 }
1549 inline ~OffsetsVector() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001550 if (offsets_vector_length_ > Isolate::kJSRegexpStaticOffsetsVectorSize) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001551 DeleteArray(vector_);
1552 vector_ = NULL;
1553 }
1554 }
1555 inline int* vector() { return vector_; }
1556 inline int length() { return offsets_vector_length_; }
1557
1558 static const int kStaticOffsetsVectorSize = 50;
1559
1560 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001561 static Address static_offsets_vector_address(Isolate* isolate) {
1562 return reinterpret_cast<Address>(isolate->jsregexp_static_offsets_vector());
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001563 }
1564
1565 int* vector_;
1566 int offsets_vector_length_;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001567
1568 friend class ExternalReference;
1569};
1570
1571
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001572} } // namespace v8::internal
1573
1574#endif // V8_JSREGEXP_H_