blob: 9a84237fde95ba829715e03d6947acd7444959e9 [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,
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +000074 Handle<String> flags,
75 Zone* zone);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000076
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000077 // See ECMA-262 section 15.10.6.2.
78 // This function calls the garbage collector if necessary.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000079 static Handle<Object> Exec(Handle<JSRegExp> regexp,
80 Handle<String> subject,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000081 int index,
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +000082 Handle<JSArray> lastMatchInfo);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000083
ager@chromium.org8bb60582008-12-11 12:02:20 +000084 // Prepares a JSRegExp object with Irregexp-specific data.
whesse@chromium.orgcec079d2010-03-22 14:44:04 +000085 static void IrregexpInitialize(Handle<JSRegExp> re,
86 Handle<String> pattern,
87 JSRegExp::Flags flags,
88 int capture_register_count);
ager@chromium.orga74f0da2008-12-03 16:05:52 +000089
90
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000091 static void AtomCompile(Handle<JSRegExp> re,
92 Handle<String> pattern,
93 JSRegExp::Flags flags,
94 Handle<String> match_pattern);
95
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000096 static Handle<Object> AtomExec(Handle<JSRegExp> regexp,
97 Handle<String> subject,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000098 int index,
99 Handle<JSArray> lastMatchInfo);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000100
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000101 enum IrregexpResult { RE_FAILURE = 0, RE_SUCCESS = 1, RE_EXCEPTION = -1 };
102
103 // Prepare a RegExp for being executed one or more times (using
104 // IrregexpExecOnce) on the subject.
105 // This ensures that the regexp is compiled for the subject, and that
106 // the subject is flat.
107 // Returns the number of integer spaces required by IrregexpExecOnce
108 // as its "registers" argument. If the regexp cannot be compiled,
109 // an exception is set as pending, and this function returns negative.
110 static int IrregexpPrepare(Handle<JSRegExp> regexp,
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000111 Handle<String> subject);
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000112
mstarzinger@chromium.org15613d02012-05-23 12:04:37 +0000113 // Calculate the size of offsets vector for the case of global regexp
114 // and the number of matches this vector is able to store.
115 static int GlobalOffsetsVectorSize(Handle<JSRegExp> regexp,
116 int registers_per_match,
117 int* max_matches);
118
119 // Execute a regular expression on the subject, starting from index.
120 // If matching succeeds, return the number of matches. This can be larger
121 // than one in the case of global regular expressions.
122 // The captures and subcaptures are stored into the registers vector.
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000123 // If matching fails, returns RE_FAILURE.
124 // If execution fails, sets a pending exception and returns RE_EXCEPTION.
mstarzinger@chromium.org15613d02012-05-23 12:04:37 +0000125 static int IrregexpExecRaw(Handle<JSRegExp> regexp,
126 Handle<String> subject,
127 int index,
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000128 Vector<int> registers);
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000129
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000130 // Execute an Irregexp bytecode pattern.
ager@chromium.org41826e72009-03-30 13:30:57 +0000131 // On a successful match, the result is a JSArray containing
132 // captured positions. On a failure, the result is the null value.
133 // Returns an empty handle in case of an exception.
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000134 static Handle<Object> IrregexpExec(Handle<JSRegExp> regexp,
135 Handle<String> subject,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000136 int index,
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000137 Handle<JSArray> lastMatchInfo);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000138
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000139 // Array index in the lastMatchInfo array.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000140 static const int kLastCaptureCount = 0;
141 static const int kLastSubject = 1;
142 static const int kLastInput = 2;
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000143 static const int kFirstCapture = 3;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000144 static const int kLastMatchOverhead = 3;
145
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000146 // Direct offset into the lastMatchInfo array.
147 static const int kLastCaptureCountOffset =
148 FixedArray::kHeaderSize + kLastCaptureCount * kPointerSize;
149 static const int kLastSubjectOffset =
150 FixedArray::kHeaderSize + kLastSubject * kPointerSize;
151 static const int kLastInputOffset =
152 FixedArray::kHeaderSize + kLastInput * kPointerSize;
153 static const int kFirstCaptureOffset =
154 FixedArray::kHeaderSize + kFirstCapture * kPointerSize;
155
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000156 // Used to access the lastMatchInfo array.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000157 static int GetCapture(FixedArray* array, int index) {
158 return Smi::cast(array->get(index + kFirstCapture))->value();
159 }
160
161 static void SetLastCaptureCount(FixedArray* array, int to) {
162 array->set(kLastCaptureCount, Smi::FromInt(to));
163 }
164
165 static void SetLastSubject(FixedArray* array, String* to) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000166 array->set(kLastSubject, to);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000167 }
168
169 static void SetLastInput(FixedArray* array, String* to) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000170 array->set(kLastInput, to);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000171 }
172
173 static void SetCapture(FixedArray* array, int index, int to) {
174 array->set(index + kFirstCapture, Smi::FromInt(to));
175 }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000176
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000177 static int GetLastCaptureCount(FixedArray* array) {
178 return Smi::cast(array->get(kLastCaptureCount))->value();
179 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000180
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000181 // For acting on the JSRegExp data FixedArray.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000182 static int IrregexpMaxRegisterCount(FixedArray* re);
183 static void SetIrregexpMaxRegisterCount(FixedArray* re, int value);
184 static int IrregexpNumberOfCaptures(FixedArray* re);
185 static int IrregexpNumberOfRegisters(FixedArray* re);
186 static ByteArray* IrregexpByteCode(FixedArray* re, bool is_ascii);
187 static Code* IrregexpNativeCode(FixedArray* re, bool is_ascii);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000188
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000189 // Limit the space regexps take up on the heap. In order to limit this we
190 // would like to keep track of the amount of regexp code on the heap. This
191 // is not tracked, however. As a conservative approximation we track the
192 // total regexp code compiled including code that has subsequently been freed
193 // and the total executable memory at any point.
194 static const int kRegExpExecutableMemoryLimit = 16 * MB;
195 static const int kRegWxpCompiledLimit = 1 * MB;
196
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000197 private:
198 static String* last_ascii_string_;
199 static String* two_byte_cached_string_;
200
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000201 static bool CompileIrregexp(
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000202 Handle<JSRegExp> re, Handle<String> sample_subject, bool is_ascii);
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000203 static inline bool EnsureCompiledIrregexp(
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000204 Handle<JSRegExp> re, Handle<String> sample_subject, bool is_ascii);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000205
206
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000207 // Set the subject cache. The previous string buffer is not deleted, so the
208 // caller should ensure that it doesn't leak.
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000209 static void SetSubjectCache(String* subject,
210 char* utf8_subject,
211 int uft8_length,
212 int character_position,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000213 int utf8_position);
214
215 // A one element cache of the last utf8_subject string and its length. The
216 // subject JS String object is cached in the heap. We also cache a
217 // translation between position and utf8 position.
218 static char* utf8_subject_cache_;
219 static int utf8_length_cache_;
220 static int utf8_position_;
221 static int character_position_;
222};
223
224
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000225// Represents the location of one element relative to the intersection of
226// two sets. Corresponds to the four areas of a Venn diagram.
227enum ElementInSetsRelation {
228 kInsideNone = 0,
229 kInsideFirst = 1,
230 kInsideSecond = 2,
231 kInsideBoth = 3
232};
233
234
danno@chromium.org1044a4d2012-04-30 12:34:39 +0000235// Represents code units in the range from from_ to to_, both ends are
236// inclusive.
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000237class CharacterRange {
238 public:
239 CharacterRange() : from_(0), to_(0) { }
240 // For compatibility with the CHECK_OK macro
241 CharacterRange(void* null) { ASSERT_EQ(NULL, null); } //NOLINT
242 CharacterRange(uc16 from, uc16 to) : from_(from), to_(to) { }
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000243 static void AddClassEscape(uc16 type, ZoneList<CharacterRange>* ranges,
244 Zone* zone);
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000245 static Vector<const int> GetWordBounds();
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000246 static inline CharacterRange Singleton(uc16 value) {
247 return CharacterRange(value, value);
248 }
249 static inline CharacterRange Range(uc16 from, uc16 to) {
250 ASSERT(from <= to);
251 return CharacterRange(from, to);
252 }
253 static inline CharacterRange Everything() {
254 return CharacterRange(0, 0xFFFF);
255 }
256 bool Contains(uc16 i) { return from_ <= i && i <= to_; }
257 uc16 from() const { return from_; }
258 void set_from(uc16 value) { from_ = value; }
259 uc16 to() const { return to_; }
260 void set_to(uc16 value) { to_ = value; }
261 bool is_valid() { return from_ <= to_; }
ager@chromium.org8bb60582008-12-11 12:02:20 +0000262 bool IsEverything(uc16 max) { return from_ == 0 && to_ >= max; }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000263 bool IsSingleton() { return (from_ == to_); }
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000264 void AddCaseEquivalents(ZoneList<CharacterRange>* ranges, bool is_ascii,
265 Zone* zone);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000266 static void Split(ZoneList<CharacterRange>* base,
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000267 Vector<const int> overlay,
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000268 ZoneList<CharacterRange>** included,
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000269 ZoneList<CharacterRange>** excluded,
270 Zone* zone);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000271 // Whether a range list is in canonical form: Ranges ordered by from value,
272 // and ranges non-overlapping and non-adjacent.
273 static bool IsCanonical(ZoneList<CharacterRange>* ranges);
274 // Convert range list to canonical form. The characters covered by the ranges
275 // will still be the same, but no character is in more than one range, and
276 // adjacent ranges are merged. The resulting list may be shorter than the
277 // original, but cannot be longer.
278 static void Canonicalize(ZoneList<CharacterRange>* ranges);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000279 // Negate the contents of a character range in canonical form.
280 static void Negate(ZoneList<CharacterRange>* src,
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000281 ZoneList<CharacterRange>* dst,
282 Zone* zone);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000283 static const int kStartMarker = (1 << 24);
284 static const int kPayloadMask = (1 << 24) - 1;
285
286 private:
287 uc16 from_;
288 uc16 to_;
289};
290
291
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000292// A set of unsigned integers that behaves especially well on small
293// integers (< 32). May do zone-allocation.
294class OutSet: public ZoneObject {
295 public:
296 OutSet() : first_(0), remaining_(NULL), successors_(NULL) { }
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000297 OutSet* Extend(unsigned value, Zone* zone);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000298 bool Get(unsigned value);
299 static const unsigned kFirstLimit = 32;
300
301 private:
302 // Destructively set a value in this set. In most cases you want
303 // to use Extend instead to ensure that only one instance exists
304 // that contains the same values.
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000305 void Set(unsigned value, Zone* zone);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000306
307 // The successors are a list of sets that contain the same values
308 // as this set and the one more value that is not present in this
309 // set.
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000310 ZoneList<OutSet*>* successors(Zone* zone) { return successors_; }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000311
312 OutSet(uint32_t first, ZoneList<unsigned>* remaining)
313 : first_(first), remaining_(remaining), successors_(NULL) { }
314 uint32_t first_;
315 ZoneList<unsigned>* remaining_;
316 ZoneList<OutSet*>* successors_;
ager@chromium.org32912102009-01-16 10:38:43 +0000317 friend class Trace;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000318};
319
320
321// A mapping from integers, specified as ranges, to a set of integers.
322// Used for mapping character ranges to choices.
323class DispatchTable : public ZoneObject {
324 public:
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000325 explicit DispatchTable(Zone* zone) : tree_(zone) { }
326
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000327 class Entry {
328 public:
329 Entry() : from_(0), to_(0), out_set_(NULL) { }
330 Entry(uc16 from, uc16 to, OutSet* out_set)
331 : from_(from), to_(to), out_set_(out_set) { }
332 uc16 from() { return from_; }
333 uc16 to() { return to_; }
334 void set_to(uc16 value) { to_ = value; }
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000335 void AddValue(int value, Zone* zone) {
336 out_set_ = out_set_->Extend(value, zone);
337 }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000338 OutSet* out_set() { return out_set_; }
339 private:
340 uc16 from_;
341 uc16 to_;
342 OutSet* out_set_;
343 };
344
345 class Config {
346 public:
347 typedef uc16 Key;
348 typedef Entry Value;
349 static const uc16 kNoKey;
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000350 static const Entry NoValue() { return Value(); }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000351 static inline int Compare(uc16 a, uc16 b) {
352 if (a == b)
353 return 0;
354 else if (a < b)
355 return -1;
356 else
357 return 1;
358 }
359 };
360
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000361 void AddRange(CharacterRange range, int value, Zone* zone);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000362 OutSet* Get(uc16 value);
363 void Dump();
364
365 template <typename Callback>
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000366 void ForEach(Callback* callback) {
367 return tree()->ForEach(callback);
368 }
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000369
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000370 private:
371 // There can't be a static empty set since it allocates its
372 // successors in a zone and caches them.
373 OutSet* empty() { return &empty_; }
374 OutSet empty_;
375 ZoneSplayTree<Config>* tree() { return &tree_; }
376 ZoneSplayTree<Config> tree_;
377};
378
379
380#define FOR_EACH_NODE_TYPE(VISIT) \
381 VISIT(End) \
382 VISIT(Action) \
383 VISIT(Choice) \
384 VISIT(BackReference) \
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000385 VISIT(Assertion) \
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000386 VISIT(Text)
387
388
389#define FOR_EACH_REG_EXP_TREE_TYPE(VISIT) \
390 VISIT(Disjunction) \
391 VISIT(Alternative) \
392 VISIT(Assertion) \
393 VISIT(CharacterClass) \
394 VISIT(Atom) \
395 VISIT(Quantifier) \
396 VISIT(Capture) \
397 VISIT(Lookahead) \
398 VISIT(BackReference) \
399 VISIT(Empty) \
400 VISIT(Text)
401
402
403#define FORWARD_DECLARE(Name) class RegExp##Name;
404FOR_EACH_REG_EXP_TREE_TYPE(FORWARD_DECLARE)
405#undef FORWARD_DECLARE
406
407
408class TextElement {
409 public:
410 enum Type {UNINITIALIZED, ATOM, CHAR_CLASS};
411 TextElement() : type(UNINITIALIZED) { }
ager@chromium.org8bb60582008-12-11 12:02:20 +0000412 explicit TextElement(Type t) : type(t), cp_offset(-1) { }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000413 static TextElement Atom(RegExpAtom* atom);
414 static TextElement CharClass(RegExpCharacterClass* char_class);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000415 int length();
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000416 Type type;
417 union {
418 RegExpAtom* u_atom;
419 RegExpCharacterClass* u_char_class;
420 } data;
ager@chromium.org8bb60582008-12-11 12:02:20 +0000421 int cp_offset;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000422};
423
424
ager@chromium.org32912102009-01-16 10:38:43 +0000425class Trace;
ager@chromium.org8bb60582008-12-11 12:02:20 +0000426
427
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000428struct NodeInfo {
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000429 NodeInfo()
430 : being_analyzed(false),
431 been_analyzed(false),
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000432 follows_word_interest(false),
433 follows_newline_interest(false),
434 follows_start_interest(false),
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000435 at_end(false),
danno@chromium.org1044a4d2012-04-30 12:34:39 +0000436 visited(false),
437 replacement_calculated(false) { }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000438
439 // Returns true if the interests and assumptions of this node
440 // matches the given one.
441 bool Matches(NodeInfo* that) {
442 return (at_end == that->at_end) &&
443 (follows_word_interest == that->follows_word_interest) &&
444 (follows_newline_interest == that->follows_newline_interest) &&
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000445 (follows_start_interest == that->follows_start_interest);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000446 }
447
448 // Updates the interests of this node given the interests of the
449 // node preceding it.
450 void AddFromPreceding(NodeInfo* that) {
451 at_end |= that->at_end;
452 follows_word_interest |= that->follows_word_interest;
453 follows_newline_interest |= that->follows_newline_interest;
454 follows_start_interest |= that->follows_start_interest;
455 }
456
ager@chromium.org8bb60582008-12-11 12:02:20 +0000457 bool HasLookbehind() {
458 return follows_word_interest ||
459 follows_newline_interest ||
460 follows_start_interest;
461 }
462
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000463 // Sets the interests of this node to include the interests of the
464 // following node.
465 void AddFromFollowing(NodeInfo* that) {
466 follows_word_interest |= that->follows_word_interest;
467 follows_newline_interest |= that->follows_newline_interest;
468 follows_start_interest |= that->follows_start_interest;
469 }
470
471 void ResetCompilationState() {
472 being_analyzed = false;
473 been_analyzed = false;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000474 }
475
476 bool being_analyzed: 1;
477 bool been_analyzed: 1;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000478
479 // These bits are set of this node has to know what the preceding
480 // character was.
481 bool follows_word_interest: 1;
482 bool follows_newline_interest: 1;
483 bool follows_start_interest: 1;
484
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000485 bool at_end: 1;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000486 bool visited: 1;
danno@chromium.org1044a4d2012-04-30 12:34:39 +0000487 bool replacement_calculated: 1;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000488};
489
490
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000491// Details of a quick mask-compare check that can look ahead in the
492// input stream.
493class QuickCheckDetails {
494 public:
495 QuickCheckDetails()
496 : characters_(0),
497 mask_(0),
iposva@chromium.org245aa852009-02-10 00:49:54 +0000498 value_(0),
499 cannot_match_(false) { }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000500 explicit QuickCheckDetails(int characters)
501 : characters_(characters),
502 mask_(0),
iposva@chromium.org245aa852009-02-10 00:49:54 +0000503 value_(0),
504 cannot_match_(false) { }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000505 bool Rationalize(bool ascii);
506 // Merge in the information from another branch of an alternation.
507 void Merge(QuickCheckDetails* other, int from_index);
508 // Advance the current position by some amount.
509 void Advance(int by, bool ascii);
510 void Clear();
iposva@chromium.org245aa852009-02-10 00:49:54 +0000511 bool cannot_match() { return cannot_match_; }
512 void set_cannot_match() { cannot_match_ = true; }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000513 struct Position {
514 Position() : mask(0), value(0), determines_perfectly(false) { }
515 uc16 mask;
516 uc16 value;
517 bool determines_perfectly;
518 };
519 int characters() { return characters_; }
520 void set_characters(int characters) { characters_ = characters; }
521 Position* positions(int index) {
522 ASSERT(index >= 0);
523 ASSERT(index < characters_);
524 return positions_ + index;
525 }
526 uint32_t mask() { return mask_; }
527 uint32_t value() { return value_; }
528
529 private:
530 // How many characters do we have quick check information from. This is
531 // the same for all branches of a choice node.
532 int characters_;
533 Position positions_[4];
534 // These values are the condensate of the above array after Rationalize().
535 uint32_t mask_;
536 uint32_t value_;
iposva@chromium.org245aa852009-02-10 00:49:54 +0000537 // If set to true, there is no way this quick check can match at all.
538 // E.g., if it requires to be at the start of the input, and isn't.
539 bool cannot_match_;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000540};
541
542
danno@chromium.org1044a4d2012-04-30 12:34:39 +0000543extern int kUninitializedRegExpNodePlaceHolder;
544
545
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000546class RegExpNode: public ZoneObject {
547 public:
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000548 explicit RegExpNode(Zone* zone)
549 : replacement_(NULL), trace_count_(0), zone_(zone) {
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000550 bm_info_[0] = bm_info_[1] = NULL;
551 }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000552 virtual ~RegExpNode();
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000553 virtual void Accept(NodeVisitor* visitor) = 0;
554 // Generates a goto to this node or actually generates the code at this point.
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000555 virtual void Emit(RegExpCompiler* compiler, Trace* trace) = 0;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000556 // How many characters must this node consume at a minimum in order to
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000557 // succeed. If we have found at least 'still_to_find' characters that
558 // must be consumed there is no need to ask any following nodes whether
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000559 // they are sure to eat any more characters. The not_at_start argument is
560 // used to indicate that we know we are not at the start of the input. In
561 // this case anchored branches will always fail and can be ignored when
562 // determining how many characters are consumed on success.
563 virtual int EatsAtLeast(int still_to_find,
564 int recursion_depth,
565 bool not_at_start) = 0;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000566 // Emits some quick code that checks whether the preloaded characters match.
567 // Falls through on certain failure, jumps to the label on possible success.
568 // If the node cannot make a quick check it does nothing and returns false.
569 bool EmitQuickCheck(RegExpCompiler* compiler,
ager@chromium.org32912102009-01-16 10:38:43 +0000570 Trace* trace,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000571 bool preload_has_checked_bounds,
572 Label* on_possible_success,
573 QuickCheckDetails* details_return,
574 bool fall_through_on_failure);
575 // For a given number of characters this returns a mask and a value. The
576 // next n characters are anded with the mask and compared with the value.
577 // A comparison failure indicates the node cannot match the next n characters.
578 // A comparison success indicates the node may match.
579 virtual void GetQuickCheckDetails(QuickCheckDetails* details,
580 RegExpCompiler* compiler,
iposva@chromium.org245aa852009-02-10 00:49:54 +0000581 int characters_filled_in,
582 bool not_at_start) = 0;
ager@chromium.org8bb60582008-12-11 12:02:20 +0000583 static const int kNodeIsTooComplexForGreedyLoops = -1;
584 virtual int GreedyLoopTextLength() { return kNodeIsTooComplexForGreedyLoops; }
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000585 // Only returns the successor for a text node of length 1 that matches any
586 // character and that has no guards on it.
587 virtual RegExpNode* GetSuccessorOfOmnivorousTextNode(
588 RegExpCompiler* compiler) {
589 return NULL;
590 }
591
592 // Collects information on the possible code units (mod 128) that can match if
593 // we look forward. This is used for a Boyer-Moore-like string searching
594 // implementation. TODO(erikcorry): This should share more code with
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000595 // EatsAtLeast, GetQuickCheckDetails. The budget argument is used to limit
596 // the number of nodes we are willing to look at in order to create this data.
597 static const int kFillInBMBudget = 200;
verwaest@chromium.org37141392012-05-31 13:27:02 +0000598 virtual void FillInBMInfo(int offset,
599 int recursion_depth,
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000600 int budget,
verwaest@chromium.org37141392012-05-31 13:27:02 +0000601 BoyerMooreLookahead* bm,
602 bool not_at_start) {
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000603 UNREACHABLE();
604 }
danno@chromium.org1044a4d2012-04-30 12:34:39 +0000605
606 // If we know that the input is ASCII then there are some nodes that can
607 // never match. This method returns a node that can be substituted for
608 // itself, or NULL if the node can never match.
609 virtual RegExpNode* FilterASCII(int depth) { return this; }
610 // Helper for FilterASCII.
611 RegExpNode* replacement() {
612 ASSERT(info()->replacement_calculated);
613 return replacement_;
614 }
615 RegExpNode* set_replacement(RegExpNode* replacement) {
616 info()->replacement_calculated = true;
617 replacement_ = replacement;
618 return replacement; // For convenience.
619 }
620
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000621 // We want to avoid recalculating the lookahead info, so we store it on the
622 // node. Only info that is for this node is stored. We can tell that the
623 // info is for this node when offset == 0, so the information is calculated
624 // relative to this node.
625 void SaveBMInfo(BoyerMooreLookahead* bm, bool not_at_start, int offset) {
626 if (offset == 0) set_bm_info(not_at_start, bm);
627 }
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000628
ager@chromium.org8bb60582008-12-11 12:02:20 +0000629 Label* label() { return &label_; }
ulan@chromium.org2efb9002012-01-19 15:36:35 +0000630 // 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 +0000631 // start of the trace) then it cannot be reused. This variable sets a limit
632 // on how often we allow that to happen before we insist on starting a new
633 // trace and generating generic code for a node that can be reused by flushing
634 // the deferred actions in the current trace and generating a goto.
635 static const int kMaxCopiesCodeGenerated = 10;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000636
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000637 NodeInfo* info() { return &info_; }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000638
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000639 BoyerMooreLookahead* bm_info(bool not_at_start) {
640 return bm_info_[not_at_start ? 1 : 0];
641 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000642
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000643 Zone* zone() const { return zone_; }
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000644
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000645 protected:
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000646 enum LimitResult { DONE, CONTINUE };
danno@chromium.org1044a4d2012-04-30 12:34:39 +0000647 RegExpNode* replacement_;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000648
ager@chromium.org32912102009-01-16 10:38:43 +0000649 LimitResult LimitVersions(RegExpCompiler* compiler, Trace* trace);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000650
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000651 void set_bm_info(bool not_at_start, BoyerMooreLookahead* bm) {
652 bm_info_[not_at_start ? 1 : 0] = bm;
653 }
654
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000655 private:
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000656 static const int kFirstCharBudget = 10;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000657 Label label_;
658 NodeInfo info_;
ager@chromium.org32912102009-01-16 10:38:43 +0000659 // This variable keeps track of how many times code has been generated for
660 // this node (in different traces). We don't keep track of where the
661 // generated code is located unless the code is generated at the start of
662 // a trace, in which case it is generic and can be reused by flushing the
663 // deferred operations in the current trace and generating a goto.
664 int trace_count_;
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000665 BoyerMooreLookahead* bm_info_[2];
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000666
667 Zone* zone_;
ager@chromium.org32912102009-01-16 10:38:43 +0000668};
669
670
671// A simple closed interval.
672class Interval {
673 public:
674 Interval() : from_(kNone), to_(kNone) { }
675 Interval(int from, int to) : from_(from), to_(to) { }
676 Interval Union(Interval that) {
677 if (that.from_ == kNone)
678 return *this;
679 else if (from_ == kNone)
680 return that;
681 else
682 return Interval(Min(from_, that.from_), Max(to_, that.to_));
683 }
684 bool Contains(int value) {
685 return (from_ <= value) && (value <= to_);
686 }
687 bool is_empty() { return from_ == kNone; }
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000688 int from() const { return from_; }
689 int to() const { return to_; }
ager@chromium.org32912102009-01-16 10:38:43 +0000690 static Interval Empty() { return Interval(); }
691 static const int kNone = -1;
692 private:
693 int from_;
694 int to_;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000695};
696
697
698class SeqRegExpNode: public RegExpNode {
699 public:
700 explicit SeqRegExpNode(RegExpNode* on_success)
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000701 : RegExpNode(on_success->zone()), on_success_(on_success) { }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000702 RegExpNode* on_success() { return on_success_; }
703 void set_on_success(RegExpNode* node) { on_success_ = node; }
danno@chromium.org1044a4d2012-04-30 12:34:39 +0000704 virtual RegExpNode* FilterASCII(int depth);
verwaest@chromium.org37141392012-05-31 13:27:02 +0000705 virtual void FillInBMInfo(int offset,
706 int recursion_depth,
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000707 int budget,
verwaest@chromium.org37141392012-05-31 13:27:02 +0000708 BoyerMooreLookahead* bm,
709 bool not_at_start) {
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000710 on_success_->FillInBMInfo(
711 offset, recursion_depth + 1, budget - 1, bm, not_at_start);
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000712 if (offset == 0) set_bm_info(not_at_start, bm);
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000713 }
danno@chromium.org1044a4d2012-04-30 12:34:39 +0000714
715 protected:
716 RegExpNode* FilterSuccessor(int depth);
717
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000718 private:
719 RegExpNode* on_success_;
720};
721
722
723class ActionNode: public SeqRegExpNode {
724 public:
725 enum Type {
ager@chromium.org8bb60582008-12-11 12:02:20 +0000726 SET_REGISTER,
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000727 INCREMENT_REGISTER,
728 STORE_POSITION,
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000729 BEGIN_SUBMATCH,
ager@chromium.org32912102009-01-16 10:38:43 +0000730 POSITIVE_SUBMATCH_SUCCESS,
731 EMPTY_MATCH_CHECK,
732 CLEAR_CAPTURES
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000733 };
ager@chromium.org8bb60582008-12-11 12:02:20 +0000734 static ActionNode* SetRegister(int reg, int val, RegExpNode* on_success);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000735 static ActionNode* IncrementRegister(int reg, RegExpNode* on_success);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000736 static ActionNode* StorePosition(int reg,
737 bool is_capture,
738 RegExpNode* on_success);
ager@chromium.org32912102009-01-16 10:38:43 +0000739 static ActionNode* ClearCaptures(Interval range, RegExpNode* on_success);
740 static ActionNode* BeginSubmatch(int stack_pointer_reg,
741 int position_reg,
742 RegExpNode* on_success);
743 static ActionNode* PositiveSubmatchSuccess(int stack_pointer_reg,
744 int restore_reg,
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000745 int clear_capture_count,
746 int clear_capture_from,
ager@chromium.org32912102009-01-16 10:38:43 +0000747 RegExpNode* on_success);
748 static ActionNode* EmptyMatchCheck(int start_register,
749 int repetition_register,
750 int repetition_limit,
751 RegExpNode* on_success);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000752 virtual void Accept(NodeVisitor* visitor);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000753 virtual void Emit(RegExpCompiler* compiler, Trace* trace);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000754 virtual int EatsAtLeast(int still_to_find,
755 int recursion_depth,
756 bool not_at_start);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000757 virtual void GetQuickCheckDetails(QuickCheckDetails* details,
758 RegExpCompiler* compiler,
iposva@chromium.org245aa852009-02-10 00:49:54 +0000759 int filled_in,
760 bool not_at_start) {
761 return on_success()->GetQuickCheckDetails(
762 details, compiler, filled_in, not_at_start);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000763 }
verwaest@chromium.org37141392012-05-31 13:27:02 +0000764 virtual void FillInBMInfo(int offset,
765 int recursion_depth,
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000766 int budget,
verwaest@chromium.org37141392012-05-31 13:27:02 +0000767 BoyerMooreLookahead* bm,
768 bool not_at_start);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000769 Type type() { return type_; }
770 // TODO(erikcorry): We should allow some action nodes in greedy loops.
771 virtual int GreedyLoopTextLength() { return kNodeIsTooComplexForGreedyLoops; }
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000772
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000773 private:
774 union {
775 struct {
776 int reg;
777 int value;
778 } u_store_register;
779 struct {
780 int reg;
781 } u_increment_register;
782 struct {
783 int reg;
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000784 bool is_capture;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000785 } u_position_register;
786 struct {
787 int stack_pointer_register;
788 int current_position_register;
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000789 int clear_register_count;
790 int clear_register_from;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000791 } u_submatch;
ager@chromium.org32912102009-01-16 10:38:43 +0000792 struct {
793 int start_register;
794 int repetition_register;
795 int repetition_limit;
796 } u_empty_match_check;
797 struct {
798 int range_from;
799 int range_to;
800 } u_clear_captures;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000801 } data_;
802 ActionNode(Type type, RegExpNode* on_success)
803 : SeqRegExpNode(on_success),
804 type_(type) { }
805 Type type_;
806 friend class DotPrinter;
807};
808
809
810class TextNode: public SeqRegExpNode {
811 public:
812 TextNode(ZoneList<TextElement>* elms,
ager@chromium.org8bb60582008-12-11 12:02:20 +0000813 RegExpNode* on_success)
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000814 : SeqRegExpNode(on_success),
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000815 elms_(elms) { }
816 TextNode(RegExpCharacterClass* that,
ager@chromium.org8bb60582008-12-11 12:02:20 +0000817 RegExpNode* on_success)
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000818 : SeqRegExpNode(on_success),
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000819 elms_(new(zone()) ZoneList<TextElement>(1, zone())) {
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000820 elms_->Add(TextElement::CharClass(that), zone());
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000821 }
822 virtual void Accept(NodeVisitor* visitor);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000823 virtual void Emit(RegExpCompiler* compiler, Trace* trace);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000824 virtual int EatsAtLeast(int still_to_find,
825 int recursion_depth,
826 bool not_at_start);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000827 virtual void GetQuickCheckDetails(QuickCheckDetails* details,
828 RegExpCompiler* compiler,
iposva@chromium.org245aa852009-02-10 00:49:54 +0000829 int characters_filled_in,
830 bool not_at_start);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000831 ZoneList<TextElement>* elements() { return elms_; }
ager@chromium.org38e4c712009-11-11 09:11:58 +0000832 void MakeCaseIndependent(bool is_ascii);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000833 virtual int GreedyLoopTextLength();
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000834 virtual RegExpNode* GetSuccessorOfOmnivorousTextNode(
835 RegExpCompiler* compiler);
verwaest@chromium.org37141392012-05-31 13:27:02 +0000836 virtual void FillInBMInfo(int offset,
837 int recursion_depth,
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000838 int budget,
verwaest@chromium.org37141392012-05-31 13:27:02 +0000839 BoyerMooreLookahead* bm,
840 bool not_at_start);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000841 void CalculateOffsets();
danno@chromium.org1044a4d2012-04-30 12:34:39 +0000842 virtual RegExpNode* FilterASCII(int depth);
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000843
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000844 private:
845 enum TextEmitPassType {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000846 NON_ASCII_MATCH, // Check for characters that can't match.
847 SIMPLE_CHARACTER_MATCH, // Case-dependent single character check.
848 NON_LETTER_CHARACTER_MATCH, // Check characters that have no case equivs.
849 CASE_CHARACTER_MATCH, // Case-independent single character check.
850 CHARACTER_CLASS_MATCH // Character class.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000851 };
ager@chromium.org381abbb2009-02-25 13:23:22 +0000852 static bool SkipPass(int pass, bool ignore_case);
853 static const int kFirstRealPass = SIMPLE_CHARACTER_MATCH;
854 static const int kLastPass = CHARACTER_CLASS_MATCH;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000855 void TextEmitPass(RegExpCompiler* compiler,
856 TextEmitPassType pass,
857 bool preloaded,
ager@chromium.org32912102009-01-16 10:38:43 +0000858 Trace* trace,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000859 bool first_element_checked,
860 int* checked_up_to);
861 int Length();
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000862 ZoneList<TextElement>* elms_;
863};
864
865
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000866class AssertionNode: public SeqRegExpNode {
867 public:
868 enum AssertionNodeType {
869 AT_END,
870 AT_START,
871 AT_BOUNDARY,
872 AT_NON_BOUNDARY,
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000873 AFTER_NEWLINE
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000874 };
875 static AssertionNode* AtEnd(RegExpNode* on_success) {
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000876 return new(on_success->zone()) AssertionNode(AT_END, on_success);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000877 }
878 static AssertionNode* AtStart(RegExpNode* on_success) {
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000879 return new(on_success->zone()) AssertionNode(AT_START, on_success);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000880 }
881 static AssertionNode* AtBoundary(RegExpNode* on_success) {
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000882 return new(on_success->zone()) AssertionNode(AT_BOUNDARY, on_success);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000883 }
884 static AssertionNode* AtNonBoundary(RegExpNode* on_success) {
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000885 return new(on_success->zone()) AssertionNode(AT_NON_BOUNDARY, on_success);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000886 }
887 static AssertionNode* AfterNewline(RegExpNode* on_success) {
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000888 return new(on_success->zone()) AssertionNode(AFTER_NEWLINE, on_success);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000889 }
890 virtual void Accept(NodeVisitor* visitor);
891 virtual void Emit(RegExpCompiler* compiler, Trace* trace);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000892 virtual int EatsAtLeast(int still_to_find,
893 int recursion_depth,
894 bool not_at_start);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000895 virtual void GetQuickCheckDetails(QuickCheckDetails* details,
896 RegExpCompiler* compiler,
iposva@chromium.org245aa852009-02-10 00:49:54 +0000897 int filled_in,
898 bool not_at_start);
verwaest@chromium.org37141392012-05-31 13:27:02 +0000899 virtual void FillInBMInfo(int offset,
900 int recursion_depth,
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000901 int budget,
verwaest@chromium.org37141392012-05-31 13:27:02 +0000902 BoyerMooreLookahead* bm,
903 bool not_at_start);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000904 AssertionNodeType type() { return type_; }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000905 void set_type(AssertionNodeType type) { type_ = type; }
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000906
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000907 private:
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000908 void EmitBoundaryCheck(RegExpCompiler* compiler, Trace* trace);
909 enum IfPrevious { kIsNonWord, kIsWord };
910 void BacktrackIfPrevious(RegExpCompiler* compiler,
911 Trace* trace,
912 IfPrevious backtrack_if_previous);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000913 AssertionNode(AssertionNodeType t, RegExpNode* on_success)
914 : SeqRegExpNode(on_success), type_(t) { }
915 AssertionNodeType type_;
916};
917
918
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000919class BackReferenceNode: public SeqRegExpNode {
920 public:
921 BackReferenceNode(int start_reg,
922 int end_reg,
ager@chromium.org8bb60582008-12-11 12:02:20 +0000923 RegExpNode* on_success)
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000924 : SeqRegExpNode(on_success),
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000925 start_reg_(start_reg),
926 end_reg_(end_reg) { }
927 virtual void Accept(NodeVisitor* visitor);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000928 int start_register() { return start_reg_; }
929 int end_register() { return end_reg_; }
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000930 virtual void Emit(RegExpCompiler* compiler, Trace* trace);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000931 virtual int EatsAtLeast(int still_to_find,
932 int recursion_depth,
933 bool not_at_start);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000934 virtual void GetQuickCheckDetails(QuickCheckDetails* details,
935 RegExpCompiler* compiler,
iposva@chromium.org245aa852009-02-10 00:49:54 +0000936 int characters_filled_in,
937 bool not_at_start) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000938 return;
939 }
verwaest@chromium.org37141392012-05-31 13:27:02 +0000940 virtual void FillInBMInfo(int offset,
941 int recursion_depth,
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000942 int budget,
verwaest@chromium.org37141392012-05-31 13:27:02 +0000943 BoyerMooreLookahead* bm,
944 bool not_at_start);
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000945
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000946 private:
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000947 int start_reg_;
948 int end_reg_;
949};
950
951
952class EndNode: public RegExpNode {
953 public:
ager@chromium.org8bb60582008-12-11 12:02:20 +0000954 enum Action { ACCEPT, BACKTRACK, NEGATIVE_SUBMATCH_SUCCESS };
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000955 explicit EndNode(Action action, Zone* zone)
956 : RegExpNode(zone), action_(action) { }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000957 virtual void Accept(NodeVisitor* visitor);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000958 virtual void Emit(RegExpCompiler* compiler, Trace* trace);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000959 virtual int EatsAtLeast(int still_to_find,
960 int recursion_depth,
961 bool not_at_start) { return 0; }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000962 virtual void GetQuickCheckDetails(QuickCheckDetails* details,
963 RegExpCompiler* compiler,
iposva@chromium.org245aa852009-02-10 00:49:54 +0000964 int characters_filled_in,
965 bool not_at_start) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000966 // Returning 0 from EatsAtLeast should ensure we never get here.
967 UNREACHABLE();
968 }
verwaest@chromium.org37141392012-05-31 13:27:02 +0000969 virtual void FillInBMInfo(int offset,
970 int recursion_depth,
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000971 int budget,
verwaest@chromium.org37141392012-05-31 13:27:02 +0000972 BoyerMooreLookahead* bm,
973 bool not_at_start) {
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000974 // Returning 0 from EatsAtLeast should ensure we never get here.
975 UNREACHABLE();
976 }
danno@chromium.org1044a4d2012-04-30 12:34:39 +0000977
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000978 private:
979 Action action_;
980};
981
982
ager@chromium.org8bb60582008-12-11 12:02:20 +0000983class NegativeSubmatchSuccess: public EndNode {
984 public:
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000985 NegativeSubmatchSuccess(int stack_pointer_reg,
986 int position_reg,
987 int clear_capture_count,
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000988 int clear_capture_start,
989 Zone* zone)
990 : EndNode(NEGATIVE_SUBMATCH_SUCCESS, zone),
ager@chromium.org8bb60582008-12-11 12:02:20 +0000991 stack_pointer_register_(stack_pointer_reg),
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000992 current_position_register_(position_reg),
993 clear_capture_count_(clear_capture_count),
994 clear_capture_start_(clear_capture_start) { }
995 virtual void Emit(RegExpCompiler* compiler, Trace* trace);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000996
997 private:
998 int stack_pointer_register_;
999 int current_position_register_;
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001000 int clear_capture_count_;
1001 int clear_capture_start_;
ager@chromium.org8bb60582008-12-11 12:02:20 +00001002};
1003
1004
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001005class Guard: public ZoneObject {
1006 public:
1007 enum Relation { LT, GEQ };
1008 Guard(int reg, Relation op, int value)
1009 : reg_(reg),
1010 op_(op),
1011 value_(value) { }
1012 int reg() { return reg_; }
1013 Relation op() { return op_; }
1014 int value() { return value_; }
1015
1016 private:
1017 int reg_;
1018 Relation op_;
1019 int value_;
1020};
1021
1022
1023class GuardedAlternative {
1024 public:
1025 explicit GuardedAlternative(RegExpNode* node) : node_(node), guards_(NULL) { }
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001026 void AddGuard(Guard* guard, Zone* zone);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001027 RegExpNode* node() { return node_; }
1028 void set_node(RegExpNode* node) { node_ = node; }
1029 ZoneList<Guard*>* guards() { return guards_; }
1030
1031 private:
1032 RegExpNode* node_;
1033 ZoneList<Guard*>* guards_;
1034};
1035
1036
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001037class AlternativeGeneration;
1038
1039
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001040class ChoiceNode: public RegExpNode {
1041 public:
rossberg@chromium.org400388e2012-06-06 09:29:22 +00001042 explicit ChoiceNode(int expected_size, Zone* zone)
1043 : RegExpNode(zone),
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001044 alternatives_(new(zone)
1045 ZoneList<GuardedAlternative>(expected_size, zone)),
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001046 table_(NULL),
iposva@chromium.org245aa852009-02-10 00:49:54 +00001047 not_at_start_(false),
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001048 being_calculated_(false) { }
1049 virtual void Accept(NodeVisitor* visitor);
rossberg@chromium.org400388e2012-06-06 09:29:22 +00001050 void AddAlternative(GuardedAlternative node) {
1051 alternatives()->Add(node, zone());
1052 }
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001053 ZoneList<GuardedAlternative>* alternatives() { return alternatives_; }
1054 DispatchTable* GetTable(bool ignore_case);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001055 virtual void Emit(RegExpCompiler* compiler, Trace* trace);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001056 virtual int EatsAtLeast(int still_to_find,
1057 int recursion_depth,
1058 bool not_at_start);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001059 int EatsAtLeastHelper(int still_to_find,
1060 int recursion_depth,
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001061 RegExpNode* ignore_this_node,
1062 bool not_at_start);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001063 virtual void GetQuickCheckDetails(QuickCheckDetails* details,
1064 RegExpCompiler* compiler,
iposva@chromium.org245aa852009-02-10 00:49:54 +00001065 int characters_filled_in,
1066 bool not_at_start);
verwaest@chromium.org37141392012-05-31 13:27:02 +00001067 virtual void FillInBMInfo(int offset,
1068 int recursion_depth,
rossberg@chromium.org400388e2012-06-06 09:29:22 +00001069 int budget,
verwaest@chromium.org37141392012-05-31 13:27:02 +00001070 BoyerMooreLookahead* bm,
1071 bool not_at_start);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001072
1073 bool being_calculated() { return being_calculated_; }
iposva@chromium.org245aa852009-02-10 00:49:54 +00001074 bool not_at_start() { return not_at_start_; }
1075 void set_not_at_start() { not_at_start_ = true; }
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001076 void set_being_calculated(bool b) { being_calculated_ = b; }
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001077 virtual bool try_to_emit_quick_check_for_alternative(int i) { return true; }
danno@chromium.org1044a4d2012-04-30 12:34:39 +00001078 virtual RegExpNode* FilterASCII(int depth);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001079
ager@chromium.org8bb60582008-12-11 12:02:20 +00001080 protected:
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001081 int GreedyLoopTextLengthForAlternative(GuardedAlternative* alternative);
ager@chromium.org8bb60582008-12-11 12:02:20 +00001082 ZoneList<GuardedAlternative>* alternatives_;
1083
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001084 private:
1085 friend class DispatchTableConstructor;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001086 friend class Analysis;
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001087 void GenerateGuard(RegExpMacroAssembler* macro_assembler,
ager@chromium.org5ec48922009-05-05 07:25:34 +00001088 Guard* guard,
ager@chromium.org32912102009-01-16 10:38:43 +00001089 Trace* trace);
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00001090 int CalculatePreloadCharacters(RegExpCompiler* compiler, int eats_at_least);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001091 void EmitOutOfLineContinuation(RegExpCompiler* compiler,
ager@chromium.org32912102009-01-16 10:38:43 +00001092 Trace* trace,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001093 GuardedAlternative alternative,
1094 AlternativeGeneration* alt_gen,
1095 int preload_characters,
1096 bool next_expects_preload);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001097 DispatchTable* table_;
iposva@chromium.org245aa852009-02-10 00:49:54 +00001098 // If true, this node is never checked at the start of the input.
1099 // Allows a new trace to start with at_start() set to false.
1100 bool not_at_start_;
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001101 bool being_calculated_;
1102};
1103
1104
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001105class NegativeLookaheadChoiceNode: public ChoiceNode {
1106 public:
1107 explicit NegativeLookaheadChoiceNode(GuardedAlternative this_must_fail,
rossberg@chromium.org400388e2012-06-06 09:29:22 +00001108 GuardedAlternative then_do_this,
1109 Zone* zone)
1110 : ChoiceNode(2, zone) {
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001111 AddAlternative(this_must_fail);
1112 AddAlternative(then_do_this);
1113 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001114 virtual int EatsAtLeast(int still_to_find,
1115 int recursion_depth,
1116 bool not_at_start);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001117 virtual void GetQuickCheckDetails(QuickCheckDetails* details,
1118 RegExpCompiler* compiler,
iposva@chromium.org245aa852009-02-10 00:49:54 +00001119 int characters_filled_in,
1120 bool not_at_start);
verwaest@chromium.org37141392012-05-31 13:27:02 +00001121 virtual void FillInBMInfo(int offset,
1122 int recursion_depth,
rossberg@chromium.org400388e2012-06-06 09:29:22 +00001123 int budget,
verwaest@chromium.org37141392012-05-31 13:27:02 +00001124 BoyerMooreLookahead* bm,
1125 bool not_at_start) {
1126 alternatives_->at(1).node()->FillInBMInfo(
rossberg@chromium.org400388e2012-06-06 09:29:22 +00001127 offset, recursion_depth + 1, budget - 1, bm, not_at_start);
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00001128 if (offset == 0) set_bm_info(not_at_start, bm);
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00001129 }
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001130 // For a negative lookahead we don't emit the quick check for the
1131 // alternative that is expected to fail. This is because quick check code
1132 // starts by loading enough characters for the alternative that takes fewest
1133 // characters, but on a negative lookahead the negative branch did not take
1134 // part in that calculation (EatsAtLeast) so the assumptions don't hold.
1135 virtual bool try_to_emit_quick_check_for_alternative(int i) { return i != 0; }
danno@chromium.org1044a4d2012-04-30 12:34:39 +00001136 virtual RegExpNode* FilterASCII(int depth);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001137};
1138
1139
ager@chromium.org8bb60582008-12-11 12:02:20 +00001140class LoopChoiceNode: public ChoiceNode {
1141 public:
rossberg@chromium.org400388e2012-06-06 09:29:22 +00001142 explicit LoopChoiceNode(bool body_can_be_zero_length, Zone* zone)
1143 : ChoiceNode(2, zone),
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001144 loop_node_(NULL),
1145 continue_node_(NULL),
1146 body_can_be_zero_length_(body_can_be_zero_length) { }
1147 void AddLoopAlternative(GuardedAlternative alt);
1148 void AddContinueAlternative(GuardedAlternative alt);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001149 virtual void Emit(RegExpCompiler* compiler, Trace* trace);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001150 virtual int EatsAtLeast(int still_to_find,
1151 int recursion_depth,
1152 bool not_at_start);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001153 virtual void GetQuickCheckDetails(QuickCheckDetails* details,
1154 RegExpCompiler* compiler,
iposva@chromium.org245aa852009-02-10 00:49:54 +00001155 int characters_filled_in,
1156 bool not_at_start);
verwaest@chromium.org37141392012-05-31 13:27:02 +00001157 virtual void FillInBMInfo(int offset,
1158 int recursion_depth,
rossberg@chromium.org400388e2012-06-06 09:29:22 +00001159 int budget,
verwaest@chromium.org37141392012-05-31 13:27:02 +00001160 BoyerMooreLookahead* bm,
1161 bool not_at_start);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001162 RegExpNode* loop_node() { return loop_node_; }
1163 RegExpNode* continue_node() { return continue_node_; }
1164 bool body_can_be_zero_length() { return body_can_be_zero_length_; }
1165 virtual void Accept(NodeVisitor* visitor);
danno@chromium.org1044a4d2012-04-30 12:34:39 +00001166 virtual RegExpNode* FilterASCII(int depth);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001167
1168 private:
1169 // AddAlternative is made private for loop nodes because alternatives
1170 // should not be added freely, we need to keep track of which node
1171 // goes back to the node itself.
1172 void AddAlternative(GuardedAlternative node) {
1173 ChoiceNode::AddAlternative(node);
1174 }
1175
1176 RegExpNode* loop_node_;
1177 RegExpNode* continue_node_;
1178 bool body_can_be_zero_length_;
ager@chromium.org8bb60582008-12-11 12:02:20 +00001179};
1180
1181
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00001182// Improve the speed that we scan for an initial point where a non-anchored
1183// regexp can match by using a Boyer-Moore-like table. This is done by
1184// identifying non-greedy non-capturing loops in the nodes that eat any
1185// character one at a time. For example in the middle of the regexp
1186// /foo[\s\S]*?bar/ we find such a loop. There is also such a loop implicitly
1187// inserted at the start of any non-anchored regexp.
1188//
1189// When we have found such a loop we look ahead in the nodes to find the set of
1190// characters that can come at given distances. For example for the regexp
1191// /.?foo/ we know that there are at least 3 characters ahead of us, and the
1192// sets of characters that can occur are [any, [f, o], [o]]. We find a range in
1193// the lookahead info where the set of characters is reasonably constrained. In
1194// our example this is from index 1 to 2 (0 is not constrained). We can now
1195// look 3 characters ahead and if we don't find one of [f, o] (the union of
1196// [f, o] and [o]) then we can skip forwards by the range size (in this case 2).
1197//
1198// For Unicode input strings we do the same, but modulo 128.
1199//
1200// We also look at the first string fed to the regexp and use that to get a hint
1201// of the character frequencies in the inputs. This affects the assessment of
1202// whether the set of characters is 'reasonably constrained'.
1203//
1204// We also have another lookahead mechanism (called quick check in the code),
1205// which uses a wide load of multiple characters followed by a mask and compare
1206// to determine whether a match is possible at this point.
1207enum ContainedInLattice {
1208 kNotYet = 0,
1209 kLatticeIn = 1,
1210 kLatticeOut = 2,
1211 kLatticeUnknown = 3 // Can also mean both in and out.
1212};
1213
1214
1215inline ContainedInLattice Combine(ContainedInLattice a, ContainedInLattice b) {
1216 return static_cast<ContainedInLattice>(a | b);
1217}
1218
1219
1220ContainedInLattice AddRange(ContainedInLattice a,
1221 const int* ranges,
1222 int ranges_size,
1223 Interval new_range);
1224
1225
1226class BoyerMoorePositionInfo : public ZoneObject {
1227 public:
rossberg@chromium.org400388e2012-06-06 09:29:22 +00001228 explicit BoyerMoorePositionInfo(Zone* zone)
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001229 : map_(new(zone) ZoneList<bool>(kMapSize, zone)),
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00001230 map_count_(0),
1231 w_(kNotYet),
1232 s_(kNotYet),
1233 d_(kNotYet),
1234 surrogate_(kNotYet) {
1235 for (int i = 0; i < kMapSize; i++) {
rossberg@chromium.org400388e2012-06-06 09:29:22 +00001236 map_->Add(false, zone);
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00001237 }
1238 }
1239
1240 bool& at(int i) { return map_->at(i); }
1241
1242 static const int kMapSize = 128;
1243 static const int kMask = kMapSize - 1;
1244
1245 int map_count() const { return map_count_; }
1246
1247 void Set(int character);
1248 void SetInterval(const Interval& interval);
1249 void SetAll();
1250 bool is_non_word() { return w_ == kLatticeOut; }
1251 bool is_word() { return w_ == kLatticeIn; }
1252
1253 private:
1254 ZoneList<bool>* map_;
1255 int map_count_; // Number of set bits in the map.
1256 ContainedInLattice w_; // The \w character class.
1257 ContainedInLattice s_; // The \s character class.
1258 ContainedInLattice d_; // The \d character class.
1259 ContainedInLattice surrogate_; // Surrogate UTF-16 code units.
1260};
1261
1262
1263class BoyerMooreLookahead : public ZoneObject {
1264 public:
rossberg@chromium.org400388e2012-06-06 09:29:22 +00001265 BoyerMooreLookahead(int length, RegExpCompiler* compiler, Zone* zone);
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00001266
1267 int length() { return length_; }
1268 int max_char() { return max_char_; }
1269 RegExpCompiler* compiler() { return compiler_; }
1270
1271 int Count(int map_number) {
1272 return bitmaps_->at(map_number)->map_count();
1273 }
1274
1275 BoyerMoorePositionInfo* at(int i) { return bitmaps_->at(i); }
1276
1277 void Set(int map_number, int character) {
1278 if (character > max_char_) return;
1279 BoyerMoorePositionInfo* info = bitmaps_->at(map_number);
1280 info->Set(character);
1281 }
1282
1283 void SetInterval(int map_number, const Interval& interval) {
1284 if (interval.from() > max_char_) return;
1285 BoyerMoorePositionInfo* info = bitmaps_->at(map_number);
1286 if (interval.to() > max_char_) {
1287 info->SetInterval(Interval(interval.from(), max_char_));
1288 } else {
1289 info->SetInterval(interval);
1290 }
1291 }
1292
1293 void SetAll(int map_number) {
1294 bitmaps_->at(map_number)->SetAll();
1295 }
1296
1297 void SetRest(int from_map) {
1298 for (int i = from_map; i < length_; i++) SetAll(i);
1299 }
1300 bool EmitSkipInstructions(RegExpMacroAssembler* masm);
1301
1302 private:
1303 // This is the value obtained by EatsAtLeast. If we do not have at least this
1304 // many characters left in the sample string then the match is bound to fail.
1305 // Therefore it is OK to read a character this far ahead of the current match
1306 // point.
1307 int length_;
1308 RegExpCompiler* compiler_;
1309 // 0x7f for ASCII, 0xffff for UTF-16.
1310 int max_char_;
1311 ZoneList<BoyerMoorePositionInfo*>* bitmaps_;
1312
1313 int GetSkipTable(int min_lookahead,
1314 int max_lookahead,
1315 Handle<ByteArray> boolean_skip_table);
1316 bool FindWorthwhileInterval(int* from, int* to);
1317 int FindBestInterval(
1318 int max_number_of_chars, int old_biggest_points, int* from, int* to);
1319};
1320
1321
ager@chromium.org8bb60582008-12-11 12:02:20 +00001322// There are many ways to generate code for a node. This class encapsulates
1323// the current way we should be generating. In other words it encapsulates
ager@chromium.org32912102009-01-16 10:38:43 +00001324// the current state of the code generator. The effect of this is that we
1325// generate code for paths that the matcher can take through the regular
1326// expression. A given node in the regexp can be code-generated several times
1327// as it can be part of several traces. For example for the regexp:
1328// /foo(bar|ip)baz/ the code to match baz will be generated twice, once as part
1329// of the foo-bar-baz trace and once as part of the foo-ip-baz trace. The code
1330// to match foo is generated only once (the traces have a common prefix). The
1331// code to store the capture is deferred and generated (twice) after the places
1332// where baz has been matched.
1333class Trace {
ager@chromium.org8bb60582008-12-11 12:02:20 +00001334 public:
iposva@chromium.org245aa852009-02-10 00:49:54 +00001335 // A value for a property that is either known to be true, know to be false,
1336 // or not known.
1337 enum TriBool {
1338 UNKNOWN = -1, FALSE = 0, TRUE = 1
1339 };
1340
ager@chromium.org8bb60582008-12-11 12:02:20 +00001341 class DeferredAction {
1342 public:
1343 DeferredAction(ActionNode::Type type, int reg)
1344 : type_(type), reg_(reg), next_(NULL) { }
1345 DeferredAction* next() { return next_; }
ager@chromium.org32912102009-01-16 10:38:43 +00001346 bool Mentions(int reg);
ager@chromium.org8bb60582008-12-11 12:02:20 +00001347 int reg() { return reg_; }
1348 ActionNode::Type type() { return type_; }
1349 private:
1350 ActionNode::Type type_;
1351 int reg_;
1352 DeferredAction* next_;
ager@chromium.org32912102009-01-16 10:38:43 +00001353 friend class Trace;
ager@chromium.org8bb60582008-12-11 12:02:20 +00001354 };
1355
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001356 class DeferredCapture : public DeferredAction {
ager@chromium.org8bb60582008-12-11 12:02:20 +00001357 public:
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001358 DeferredCapture(int reg, bool is_capture, Trace* trace)
ager@chromium.org8bb60582008-12-11 12:02:20 +00001359 : DeferredAction(ActionNode::STORE_POSITION, reg),
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001360 cp_offset_(trace->cp_offset()),
1361 is_capture_(is_capture) { }
ager@chromium.org8bb60582008-12-11 12:02:20 +00001362 int cp_offset() { return cp_offset_; }
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001363 bool is_capture() { return is_capture_; }
ager@chromium.org8bb60582008-12-11 12:02:20 +00001364 private:
1365 int cp_offset_;
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001366 bool is_capture_;
ager@chromium.org8bb60582008-12-11 12:02:20 +00001367 void set_cp_offset(int cp_offset) { cp_offset_ = cp_offset; }
1368 };
1369
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001370 class DeferredSetRegister : public DeferredAction {
ager@chromium.org8bb60582008-12-11 12:02:20 +00001371 public:
1372 DeferredSetRegister(int reg, int value)
1373 : DeferredAction(ActionNode::SET_REGISTER, reg),
1374 value_(value) { }
1375 int value() { return value_; }
1376 private:
1377 int value_;
1378 };
1379
ager@chromium.org32912102009-01-16 10:38:43 +00001380 class DeferredClearCaptures : public DeferredAction {
1381 public:
1382 explicit DeferredClearCaptures(Interval range)
1383 : DeferredAction(ActionNode::CLEAR_CAPTURES, -1),
1384 range_(range) { }
1385 Interval range() { return range_; }
1386 private:
1387 Interval range_;
1388 };
1389
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001390 class DeferredIncrementRegister : public DeferredAction {
ager@chromium.org8bb60582008-12-11 12:02:20 +00001391 public:
1392 explicit DeferredIncrementRegister(int reg)
1393 : DeferredAction(ActionNode::INCREMENT_REGISTER, reg) { }
1394 };
1395
ager@chromium.org32912102009-01-16 10:38:43 +00001396 Trace()
ager@chromium.org8bb60582008-12-11 12:02:20 +00001397 : cp_offset_(0),
1398 actions_(NULL),
1399 backtrack_(NULL),
1400 stop_node_(NULL),
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001401 loop_label_(NULL),
1402 characters_preloaded_(0),
iposva@chromium.org245aa852009-02-10 00:49:54 +00001403 bound_checked_up_to_(0),
ager@chromium.org381abbb2009-02-25 13:23:22 +00001404 flush_budget_(100),
iposva@chromium.org245aa852009-02-10 00:49:54 +00001405 at_start_(UNKNOWN) { }
1406
ager@chromium.org32912102009-01-16 10:38:43 +00001407 // End the trace. This involves flushing the deferred actions in the trace
1408 // and pushing a backtrack location onto the backtrack stack. Once this is
1409 // done we can start a new trace or go to one that has already been
1410 // generated.
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001411 void Flush(RegExpCompiler* compiler, RegExpNode* successor);
ager@chromium.org8bb60582008-12-11 12:02:20 +00001412 int cp_offset() { return cp_offset_; }
1413 DeferredAction* actions() { return actions_; }
ager@chromium.org32912102009-01-16 10:38:43 +00001414 // A trivial trace is one that has no deferred actions or other state that
1415 // affects the assumptions used when generating code. There is no recorded
1416 // backtrack location in a trivial trace, so with a trivial trace we will
1417 // generate code that, on a failure to match, gets the backtrack location
1418 // from the backtrack stack rather than using a direct jump instruction. We
1419 // always start code generation with a trivial trace and non-trivial traces
1420 // are created as we emit code for nodes or add to the list of deferred
1421 // actions in the trace. The location of the code generated for a node using
1422 // a trivial trace is recorded in a label in the node so that gotos can be
1423 // generated to that code.
ager@chromium.org8bb60582008-12-11 12:02:20 +00001424 bool is_trivial() {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001425 return backtrack_ == NULL &&
1426 actions_ == NULL &&
1427 cp_offset_ == 0 &&
1428 characters_preloaded_ == 0 &&
1429 bound_checked_up_to_ == 0 &&
iposva@chromium.org245aa852009-02-10 00:49:54 +00001430 quick_check_performed_.characters() == 0 &&
1431 at_start_ == UNKNOWN;
ager@chromium.org8bb60582008-12-11 12:02:20 +00001432 }
iposva@chromium.org245aa852009-02-10 00:49:54 +00001433 TriBool at_start() { return at_start_; }
1434 void set_at_start(bool at_start) { at_start_ = at_start ? TRUE : FALSE; }
ager@chromium.org8bb60582008-12-11 12:02:20 +00001435 Label* backtrack() { return backtrack_; }
1436 Label* loop_label() { return loop_label_; }
1437 RegExpNode* stop_node() { return stop_node_; }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001438 int characters_preloaded() { return characters_preloaded_; }
1439 int bound_checked_up_to() { return bound_checked_up_to_; }
ager@chromium.org381abbb2009-02-25 13:23:22 +00001440 int flush_budget() { return flush_budget_; }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001441 QuickCheckDetails* quick_check_performed() { return &quick_check_performed_; }
1442 bool mentions_reg(int reg);
ager@chromium.org32912102009-01-16 10:38:43 +00001443 // Returns true if a deferred position store exists to the specified
1444 // register and stores the offset in the out-parameter. Otherwise
1445 // returns false.
1446 bool GetStoredPosition(int reg, int* cp_offset);
1447 // These set methods and AdvanceCurrentPositionInTrace should be used only on
1448 // new traces - the intention is that traces are immutable after creation.
ager@chromium.org8bb60582008-12-11 12:02:20 +00001449 void add_action(DeferredAction* new_action) {
1450 ASSERT(new_action->next_ == NULL);
1451 new_action->next_ = actions_;
1452 actions_ = new_action;
1453 }
ager@chromium.org8bb60582008-12-11 12:02:20 +00001454 void set_backtrack(Label* backtrack) { backtrack_ = backtrack; }
1455 void set_stop_node(RegExpNode* node) { stop_node_ = node; }
1456 void set_loop_label(Label* label) { loop_label_ = label; }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001457 void set_characters_preloaded(int count) { characters_preloaded_ = count; }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001458 void set_bound_checked_up_to(int to) { bound_checked_up_to_ = to; }
ager@chromium.org381abbb2009-02-25 13:23:22 +00001459 void set_flush_budget(int to) { flush_budget_ = to; }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001460 void set_quick_check_performed(QuickCheckDetails* d) {
1461 quick_check_performed_ = *d;
1462 }
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001463 void InvalidateCurrentCharacter();
1464 void AdvanceCurrentPositionInTrace(int by, RegExpCompiler* compiler);
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00001465
ager@chromium.org8bb60582008-12-11 12:02:20 +00001466 private:
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001467 int FindAffectedRegisters(OutSet* affected_registers, Zone* zone);
ager@chromium.org8bb60582008-12-11 12:02:20 +00001468 void PerformDeferredActions(RegExpMacroAssembler* macro,
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001469 int max_register,
1470 OutSet& affected_registers,
1471 OutSet* registers_to_pop,
1472 OutSet* registers_to_clear,
1473 Zone* zone);
ager@chromium.org8bb60582008-12-11 12:02:20 +00001474 void RestoreAffectedRegisters(RegExpMacroAssembler* macro,
1475 int max_register,
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001476 OutSet& registers_to_pop,
1477 OutSet& registers_to_clear);
ager@chromium.org8bb60582008-12-11 12:02:20 +00001478 int cp_offset_;
1479 DeferredAction* actions_;
1480 Label* backtrack_;
1481 RegExpNode* stop_node_;
1482 Label* loop_label_;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001483 int characters_preloaded_;
1484 int bound_checked_up_to_;
1485 QuickCheckDetails quick_check_performed_;
ager@chromium.org381abbb2009-02-25 13:23:22 +00001486 int flush_budget_;
iposva@chromium.org245aa852009-02-10 00:49:54 +00001487 TriBool at_start_;
ager@chromium.org8bb60582008-12-11 12:02:20 +00001488};
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001489
1490
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001491class NodeVisitor {
1492 public:
1493 virtual ~NodeVisitor() { }
1494#define DECLARE_VISIT(Type) \
1495 virtual void Visit##Type(Type##Node* that) = 0;
1496FOR_EACH_NODE_TYPE(DECLARE_VISIT)
1497#undef DECLARE_VISIT
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001498 virtual void VisitLoopChoice(LoopChoiceNode* that) { VisitChoice(that); }
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001499};
1500
1501
1502// Node visitor used to add the start set of the alternatives to the
1503// dispatch table of a choice node.
1504class DispatchTableConstructor: public NodeVisitor {
1505 public:
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001506 DispatchTableConstructor(DispatchTable* table, bool ignore_case,
1507 Zone* zone)
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001508 : table_(table),
1509 choice_index_(-1),
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001510 ignore_case_(ignore_case),
1511 zone_(zone) { }
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001512
1513 void BuildTable(ChoiceNode* node);
1514
1515 void AddRange(CharacterRange range) {
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001516 table()->AddRange(range, choice_index_, zone_);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001517 }
1518
1519 void AddInverse(ZoneList<CharacterRange>* ranges);
1520
1521#define DECLARE_VISIT(Type) \
1522 virtual void Visit##Type(Type##Node* that);
1523FOR_EACH_NODE_TYPE(DECLARE_VISIT)
1524#undef DECLARE_VISIT
1525
1526 DispatchTable* table() { return table_; }
1527 void set_choice_index(int value) { choice_index_ = value; }
1528
1529 protected:
ager@chromium.org5ec48922009-05-05 07:25:34 +00001530 DispatchTable* table_;
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001531 int choice_index_;
1532 bool ignore_case_;
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001533 Zone* zone_;
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001534};
1535
1536
ager@chromium.org8bb60582008-12-11 12:02:20 +00001537// Assertion propagation moves information about assertions such as
1538// \b to the affected nodes. For instance, in /.\b./ information must
1539// be propagated to the first '.' that whatever follows needs to know
1540// if it matched a word or a non-word, and to the second '.' that it
1541// has to check if it succeeds a word or non-word. In this case the
1542// result will be something like:
1543//
1544// +-------+ +------------+
1545// | . | | . |
1546// +-------+ ---> +------------+
1547// | word? | | check word |
1548// +-------+ +------------+
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001549class Analysis: public NodeVisitor {
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001550 public:
ager@chromium.org38e4c712009-11-11 09:11:58 +00001551 Analysis(bool ignore_case, bool is_ascii)
1552 : ignore_case_(ignore_case),
1553 is_ascii_(is_ascii),
1554 error_message_(NULL) { }
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001555 void EnsureAnalyzed(RegExpNode* node);
1556
1557#define DECLARE_VISIT(Type) \
1558 virtual void Visit##Type(Type##Node* that);
1559FOR_EACH_NODE_TYPE(DECLARE_VISIT)
1560#undef DECLARE_VISIT
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001561 virtual void VisitLoopChoice(LoopChoiceNode* that);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001562
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00001563 bool has_failed() { return error_message_ != NULL; }
1564 const char* error_message() {
1565 ASSERT(error_message_ != NULL);
1566 return error_message_;
1567 }
1568 void fail(const char* error_message) {
1569 error_message_ = error_message;
1570 }
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00001571
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001572 private:
1573 bool ignore_case_;
ager@chromium.org38e4c712009-11-11 09:11:58 +00001574 bool is_ascii_;
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00001575 const char* error_message_;
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001576
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001577 DISALLOW_IMPLICIT_CONSTRUCTORS(Analysis);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001578};
1579
1580
ager@chromium.org8bb60582008-12-11 12:02:20 +00001581struct RegExpCompileData {
1582 RegExpCompileData()
1583 : tree(NULL),
1584 node(NULL),
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001585 simple(true),
iposva@chromium.org245aa852009-02-10 00:49:54 +00001586 contains_anchor(false),
ager@chromium.org8bb60582008-12-11 12:02:20 +00001587 capture_count(0) { }
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001588 RegExpTree* tree;
ager@chromium.org8bb60582008-12-11 12:02:20 +00001589 RegExpNode* node;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001590 bool simple;
iposva@chromium.org245aa852009-02-10 00:49:54 +00001591 bool contains_anchor;
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001592 Handle<String> error;
1593 int capture_count;
1594};
1595
1596
1597class RegExpEngine: public AllStatic {
1598 public:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001599 struct CompilationResult {
1600 explicit CompilationResult(const char* error_message)
1601 : error_message(error_message),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001602 code(HEAP->the_hole_value()),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001603 num_registers(0) {}
1604 CompilationResult(Object* code, int registers)
1605 : error_message(NULL),
1606 code(code),
1607 num_registers(registers) {}
1608 const char* error_message;
1609 Object* code;
1610 int num_registers;
1611 };
1612
1613 static CompilationResult Compile(RegExpCompileData* input,
1614 bool ignore_case,
mstarzinger@chromium.org15613d02012-05-23 12:04:37 +00001615 bool global,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001616 bool multiline,
1617 Handle<String> pattern,
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00001618 Handle<String> sample_subject,
rossberg@chromium.org400388e2012-06-06 09:29:22 +00001619 bool is_ascii, Zone* zone);
ager@chromium.org8bb60582008-12-11 12:02:20 +00001620
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001621 static void DotPrint(const char* label, RegExpNode* node, bool ignore_case);
1622};
1623
1624
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001625class OffsetsVector {
1626 public:
ulan@chromium.org812308e2012-02-29 15:58:45 +00001627 inline OffsetsVector(int num_registers, Isolate* isolate)
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001628 : offsets_vector_length_(num_registers) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001629 if (offsets_vector_length_ > Isolate::kJSRegexpStaticOffsetsVectorSize) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001630 vector_ = NewArray<int>(offsets_vector_length_);
1631 } else {
ulan@chromium.org812308e2012-02-29 15:58:45 +00001632 vector_ = isolate->jsregexp_static_offsets_vector();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001633 }
1634 }
1635 inline ~OffsetsVector() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001636 if (offsets_vector_length_ > Isolate::kJSRegexpStaticOffsetsVectorSize) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001637 DeleteArray(vector_);
1638 vector_ = NULL;
1639 }
1640 }
1641 inline int* vector() { return vector_; }
1642 inline int length() { return offsets_vector_length_; }
1643
mstarzinger@chromium.org15613d02012-05-23 12:04:37 +00001644 static const int kStaticOffsetsVectorSize =
1645 Isolate::kJSRegexpStaticOffsetsVectorSize;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001646
1647 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001648 static Address static_offsets_vector_address(Isolate* isolate) {
1649 return reinterpret_cast<Address>(isolate->jsregexp_static_offsets_vector());
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001650 }
1651
1652 int* vector_;
1653 int offsets_vector_length_;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001654
1655 friend class ExternalReference;
1656};
1657
1658
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001659} } // namespace v8::internal
1660
1661#endif // V8_JSREGEXP_H_