2012-01-25 16:31:25 +00:00
|
|
|
// Copyright 2012 the V8 project authors. All rights reserved.
|
2014-04-29 06:42:26 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
#ifndef V8_JSREGEXP_H_
|
|
|
|
#define V8_JSREGEXP_H_
|
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/allocation.h"
|
|
|
|
#include "src/assembler.h"
|
|
|
|
#include "src/zone-inl.h"
|
2009-10-15 15:01:36 +00:00
|
|
|
|
2009-05-25 10:05:56 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2012-01-25 16:31:25 +00:00
|
|
|
class NodeVisitor;
|
|
|
|
class RegExpCompiler;
|
2008-11-25 11:07:48 +00:00
|
|
|
class RegExpMacroAssembler;
|
2012-01-25 16:31:25 +00:00
|
|
|
class RegExpNode;
|
|
|
|
class RegExpTree;
|
2012-04-17 08:06:53 +00:00
|
|
|
class BoyerMooreLookahead;
|
2008-11-25 11:07:48 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
class RegExpImpl {
|
|
|
|
public:
|
2009-07-07 08:11:19 +00:00
|
|
|
// Whether V8 is compiled with native regexp support or not.
|
|
|
|
static bool UsesNativeRegExp() {
|
2010-04-19 19:30:11 +00:00
|
|
|
#ifdef V8_INTERPRETED_REGEXP
|
2009-07-07 08:11:19 +00:00
|
|
|
return false;
|
2010-04-19 19:30:11 +00:00
|
|
|
#else
|
|
|
|
return true;
|
2009-03-13 10:22:38 +00:00
|
|
|
#endif
|
|
|
|
}
|
2009-07-07 08:11:19 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
// Creates a regular expression literal in the old space.
|
|
|
|
// This function calls the garbage collector if necessary.
|
2014-04-11 10:41:09 +00:00
|
|
|
MUST_USE_RESULT static MaybeHandle<Object> CreateRegExpLiteral(
|
|
|
|
Handle<JSFunction> constructor,
|
|
|
|
Handle<String> pattern,
|
|
|
|
Handle<String> flags);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// Returns a string representation of a regular expression.
|
|
|
|
// Implements RegExp.prototype.toString, see ECMA-262 section 15.10.6.4.
|
|
|
|
// This function calls the garbage collector if necessary.
|
|
|
|
static Handle<String> ToString(Handle<Object> value);
|
|
|
|
|
2008-12-08 12:43:01 +00:00
|
|
|
// Parses the RegExp pattern and prepares the JSRegExp object with
|
|
|
|
// generic data and choice of implementation - as well as what
|
|
|
|
// the implementation wants to store in the data field.
|
2009-03-11 14:00:55 +00:00
|
|
|
// Returns false if compilation fails.
|
2014-04-17 13:27:02 +00:00
|
|
|
MUST_USE_RESULT static MaybeHandle<Object> Compile(
|
|
|
|
Handle<JSRegExp> re,
|
|
|
|
Handle<String> pattern,
|
|
|
|
Handle<String> flags);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// See ECMA-262 section 15.10.6.2.
|
|
|
|
// This function calls the garbage collector if necessary.
|
2014-04-22 09:32:42 +00:00
|
|
|
MUST_USE_RESULT static MaybeHandle<Object> Exec(
|
|
|
|
Handle<JSRegExp> regexp,
|
|
|
|
Handle<String> subject,
|
|
|
|
int index,
|
|
|
|
Handle<JSArray> lastMatchInfo);
|
2008-10-02 15:35:28 +00:00
|
|
|
|
2008-12-08 12:43:01 +00:00
|
|
|
// Prepares a JSRegExp object with Irregexp-specific data.
|
2010-03-19 12:01:17 +00:00
|
|
|
static void IrregexpInitialize(Handle<JSRegExp> re,
|
|
|
|
Handle<String> pattern,
|
|
|
|
JSRegExp::Flags flags,
|
|
|
|
int capture_register_count);
|
2008-11-25 11:07:48 +00:00
|
|
|
|
|
|
|
|
2009-03-11 14:00:55 +00:00
|
|
|
static void AtomCompile(Handle<JSRegExp> re,
|
|
|
|
Handle<String> pattern,
|
|
|
|
JSRegExp::Flags flags,
|
|
|
|
Handle<String> match_pattern);
|
|
|
|
|
2012-08-28 09:37:41 +00:00
|
|
|
|
|
|
|
static int AtomExecRaw(Handle<JSRegExp> regexp,
|
|
|
|
Handle<String> subject,
|
|
|
|
int index,
|
|
|
|
int32_t* output,
|
|
|
|
int output_size);
|
|
|
|
|
|
|
|
|
2008-10-02 15:35:28 +00:00
|
|
|
static Handle<Object> AtomExec(Handle<JSRegExp> regexp,
|
|
|
|
Handle<String> subject,
|
2009-03-11 14:00:55 +00:00
|
|
|
int index,
|
|
|
|
Handle<JSArray> lastMatchInfo);
|
2008-10-02 15:35:28 +00:00
|
|
|
|
2010-03-19 12:01:17 +00:00
|
|
|
enum IrregexpResult { RE_FAILURE = 0, RE_SUCCESS = 1, RE_EXCEPTION = -1 };
|
|
|
|
|
|
|
|
// Prepare a RegExp for being executed one or more times (using
|
|
|
|
// IrregexpExecOnce) on the subject.
|
|
|
|
// This ensures that the regexp is compiled for the subject, and that
|
|
|
|
// the subject is flat.
|
|
|
|
// Returns the number of integer spaces required by IrregexpExecOnce
|
2012-08-28 09:37:41 +00:00
|
|
|
// as its "registers" argument. If the regexp cannot be compiled,
|
2010-03-19 12:01:17 +00:00
|
|
|
// an exception is set as pending, and this function returns negative.
|
|
|
|
static int IrregexpPrepare(Handle<JSRegExp> regexp,
|
2012-06-14 17:06:16 +00:00
|
|
|
Handle<String> subject);
|
2010-03-19 12:01:17 +00:00
|
|
|
|
2012-05-22 14:05:44 +00:00
|
|
|
// Execute a regular expression on the subject, starting from index.
|
|
|
|
// If matching succeeds, return the number of matches. This can be larger
|
|
|
|
// than one in the case of global regular expressions.
|
|
|
|
// The captures and subcaptures are stored into the registers vector.
|
2010-03-19 12:01:17 +00:00
|
|
|
// If matching fails, returns RE_FAILURE.
|
|
|
|
// If execution fails, sets a pending exception and returns RE_EXCEPTION.
|
2012-05-22 14:05:44 +00:00
|
|
|
static int IrregexpExecRaw(Handle<JSRegExp> regexp,
|
|
|
|
Handle<String> subject,
|
|
|
|
int index,
|
2012-08-28 09:37:41 +00:00
|
|
|
int32_t* output,
|
|
|
|
int output_size);
|
2010-03-19 12:01:17 +00:00
|
|
|
|
2008-11-25 11:07:48 +00:00
|
|
|
// Execute an Irregexp bytecode pattern.
|
2009-03-25 12:14:10 +00:00
|
|
|
// On a successful match, the result is a JSArray containing
|
2012-08-28 09:37:41 +00:00
|
|
|
// captured positions. On a failure, the result is the null value.
|
2009-03-25 12:14:10 +00:00
|
|
|
// Returns an empty handle in case of an exception.
|
2014-04-22 09:32:42 +00:00
|
|
|
MUST_USE_RESULT static MaybeHandle<Object> IrregexpExec(
|
|
|
|
Handle<JSRegExp> regexp,
|
|
|
|
Handle<String> subject,
|
|
|
|
int index,
|
|
|
|
Handle<JSArray> lastMatchInfo);
|
2008-11-25 11:07:48 +00:00
|
|
|
|
2012-08-28 09:37:41 +00:00
|
|
|
// Set last match info. If match is NULL, then setting captures is omitted.
|
|
|
|
static Handle<JSArray> SetLastMatchInfo(Handle<JSArray> last_match_info,
|
|
|
|
Handle<String> subject,
|
|
|
|
int capture_count,
|
|
|
|
int32_t* match);
|
|
|
|
|
|
|
|
|
|
|
|
class GlobalCache {
|
|
|
|
public:
|
|
|
|
GlobalCache(Handle<JSRegExp> regexp,
|
|
|
|
Handle<String> subject,
|
|
|
|
bool is_global,
|
|
|
|
Isolate* isolate);
|
|
|
|
|
2013-02-06 12:58:09 +00:00
|
|
|
INLINE(~GlobalCache());
|
2012-08-28 09:37:41 +00:00
|
|
|
|
|
|
|
// Fetch the next entry in the cache for global regexp match results.
|
|
|
|
// This does not set the last match info. Upon failure, NULL is returned.
|
|
|
|
// The cause can be checked with Result(). The previous
|
|
|
|
// result is still in available in memory when a failure happens.
|
2013-02-06 12:58:09 +00:00
|
|
|
INLINE(int32_t* FetchNext());
|
2012-08-28 09:37:41 +00:00
|
|
|
|
2013-02-06 12:58:09 +00:00
|
|
|
INLINE(int32_t* LastSuccessfulMatch());
|
2012-08-28 09:37:41 +00:00
|
|
|
|
2013-02-06 12:58:09 +00:00
|
|
|
INLINE(bool HasException()) { return num_matches_ < 0; }
|
2012-08-28 09:37:41 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
int num_matches_;
|
|
|
|
int max_matches_;
|
|
|
|
int current_match_index_;
|
|
|
|
int registers_per_match_;
|
|
|
|
// Pointer to the last set of captures.
|
|
|
|
int32_t* register_array_;
|
|
|
|
int register_array_size_;
|
|
|
|
Handle<JSRegExp> regexp_;
|
|
|
|
Handle<String> subject_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-01-06 11:09:30 +00:00
|
|
|
// Array index in the lastMatchInfo array.
|
2009-03-11 14:00:55 +00:00
|
|
|
static const int kLastCaptureCount = 0;
|
|
|
|
static const int kLastSubject = 1;
|
|
|
|
static const int kLastInput = 2;
|
2009-03-17 12:44:20 +00:00
|
|
|
static const int kFirstCapture = 3;
|
2009-03-11 14:00:55 +00:00
|
|
|
static const int kLastMatchOverhead = 3;
|
|
|
|
|
2010-01-06 11:09:30 +00:00
|
|
|
// Direct offset into the lastMatchInfo array.
|
|
|
|
static const int kLastCaptureCountOffset =
|
|
|
|
FixedArray::kHeaderSize + kLastCaptureCount * kPointerSize;
|
|
|
|
static const int kLastSubjectOffset =
|
|
|
|
FixedArray::kHeaderSize + kLastSubject * kPointerSize;
|
|
|
|
static const int kLastInputOffset =
|
|
|
|
FixedArray::kHeaderSize + kLastInput * kPointerSize;
|
|
|
|
static const int kFirstCaptureOffset =
|
|
|
|
FixedArray::kHeaderSize + kFirstCapture * kPointerSize;
|
|
|
|
|
2009-03-13 10:22:38 +00:00
|
|
|
// Used to access the lastMatchInfo array.
|
2009-03-11 14:00:55 +00:00
|
|
|
static int GetCapture(FixedArray* array, int index) {
|
|
|
|
return Smi::cast(array->get(index + kFirstCapture))->value();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void SetLastCaptureCount(FixedArray* array, int to) {
|
|
|
|
array->set(kLastCaptureCount, Smi::FromInt(to));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void SetLastSubject(FixedArray* array, String* to) {
|
2009-03-17 12:44:20 +00:00
|
|
|
array->set(kLastSubject, to);
|
2009-03-11 14:00:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void SetLastInput(FixedArray* array, String* to) {
|
2009-03-17 12:44:20 +00:00
|
|
|
array->set(kLastInput, to);
|
2009-03-11 14:00:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void SetCapture(FixedArray* array, int index, int to) {
|
|
|
|
array->set(index + kFirstCapture, Smi::FromInt(to));
|
|
|
|
}
|
2009-02-27 10:04:34 +00:00
|
|
|
|
2009-03-13 10:22:38 +00:00
|
|
|
static int GetLastCaptureCount(FixedArray* array) {
|
|
|
|
return Smi::cast(array->get(kLastCaptureCount))->value();
|
|
|
|
}
|
2009-03-11 14:00:55 +00:00
|
|
|
|
2009-03-13 10:22:38 +00:00
|
|
|
// For acting on the JSRegExp data FixedArray.
|
2009-03-11 14:00:55 +00:00
|
|
|
static int IrregexpMaxRegisterCount(FixedArray* re);
|
|
|
|
static void SetIrregexpMaxRegisterCount(FixedArray* re, int value);
|
|
|
|
static int IrregexpNumberOfCaptures(FixedArray* re);
|
|
|
|
static int IrregexpNumberOfRegisters(FixedArray* re);
|
|
|
|
static ByteArray* IrregexpByteCode(FixedArray* re, bool is_ascii);
|
|
|
|
static Code* IrregexpNativeCode(FixedArray* re, bool is_ascii);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2011-05-11 06:39:27 +00:00
|
|
|
// Limit the space regexps take up on the heap. In order to limit this we
|
|
|
|
// would like to keep track of the amount of regexp code on the heap. This
|
|
|
|
// is not tracked, however. As a conservative approximation we track the
|
|
|
|
// total regexp code compiled including code that has subsequently been freed
|
|
|
|
// and the total executable memory at any point.
|
|
|
|
static const int kRegExpExecutableMemoryLimit = 16 * MB;
|
|
|
|
static const int kRegWxpCompiledLimit = 1 * MB;
|
|
|
|
|
2009-03-13 10:22:38 +00:00
|
|
|
private:
|
2012-04-02 09:38:07 +00:00
|
|
|
static bool CompileIrregexp(
|
2012-06-14 17:06:16 +00:00
|
|
|
Handle<JSRegExp> re, Handle<String> sample_subject, bool is_ascii);
|
2012-04-02 09:38:07 +00:00
|
|
|
static inline bool EnsureCompiledIrregexp(
|
2012-06-14 17:06:16 +00:00
|
|
|
Handle<JSRegExp> re, Handle<String> sample_subject, bool is_ascii);
|
2008-07-03 15:10:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-01-07 19:01:23 +00:00
|
|
|
// Represents the location of one element relative to the intersection of
|
|
|
|
// two sets. Corresponds to the four areas of a Venn diagram.
|
|
|
|
enum ElementInSetsRelation {
|
|
|
|
kInsideNone = 0,
|
|
|
|
kInsideFirst = 1,
|
|
|
|
kInsideSecond = 2,
|
|
|
|
kInsideBoth = 3
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-04-26 09:11:19 +00:00
|
|
|
// Represents code units in the range from from_ to to_, both ends are
|
|
|
|
// inclusive.
|
2008-11-25 11:07:48 +00:00
|
|
|
class CharacterRange {
|
|
|
|
public:
|
|
|
|
CharacterRange() : from_(0), to_(0) { }
|
|
|
|
// For compatibility with the CHECK_OK macro
|
2014-08-04 11:34:54 +00:00
|
|
|
CharacterRange(void* null) { DCHECK_EQ(NULL, null); } //NOLINT
|
2008-11-26 08:03:55 +00:00
|
|
|
CharacterRange(uc16 from, uc16 to) : from_(from), to_(to) { }
|
2012-06-11 12:42:31 +00:00
|
|
|
static void AddClassEscape(uc16 type, ZoneList<CharacterRange>* ranges,
|
|
|
|
Zone* zone);
|
2012-04-17 08:06:53 +00:00
|
|
|
static Vector<const int> GetWordBounds();
|
2008-11-25 11:07:48 +00:00
|
|
|
static inline CharacterRange Singleton(uc16 value) {
|
|
|
|
return CharacterRange(value, value);
|
|
|
|
}
|
|
|
|
static inline CharacterRange Range(uc16 from, uc16 to) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(from <= to);
|
2008-11-25 11:07:48 +00:00
|
|
|
return CharacterRange(from, to);
|
|
|
|
}
|
|
|
|
static inline CharacterRange Everything() {
|
|
|
|
return CharacterRange(0, 0xFFFF);
|
|
|
|
}
|
|
|
|
bool Contains(uc16 i) { return from_ <= i && i <= to_; }
|
|
|
|
uc16 from() const { return from_; }
|
|
|
|
void set_from(uc16 value) { from_ = value; }
|
|
|
|
uc16 to() const { return to_; }
|
|
|
|
void set_to(uc16 value) { to_ = value; }
|
|
|
|
bool is_valid() { return from_ <= to_; }
|
2008-12-08 09:22:12 +00:00
|
|
|
bool IsEverything(uc16 max) { return from_ == 0 && to_ >= max; }
|
2008-11-25 11:07:48 +00:00
|
|
|
bool IsSingleton() { return (from_ == to_); }
|
2012-06-11 12:42:31 +00:00
|
|
|
void AddCaseEquivalents(ZoneList<CharacterRange>* ranges, bool is_ascii,
|
|
|
|
Zone* zone);
|
2008-12-01 15:42:35 +00:00
|
|
|
static void Split(ZoneList<CharacterRange>* base,
|
2012-04-17 08:06:53 +00:00
|
|
|
Vector<const int> overlay,
|
2008-12-01 15:42:35 +00:00
|
|
|
ZoneList<CharacterRange>** included,
|
2012-06-11 12:42:31 +00:00
|
|
|
ZoneList<CharacterRange>** excluded,
|
|
|
|
Zone* zone);
|
2010-01-07 19:01:23 +00:00
|
|
|
// Whether a range list is in canonical form: Ranges ordered by from value,
|
|
|
|
// and ranges non-overlapping and non-adjacent.
|
|
|
|
static bool IsCanonical(ZoneList<CharacterRange>* ranges);
|
|
|
|
// Convert range list to canonical form. The characters covered by the ranges
|
|
|
|
// will still be the same, but no character is in more than one range, and
|
|
|
|
// adjacent ranges are merged. The resulting list may be shorter than the
|
|
|
|
// original, but cannot be longer.
|
|
|
|
static void Canonicalize(ZoneList<CharacterRange>* ranges);
|
|
|
|
// Negate the contents of a character range in canonical form.
|
|
|
|
static void Negate(ZoneList<CharacterRange>* src,
|
2012-06-11 12:42:31 +00:00
|
|
|
ZoneList<CharacterRange>* dst,
|
|
|
|
Zone* zone);
|
2008-11-25 11:07:48 +00:00
|
|
|
static const int kStartMarker = (1 << 24);
|
|
|
|
static const int kPayloadMask = (1 << 24) - 1;
|
2008-12-01 15:42:35 +00:00
|
|
|
|
2008-11-25 11:07:48 +00:00
|
|
|
private:
|
|
|
|
uc16 from_;
|
|
|
|
uc16 to_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// A set of unsigned integers that behaves especially well on small
|
|
|
|
// integers (< 32). May do zone-allocation.
|
|
|
|
class OutSet: public ZoneObject {
|
|
|
|
public:
|
|
|
|
OutSet() : first_(0), remaining_(NULL), successors_(NULL) { }
|
2012-06-11 12:42:31 +00:00
|
|
|
OutSet* Extend(unsigned value, Zone* zone);
|
2014-06-03 15:45:38 +00:00
|
|
|
bool Get(unsigned value) const;
|
2008-11-25 11:07:48 +00:00
|
|
|
static const unsigned kFirstLimit = 32;
|
|
|
|
|
2008-11-26 08:03:55 +00:00
|
|
|
private:
|
2008-11-25 11:07:48 +00:00
|
|
|
// Destructively set a value in this set. In most cases you want
|
|
|
|
// to use Extend instead to ensure that only one instance exists
|
|
|
|
// that contains the same values.
|
2012-06-11 12:42:31 +00:00
|
|
|
void Set(unsigned value, Zone* zone);
|
2008-11-25 11:07:48 +00:00
|
|
|
|
|
|
|
// The successors are a list of sets that contain the same values
|
|
|
|
// as this set and the one more value that is not present in this
|
|
|
|
// set.
|
2012-06-11 12:42:31 +00:00
|
|
|
ZoneList<OutSet*>* successors(Zone* zone) { return successors_; }
|
2008-11-25 11:07:48 +00:00
|
|
|
|
|
|
|
OutSet(uint32_t first, ZoneList<unsigned>* remaining)
|
2008-11-26 08:03:55 +00:00
|
|
|
: first_(first), remaining_(remaining), successors_(NULL) { }
|
2008-11-25 11:07:48 +00:00
|
|
|
uint32_t first_;
|
|
|
|
ZoneList<unsigned>* remaining_;
|
|
|
|
ZoneList<OutSet*>* successors_;
|
2009-01-15 12:45:48 +00:00
|
|
|
friend class Trace;
|
2008-11-25 11:07:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// A mapping from integers, specified as ranges, to a set of integers.
|
|
|
|
// Used for mapping character ranges to choices.
|
2008-12-01 15:42:35 +00:00
|
|
|
class DispatchTable : public ZoneObject {
|
2008-11-25 11:07:48 +00:00
|
|
|
public:
|
2012-06-11 12:42:31 +00:00
|
|
|
explicit DispatchTable(Zone* zone) : tree_(zone) { }
|
|
|
|
|
2008-11-25 11:07:48 +00:00
|
|
|
class Entry {
|
|
|
|
public:
|
2008-11-26 08:03:55 +00:00
|
|
|
Entry() : from_(0), to_(0), out_set_(NULL) { }
|
2008-11-25 11:07:48 +00:00
|
|
|
Entry(uc16 from, uc16 to, OutSet* out_set)
|
2008-11-26 08:03:55 +00:00
|
|
|
: from_(from), to_(to), out_set_(out_set) { }
|
2008-11-25 11:07:48 +00:00
|
|
|
uc16 from() { return from_; }
|
|
|
|
uc16 to() { return to_; }
|
|
|
|
void set_to(uc16 value) { to_ = value; }
|
2012-06-11 12:42:31 +00:00
|
|
|
void AddValue(int value, Zone* zone) {
|
|
|
|
out_set_ = out_set_->Extend(value, zone);
|
|
|
|
}
|
2008-11-25 11:07:48 +00:00
|
|
|
OutSet* out_set() { return out_set_; }
|
|
|
|
private:
|
|
|
|
uc16 from_;
|
|
|
|
uc16 to_;
|
|
|
|
OutSet* out_set_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Config {
|
|
|
|
public:
|
|
|
|
typedef uc16 Key;
|
|
|
|
typedef Entry Value;
|
|
|
|
static const uc16 kNoKey;
|
2011-10-07 14:41:08 +00:00
|
|
|
static const Entry NoValue() { return Value(); }
|
2008-11-25 11:07:48 +00:00
|
|
|
static inline int Compare(uc16 a, uc16 b) {
|
|
|
|
if (a == b)
|
|
|
|
return 0;
|
|
|
|
else if (a < b)
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-06-11 12:42:31 +00:00
|
|
|
void AddRange(CharacterRange range, int value, Zone* zone);
|
2008-11-25 11:07:48 +00:00
|
|
|
OutSet* Get(uc16 value);
|
|
|
|
void Dump();
|
|
|
|
|
|
|
|
template <typename Callback>
|
2012-06-11 12:42:31 +00:00
|
|
|
void ForEach(Callback* callback) {
|
|
|
|
return tree()->ForEach(callback);
|
|
|
|
}
|
2011-09-08 19:57:14 +00:00
|
|
|
|
2008-11-25 11:07:48 +00:00
|
|
|
private:
|
|
|
|
// There can't be a static empty set since it allocates its
|
|
|
|
// successors in a zone and caches them.
|
|
|
|
OutSet* empty() { return &empty_; }
|
|
|
|
OutSet empty_;
|
|
|
|
ZoneSplayTree<Config>* tree() { return &tree_; }
|
|
|
|
ZoneSplayTree<Config> tree_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#define FOR_EACH_NODE_TYPE(VISIT) \
|
|
|
|
VISIT(End) \
|
|
|
|
VISIT(Action) \
|
|
|
|
VISIT(Choice) \
|
|
|
|
VISIT(BackReference) \
|
2009-01-19 18:56:47 +00:00
|
|
|
VISIT(Assertion) \
|
2008-11-25 11:07:48 +00:00
|
|
|
VISIT(Text)
|
|
|
|
|
|
|
|
|
|
|
|
#define FOR_EACH_REG_EXP_TREE_TYPE(VISIT) \
|
|
|
|
VISIT(Disjunction) \
|
|
|
|
VISIT(Alternative) \
|
|
|
|
VISIT(Assertion) \
|
|
|
|
VISIT(CharacterClass) \
|
|
|
|
VISIT(Atom) \
|
|
|
|
VISIT(Quantifier) \
|
|
|
|
VISIT(Capture) \
|
|
|
|
VISIT(Lookahead) \
|
|
|
|
VISIT(BackReference) \
|
|
|
|
VISIT(Empty) \
|
|
|
|
VISIT(Text)
|
|
|
|
|
|
|
|
|
|
|
|
#define FORWARD_DECLARE(Name) class RegExp##Name;
|
|
|
|
FOR_EACH_REG_EXP_TREE_TYPE(FORWARD_DECLARE)
|
|
|
|
#undef FORWARD_DECLARE
|
|
|
|
|
|
|
|
|
2014-09-02 07:07:52 +00:00
|
|
|
class TextElement FINAL BASE_EMBEDDED {
|
2008-11-25 11:07:48 +00:00
|
|
|
public:
|
2013-08-23 11:06:16 +00:00
|
|
|
enum TextType {
|
|
|
|
ATOM,
|
|
|
|
CHAR_CLASS
|
|
|
|
};
|
|
|
|
|
2008-11-25 11:07:48 +00:00
|
|
|
static TextElement Atom(RegExpAtom* atom);
|
|
|
|
static TextElement CharClass(RegExpCharacterClass* char_class);
|
2013-08-23 11:06:16 +00:00
|
|
|
|
|
|
|
int cp_offset() const { return cp_offset_; }
|
|
|
|
void set_cp_offset(int cp_offset) { cp_offset_ = cp_offset; }
|
|
|
|
int length() const;
|
|
|
|
|
|
|
|
TextType text_type() const { return text_type_; }
|
|
|
|
|
|
|
|
RegExpTree* tree() const { return tree_; }
|
|
|
|
|
|
|
|
RegExpAtom* atom() const {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(text_type() == ATOM);
|
2013-08-23 11:06:16 +00:00
|
|
|
return reinterpret_cast<RegExpAtom*>(tree());
|
|
|
|
}
|
|
|
|
|
|
|
|
RegExpCharacterClass* char_class() const {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(text_type() == CHAR_CLASS);
|
2013-08-23 11:06:16 +00:00
|
|
|
return reinterpret_cast<RegExpCharacterClass*>(tree());
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
TextElement(TextType text_type, RegExpTree* tree)
|
|
|
|
: cp_offset_(-1), text_type_(text_type), tree_(tree) {}
|
|
|
|
|
|
|
|
int cp_offset_;
|
|
|
|
TextType text_type_;
|
|
|
|
RegExpTree* tree_;
|
2008-11-25 11:07:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-01-15 12:45:48 +00:00
|
|
|
class Trace;
|
2014-09-04 12:47:13 +00:00
|
|
|
struct PreloadState;
|
|
|
|
class GreedyLoopState;
|
|
|
|
class AlternativeGenerationList;
|
2008-12-08 09:22:12 +00:00
|
|
|
|
2008-11-25 11:07:48 +00:00
|
|
|
struct NodeInfo {
|
|
|
|
NodeInfo()
|
2008-11-26 08:03:55 +00:00
|
|
|
: being_analyzed(false),
|
|
|
|
been_analyzed(false),
|
|
|
|
follows_word_interest(false),
|
|
|
|
follows_newline_interest(false),
|
2008-11-27 10:35:06 +00:00
|
|
|
follows_start_interest(false),
|
|
|
|
at_end(false),
|
2012-04-26 09:11:19 +00:00
|
|
|
visited(false),
|
|
|
|
replacement_calculated(false) { }
|
2008-11-27 10:35:06 +00:00
|
|
|
|
2008-12-01 15:42:35 +00:00
|
|
|
// Returns true if the interests and assumptions of this node
|
|
|
|
// matches the given one.
|
|
|
|
bool Matches(NodeInfo* that) {
|
|
|
|
return (at_end == that->at_end) &&
|
|
|
|
(follows_word_interest == that->follows_word_interest) &&
|
|
|
|
(follows_newline_interest == that->follows_newline_interest) &&
|
2008-12-18 14:30:53 +00:00
|
|
|
(follows_start_interest == that->follows_start_interest);
|
2008-11-25 11:07:48 +00:00
|
|
|
}
|
2008-11-27 10:35:06 +00:00
|
|
|
|
|
|
|
// Updates the interests of this node given the interests of the
|
|
|
|
// node preceding it.
|
|
|
|
void AddFromPreceding(NodeInfo* that) {
|
|
|
|
at_end |= that->at_end;
|
|
|
|
follows_word_interest |= that->follows_word_interest;
|
|
|
|
follows_newline_interest |= that->follows_newline_interest;
|
|
|
|
follows_start_interest |= that->follows_start_interest;
|
2008-11-25 11:07:48 +00:00
|
|
|
}
|
2008-11-27 10:35:06 +00:00
|
|
|
|
2008-12-11 11:13:13 +00:00
|
|
|
bool HasLookbehind() {
|
|
|
|
return follows_word_interest ||
|
|
|
|
follows_newline_interest ||
|
|
|
|
follows_start_interest;
|
|
|
|
}
|
|
|
|
|
2008-11-27 10:35:06 +00:00
|
|
|
// Sets the interests of this node to include the interests of the
|
|
|
|
// following node.
|
|
|
|
void AddFromFollowing(NodeInfo* that) {
|
|
|
|
follows_word_interest |= that->follows_word_interest;
|
|
|
|
follows_newline_interest |= that->follows_newline_interest;
|
|
|
|
follows_start_interest |= that->follows_start_interest;
|
2008-11-25 11:07:48 +00:00
|
|
|
}
|
2008-11-27 10:35:06 +00:00
|
|
|
|
2008-12-01 15:42:35 +00:00
|
|
|
void ResetCompilationState() {
|
|
|
|
being_analyzed = false;
|
|
|
|
been_analyzed = false;
|
|
|
|
}
|
|
|
|
|
2008-11-25 11:07:48 +00:00
|
|
|
bool being_analyzed: 1;
|
|
|
|
bool been_analyzed: 1;
|
2008-12-01 15:42:35 +00:00
|
|
|
|
2008-11-27 10:35:06 +00:00
|
|
|
// These bits are set of this node has to know what the preceding
|
|
|
|
// character was.
|
2008-11-25 11:07:48 +00:00
|
|
|
bool follows_word_interest: 1;
|
|
|
|
bool follows_newline_interest: 1;
|
|
|
|
bool follows_start_interest: 1;
|
2008-11-27 10:35:06 +00:00
|
|
|
|
|
|
|
bool at_end: 1;
|
2008-12-02 08:16:12 +00:00
|
|
|
bool visited: 1;
|
2012-04-26 09:11:19 +00:00
|
|
|
bool replacement_calculated: 1;
|
2008-12-01 15:42:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-12-19 12:02:34 +00:00
|
|
|
// Details of a quick mask-compare check that can look ahead in the
|
|
|
|
// input stream.
|
|
|
|
class QuickCheckDetails {
|
|
|
|
public:
|
|
|
|
QuickCheckDetails()
|
|
|
|
: characters_(0),
|
|
|
|
mask_(0),
|
2009-02-03 11:43:55 +00:00
|
|
|
value_(0),
|
|
|
|
cannot_match_(false) { }
|
2008-12-19 12:02:34 +00:00
|
|
|
explicit QuickCheckDetails(int characters)
|
|
|
|
: characters_(characters),
|
|
|
|
mask_(0),
|
2009-02-03 11:43:55 +00:00
|
|
|
value_(0),
|
|
|
|
cannot_match_(false) { }
|
2008-12-19 12:02:34 +00:00
|
|
|
bool Rationalize(bool ascii);
|
|
|
|
// Merge in the information from another branch of an alternation.
|
|
|
|
void Merge(QuickCheckDetails* other, int from_index);
|
|
|
|
// Advance the current position by some amount.
|
|
|
|
void Advance(int by, bool ascii);
|
|
|
|
void Clear();
|
2009-02-03 11:43:55 +00:00
|
|
|
bool cannot_match() { return cannot_match_; }
|
|
|
|
void set_cannot_match() { cannot_match_ = true; }
|
2008-12-19 12:02:34 +00:00
|
|
|
struct Position {
|
|
|
|
Position() : mask(0), value(0), determines_perfectly(false) { }
|
|
|
|
uc16 mask;
|
|
|
|
uc16 value;
|
|
|
|
bool determines_perfectly;
|
|
|
|
};
|
|
|
|
int characters() { return characters_; }
|
|
|
|
void set_characters(int characters) { characters_ = characters; }
|
|
|
|
Position* positions(int index) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(index >= 0);
|
|
|
|
DCHECK(index < characters_);
|
2008-12-19 12:02:34 +00:00
|
|
|
return positions_ + index;
|
|
|
|
}
|
|
|
|
uint32_t mask() { return mask_; }
|
|
|
|
uint32_t value() { return value_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
// How many characters do we have quick check information from. This is
|
|
|
|
// the same for all branches of a choice node.
|
|
|
|
int characters_;
|
|
|
|
Position positions_[4];
|
|
|
|
// These values are the condensate of the above array after Rationalize().
|
|
|
|
uint32_t mask_;
|
|
|
|
uint32_t value_;
|
2009-02-03 11:43:55 +00:00
|
|
|
// If set to true, there is no way this quick check can match at all.
|
|
|
|
// E.g., if it requires to be at the start of the input, and isn't.
|
|
|
|
bool cannot_match_;
|
2008-12-19 12:02:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-04-26 09:11:19 +00:00
|
|
|
extern int kUninitializedRegExpNodePlaceHolder;
|
|
|
|
|
|
|
|
|
2008-11-25 11:07:48 +00:00
|
|
|
class RegExpNode: public ZoneObject {
|
|
|
|
public:
|
2012-06-04 14:42:58 +00:00
|
|
|
explicit RegExpNode(Zone* zone)
|
|
|
|
: replacement_(NULL), trace_count_(0), zone_(zone) {
|
2012-04-17 08:06:53 +00:00
|
|
|
bm_info_[0] = bm_info_[1] = NULL;
|
|
|
|
}
|
2008-12-19 12:02:34 +00:00
|
|
|
virtual ~RegExpNode();
|
2008-11-25 11:07:48 +00:00
|
|
|
virtual void Accept(NodeVisitor* visitor) = 0;
|
|
|
|
// Generates a goto to this node or actually generates the code at this point.
|
2009-01-26 20:09:35 +00:00
|
|
|
virtual void Emit(RegExpCompiler* compiler, Trace* trace) = 0;
|
2008-12-19 12:02:34 +00:00
|
|
|
// How many characters must this node consume at a minimum in order to
|
2009-01-26 19:38:26 +00:00
|
|
|
// succeed. If we have found at least 'still_to_find' characters that
|
|
|
|
// must be consumed there is no need to ask any following nodes whether
|
2010-12-03 09:54:06 +00:00
|
|
|
// they are sure to eat any more characters. The not_at_start argument is
|
|
|
|
// used to indicate that we know we are not at the start of the input. In
|
|
|
|
// this case anchored branches will always fail and can be ignored when
|
|
|
|
// determining how many characters are consumed on success.
|
2013-03-01 14:50:14 +00:00
|
|
|
virtual int EatsAtLeast(int still_to_find, int budget, bool not_at_start) = 0;
|
2008-12-19 12:02:34 +00:00
|
|
|
// Emits some quick code that checks whether the preloaded characters match.
|
|
|
|
// Falls through on certain failure, jumps to the label on possible success.
|
|
|
|
// If the node cannot make a quick check it does nothing and returns false.
|
|
|
|
bool EmitQuickCheck(RegExpCompiler* compiler,
|
2009-01-15 12:45:48 +00:00
|
|
|
Trace* trace,
|
2008-12-19 12:02:34 +00:00
|
|
|
bool preload_has_checked_bounds,
|
|
|
|
Label* on_possible_success,
|
|
|
|
QuickCheckDetails* details_return,
|
|
|
|
bool fall_through_on_failure);
|
|
|
|
// For a given number of characters this returns a mask and a value. The
|
|
|
|
// next n characters are anded with the mask and compared with the value.
|
|
|
|
// A comparison failure indicates the node cannot match the next n characters.
|
|
|
|
// A comparison success indicates the node may match.
|
|
|
|
virtual void GetQuickCheckDetails(QuickCheckDetails* details,
|
|
|
|
RegExpCompiler* compiler,
|
2009-02-03 11:43:55 +00:00
|
|
|
int characters_filled_in,
|
|
|
|
bool not_at_start) = 0;
|
2008-12-08 09:22:12 +00:00
|
|
|
static const int kNodeIsTooComplexForGreedyLoops = -1;
|
|
|
|
virtual int GreedyLoopTextLength() { return kNodeIsTooComplexForGreedyLoops; }
|
2012-04-02 09:38:07 +00:00
|
|
|
// Only returns the successor for a text node of length 1 that matches any
|
|
|
|
// character and that has no guards on it.
|
|
|
|
virtual RegExpNode* GetSuccessorOfOmnivorousTextNode(
|
|
|
|
RegExpCompiler* compiler) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collects information on the possible code units (mod 128) that can match if
|
|
|
|
// we look forward. This is used for a Boyer-Moore-like string searching
|
|
|
|
// implementation. TODO(erikcorry): This should share more code with
|
2012-06-01 11:28:52 +00:00
|
|
|
// EatsAtLeast, GetQuickCheckDetails. The budget argument is used to limit
|
|
|
|
// the number of nodes we are willing to look at in order to create this data.
|
2013-03-01 14:50:14 +00:00
|
|
|
static const int kRecursionBudget = 200;
|
2012-05-31 11:59:04 +00:00
|
|
|
virtual void FillInBMInfo(int offset,
|
2012-06-01 11:28:52 +00:00
|
|
|
int budget,
|
2012-05-31 11:59:04 +00:00
|
|
|
BoyerMooreLookahead* bm,
|
|
|
|
bool not_at_start) {
|
2012-04-02 09:38:07 +00:00
|
|
|
UNREACHABLE();
|
|
|
|
}
|
2012-04-26 09:11:19 +00:00
|
|
|
|
|
|
|
// If we know that the input is ASCII then there are some nodes that can
|
|
|
|
// never match. This method returns a node that can be substituted for
|
|
|
|
// itself, or NULL if the node can never match.
|
2013-01-09 10:30:54 +00:00
|
|
|
virtual RegExpNode* FilterASCII(int depth, bool ignore_case) { return this; }
|
2012-04-26 09:11:19 +00:00
|
|
|
// Helper for FilterASCII.
|
|
|
|
RegExpNode* replacement() {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(info()->replacement_calculated);
|
2012-04-26 09:11:19 +00:00
|
|
|
return replacement_;
|
|
|
|
}
|
|
|
|
RegExpNode* set_replacement(RegExpNode* replacement) {
|
|
|
|
info()->replacement_calculated = true;
|
|
|
|
replacement_ = replacement;
|
|
|
|
return replacement; // For convenience.
|
|
|
|
}
|
|
|
|
|
2012-04-17 08:06:53 +00:00
|
|
|
// We want to avoid recalculating the lookahead info, so we store it on the
|
|
|
|
// node. Only info that is for this node is stored. We can tell that the
|
|
|
|
// info is for this node when offset == 0, so the information is calculated
|
|
|
|
// relative to this node.
|
|
|
|
void SaveBMInfo(BoyerMooreLookahead* bm, bool not_at_start, int offset) {
|
|
|
|
if (offset == 0) set_bm_info(not_at_start, bm);
|
|
|
|
}
|
2012-04-02 09:38:07 +00:00
|
|
|
|
2008-12-08 09:22:12 +00:00
|
|
|
Label* label() { return &label_; }
|
2012-01-16 12:38:59 +00:00
|
|
|
// If non-generic code is generated for a node (i.e. the node is not at the
|
2009-01-15 12:45:48 +00:00
|
|
|
// start of the trace) then it cannot be reused. This variable sets a limit
|
|
|
|
// on how often we allow that to happen before we insist on starting a new
|
|
|
|
// trace and generating generic code for a node that can be reused by flushing
|
|
|
|
// the deferred actions in the current trace and generating a goto.
|
|
|
|
static const int kMaxCopiesCodeGenerated = 10;
|
2008-11-27 10:35:06 +00:00
|
|
|
|
2008-11-25 11:07:48 +00:00
|
|
|
NodeInfo* info() { return &info_; }
|
2008-12-01 15:42:35 +00:00
|
|
|
|
2012-04-17 08:06:53 +00:00
|
|
|
BoyerMooreLookahead* bm_info(bool not_at_start) {
|
|
|
|
return bm_info_[not_at_start ? 1 : 0];
|
|
|
|
}
|
2010-01-07 19:01:23 +00:00
|
|
|
|
2012-06-11 12:42:31 +00:00
|
|
|
Zone* zone() const { return zone_; }
|
2012-06-04 14:42:58 +00:00
|
|
|
|
2008-11-25 11:07:48 +00:00
|
|
|
protected:
|
2009-01-26 20:09:35 +00:00
|
|
|
enum LimitResult { DONE, CONTINUE };
|
2012-04-26 09:11:19 +00:00
|
|
|
RegExpNode* replacement_;
|
2010-01-07 19:01:23 +00:00
|
|
|
|
2009-01-15 12:45:48 +00:00
|
|
|
LimitResult LimitVersions(RegExpCompiler* compiler, Trace* trace);
|
2008-12-01 15:42:35 +00:00
|
|
|
|
2012-04-17 08:06:53 +00:00
|
|
|
void set_bm_info(bool not_at_start, BoyerMooreLookahead* bm) {
|
|
|
|
bm_info_[not_at_start ? 1 : 0] = bm;
|
|
|
|
}
|
|
|
|
|
2008-11-25 11:07:48 +00:00
|
|
|
private:
|
2010-01-07 19:01:23 +00:00
|
|
|
static const int kFirstCharBudget = 10;
|
2008-11-25 11:07:48 +00:00
|
|
|
Label label_;
|
|
|
|
NodeInfo info_;
|
2009-01-15 12:45:48 +00:00
|
|
|
// This variable keeps track of how many times code has been generated for
|
|
|
|
// this node (in different traces). We don't keep track of where the
|
|
|
|
// generated code is located unless the code is generated at the start of
|
|
|
|
// a trace, in which case it is generic and can be reused by flushing the
|
|
|
|
// deferred operations in the current trace and generating a goto.
|
|
|
|
int trace_count_;
|
2012-04-17 08:06:53 +00:00
|
|
|
BoyerMooreLookahead* bm_info_[2];
|
2012-06-04 14:42:58 +00:00
|
|
|
|
|
|
|
Zone* zone_;
|
2008-11-25 11:07:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-01-14 11:32:23 +00:00
|
|
|
// A simple closed interval.
|
|
|
|
class Interval {
|
|
|
|
public:
|
|
|
|
Interval() : from_(kNone), to_(kNone) { }
|
|
|
|
Interval(int from, int to) : from_(from), to_(to) { }
|
|
|
|
Interval Union(Interval that) {
|
|
|
|
if (that.from_ == kNone)
|
|
|
|
return *this;
|
|
|
|
else if (from_ == kNone)
|
|
|
|
return that;
|
|
|
|
else
|
|
|
|
return Interval(Min(from_, that.from_), Max(to_, that.to_));
|
|
|
|
}
|
|
|
|
bool Contains(int value) {
|
|
|
|
return (from_ <= value) && (value <= to_);
|
|
|
|
}
|
|
|
|
bool is_empty() { return from_ == kNone; }
|
2012-04-17 08:06:53 +00:00
|
|
|
int from() const { return from_; }
|
|
|
|
int to() const { return to_; }
|
2009-01-14 11:32:23 +00:00
|
|
|
static Interval Empty() { return Interval(); }
|
|
|
|
static const int kNone = -1;
|
|
|
|
private:
|
|
|
|
int from_;
|
|
|
|
int to_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-11-25 11:07:48 +00:00
|
|
|
class SeqRegExpNode: public RegExpNode {
|
|
|
|
public:
|
|
|
|
explicit SeqRegExpNode(RegExpNode* on_success)
|
2012-06-04 14:42:58 +00:00
|
|
|
: RegExpNode(on_success->zone()), on_success_(on_success) { }
|
2008-11-25 11:07:48 +00:00
|
|
|
RegExpNode* on_success() { return on_success_; }
|
|
|
|
void set_on_success(RegExpNode* node) { on_success_ = node; }
|
2013-01-09 10:30:54 +00:00
|
|
|
virtual RegExpNode* FilterASCII(int depth, bool ignore_case);
|
2012-05-31 11:59:04 +00:00
|
|
|
virtual void FillInBMInfo(int offset,
|
2012-06-01 11:28:52 +00:00
|
|
|
int budget,
|
2012-05-31 11:59:04 +00:00
|
|
|
BoyerMooreLookahead* bm,
|
|
|
|
bool not_at_start) {
|
2013-03-01 14:50:14 +00:00
|
|
|
on_success_->FillInBMInfo(offset, budget - 1, bm, not_at_start);
|
2012-04-17 08:06:53 +00:00
|
|
|
if (offset == 0) set_bm_info(not_at_start, bm);
|
2012-04-02 09:38:07 +00:00
|
|
|
}
|
2012-04-26 09:11:19 +00:00
|
|
|
|
|
|
|
protected:
|
2013-01-09 10:30:54 +00:00
|
|
|
RegExpNode* FilterSuccessor(int depth, bool ignore_case);
|
2012-04-26 09:11:19 +00:00
|
|
|
|
2008-11-25 11:07:48 +00:00
|
|
|
private:
|
|
|
|
RegExpNode* on_success_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class ActionNode: public SeqRegExpNode {
|
|
|
|
public:
|
2013-06-06 13:28:22 +00:00
|
|
|
enum ActionType {
|
2008-12-08 09:22:12 +00:00
|
|
|
SET_REGISTER,
|
2008-11-25 11:07:48 +00:00
|
|
|
INCREMENT_REGISTER,
|
|
|
|
STORE_POSITION,
|
|
|
|
BEGIN_SUBMATCH,
|
2009-01-08 12:40:47 +00:00
|
|
|
POSITIVE_SUBMATCH_SUCCESS,
|
2009-01-14 11:32:23 +00:00
|
|
|
EMPTY_MATCH_CHECK,
|
|
|
|
CLEAR_CAPTURES
|
2008-11-25 11:07:48 +00:00
|
|
|
};
|
2008-12-08 09:22:12 +00:00
|
|
|
static ActionNode* SetRegister(int reg, int val, RegExpNode* on_success);
|
2008-11-25 11:07:48 +00:00
|
|
|
static ActionNode* IncrementRegister(int reg, RegExpNode* on_success);
|
2009-01-26 14:38:17 +00:00
|
|
|
static ActionNode* StorePosition(int reg,
|
|
|
|
bool is_capture,
|
|
|
|
RegExpNode* on_success);
|
2009-01-14 11:32:23 +00:00
|
|
|
static ActionNode* ClearCaptures(Interval range, RegExpNode* on_success);
|
|
|
|
static ActionNode* BeginSubmatch(int stack_pointer_reg,
|
|
|
|
int position_reg,
|
|
|
|
RegExpNode* on_success);
|
|
|
|
static ActionNode* PositiveSubmatchSuccess(int stack_pointer_reg,
|
|
|
|
int restore_reg,
|
2009-01-26 14:38:17 +00:00
|
|
|
int clear_capture_count,
|
|
|
|
int clear_capture_from,
|
2009-01-14 11:32:23 +00:00
|
|
|
RegExpNode* on_success);
|
|
|
|
static ActionNode* EmptyMatchCheck(int start_register,
|
|
|
|
int repetition_register,
|
|
|
|
int repetition_limit,
|
|
|
|
RegExpNode* on_success);
|
2008-11-25 11:07:48 +00:00
|
|
|
virtual void Accept(NodeVisitor* visitor);
|
2009-01-26 20:09:35 +00:00
|
|
|
virtual void Emit(RegExpCompiler* compiler, Trace* trace);
|
2013-03-01 14:50:14 +00:00
|
|
|
virtual int EatsAtLeast(int still_to_find, int budget, bool not_at_start);
|
2008-12-19 12:02:34 +00:00
|
|
|
virtual void GetQuickCheckDetails(QuickCheckDetails* details,
|
|
|
|
RegExpCompiler* compiler,
|
2009-02-03 11:43:55 +00:00
|
|
|
int filled_in,
|
|
|
|
bool not_at_start) {
|
|
|
|
return on_success()->GetQuickCheckDetails(
|
|
|
|
details, compiler, filled_in, not_at_start);
|
2008-12-19 12:02:34 +00:00
|
|
|
}
|
2012-05-31 11:59:04 +00:00
|
|
|
virtual void FillInBMInfo(int offset,
|
2012-06-01 11:28:52 +00:00
|
|
|
int budget,
|
2012-05-31 11:59:04 +00:00
|
|
|
BoyerMooreLookahead* bm,
|
|
|
|
bool not_at_start);
|
2013-06-06 13:28:22 +00:00
|
|
|
ActionType action_type() { return action_type_; }
|
2008-12-08 09:22:12 +00:00
|
|
|
// TODO(erikcorry): We should allow some action nodes in greedy loops.
|
|
|
|
virtual int GreedyLoopTextLength() { return kNodeIsTooComplexForGreedyLoops; }
|
2011-09-08 19:57:14 +00:00
|
|
|
|
2008-11-25 11:07:48 +00:00
|
|
|
private:
|
|
|
|
union {
|
|
|
|
struct {
|
|
|
|
int reg;
|
|
|
|
int value;
|
|
|
|
} u_store_register;
|
|
|
|
struct {
|
|
|
|
int reg;
|
|
|
|
} u_increment_register;
|
|
|
|
struct {
|
|
|
|
int reg;
|
2009-01-26 14:38:17 +00:00
|
|
|
bool is_capture;
|
2008-11-25 11:07:48 +00:00
|
|
|
} u_position_register;
|
|
|
|
struct {
|
2008-11-28 08:53:53 +00:00
|
|
|
int stack_pointer_register;
|
|
|
|
int current_position_register;
|
2009-01-26 14:38:17 +00:00
|
|
|
int clear_register_count;
|
|
|
|
int clear_register_from;
|
2008-11-28 08:53:53 +00:00
|
|
|
} u_submatch;
|
2009-01-08 12:40:47 +00:00
|
|
|
struct {
|
|
|
|
int start_register;
|
|
|
|
int repetition_register;
|
|
|
|
int repetition_limit;
|
|
|
|
} u_empty_match_check;
|
2009-01-14 11:32:23 +00:00
|
|
|
struct {
|
|
|
|
int range_from;
|
|
|
|
int range_to;
|
|
|
|
} u_clear_captures;
|
2008-11-25 11:07:48 +00:00
|
|
|
} data_;
|
2013-06-06 13:28:22 +00:00
|
|
|
ActionNode(ActionType action_type, RegExpNode* on_success)
|
2008-11-26 08:03:55 +00:00
|
|
|
: SeqRegExpNode(on_success),
|
2013-06-06 13:28:22 +00:00
|
|
|
action_type_(action_type) { }
|
|
|
|
ActionType action_type_;
|
2008-11-25 11:07:48 +00:00
|
|
|
friend class DotPrinter;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class TextNode: public SeqRegExpNode {
|
|
|
|
public:
|
|
|
|
TextNode(ZoneList<TextElement>* elms,
|
2008-12-08 09:22:12 +00:00
|
|
|
RegExpNode* on_success)
|
2008-11-26 08:03:55 +00:00
|
|
|
: SeqRegExpNode(on_success),
|
|
|
|
elms_(elms) { }
|
2008-12-01 15:42:35 +00:00
|
|
|
TextNode(RegExpCharacterClass* that,
|
2008-12-08 09:22:12 +00:00
|
|
|
RegExpNode* on_success)
|
2008-12-01 15:42:35 +00:00
|
|
|
: SeqRegExpNode(on_success),
|
2012-06-11 12:42:31 +00:00
|
|
|
elms_(new(zone()) ZoneList<TextElement>(1, zone())) {
|
2012-06-04 14:42:58 +00:00
|
|
|
elms_->Add(TextElement::CharClass(that), zone());
|
2008-12-01 15:42:35 +00:00
|
|
|
}
|
2008-11-25 11:07:48 +00:00
|
|
|
virtual void Accept(NodeVisitor* visitor);
|
2009-01-26 20:09:35 +00:00
|
|
|
virtual void Emit(RegExpCompiler* compiler, Trace* trace);
|
2013-03-01 14:50:14 +00:00
|
|
|
virtual int EatsAtLeast(int still_to_find, int budget, bool not_at_start);
|
2008-12-19 12:02:34 +00:00
|
|
|
virtual void GetQuickCheckDetails(QuickCheckDetails* details,
|
|
|
|
RegExpCompiler* compiler,
|
2009-02-03 11:43:55 +00:00
|
|
|
int characters_filled_in,
|
|
|
|
bool not_at_start);
|
2008-11-25 11:07:48 +00:00
|
|
|
ZoneList<TextElement>* elements() { return elms_; }
|
2009-11-09 10:01:23 +00:00
|
|
|
void MakeCaseIndependent(bool is_ascii);
|
2008-12-08 09:22:12 +00:00
|
|
|
virtual int GreedyLoopTextLength();
|
2012-04-02 09:38:07 +00:00
|
|
|
virtual RegExpNode* GetSuccessorOfOmnivorousTextNode(
|
|
|
|
RegExpCompiler* compiler);
|
2012-05-31 11:59:04 +00:00
|
|
|
virtual void FillInBMInfo(int offset,
|
2012-06-01 11:28:52 +00:00
|
|
|
int budget,
|
2012-05-31 11:59:04 +00:00
|
|
|
BoyerMooreLookahead* bm,
|
|
|
|
bool not_at_start);
|
2008-12-08 09:22:12 +00:00
|
|
|
void CalculateOffsets();
|
2013-01-09 10:30:54 +00:00
|
|
|
virtual RegExpNode* FilterASCII(int depth, bool ignore_case);
|
2011-09-08 19:57:14 +00:00
|
|
|
|
2008-12-18 14:30:53 +00:00
|
|
|
private:
|
2008-12-19 12:02:34 +00:00
|
|
|
enum TextEmitPassType {
|
2009-02-18 16:07:03 +00:00
|
|
|
NON_ASCII_MATCH, // Check for characters that can't match.
|
|
|
|
SIMPLE_CHARACTER_MATCH, // Case-dependent single character check.
|
|
|
|
NON_LETTER_CHARACTER_MATCH, // Check characters that have no case equivs.
|
|
|
|
CASE_CHARACTER_MATCH, // Case-independent single character check.
|
|
|
|
CHARACTER_CLASS_MATCH // Character class.
|
2008-12-19 12:02:34 +00:00
|
|
|
};
|
2009-02-18 16:07:03 +00:00
|
|
|
static bool SkipPass(int pass, bool ignore_case);
|
|
|
|
static const int kFirstRealPass = SIMPLE_CHARACTER_MATCH;
|
|
|
|
static const int kLastPass = CHARACTER_CLASS_MATCH;
|
2008-12-19 12:02:34 +00:00
|
|
|
void TextEmitPass(RegExpCompiler* compiler,
|
|
|
|
TextEmitPassType pass,
|
|
|
|
bool preloaded,
|
2009-01-15 12:45:48 +00:00
|
|
|
Trace* trace,
|
2008-12-19 12:02:34 +00:00
|
|
|
bool first_element_checked,
|
|
|
|
int* checked_up_to);
|
|
|
|
int Length();
|
2008-11-25 11:07:48 +00:00
|
|
|
ZoneList<TextElement>* elms_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-01-19 18:56:47 +00:00
|
|
|
class AssertionNode: public SeqRegExpNode {
|
|
|
|
public:
|
2013-06-06 13:28:22 +00:00
|
|
|
enum AssertionType {
|
2009-01-19 18:56:47 +00:00
|
|
|
AT_END,
|
|
|
|
AT_START,
|
|
|
|
AT_BOUNDARY,
|
|
|
|
AT_NON_BOUNDARY,
|
2012-04-17 08:06:53 +00:00
|
|
|
AFTER_NEWLINE
|
2009-01-19 18:56:47 +00:00
|
|
|
};
|
|
|
|
static AssertionNode* AtEnd(RegExpNode* on_success) {
|
2012-06-11 12:42:31 +00:00
|
|
|
return new(on_success->zone()) AssertionNode(AT_END, on_success);
|
2009-01-19 18:56:47 +00:00
|
|
|
}
|
|
|
|
static AssertionNode* AtStart(RegExpNode* on_success) {
|
2012-06-11 12:42:31 +00:00
|
|
|
return new(on_success->zone()) AssertionNode(AT_START, on_success);
|
2009-01-19 18:56:47 +00:00
|
|
|
}
|
|
|
|
static AssertionNode* AtBoundary(RegExpNode* on_success) {
|
2012-06-11 12:42:31 +00:00
|
|
|
return new(on_success->zone()) AssertionNode(AT_BOUNDARY, on_success);
|
2009-01-19 18:56:47 +00:00
|
|
|
}
|
|
|
|
static AssertionNode* AtNonBoundary(RegExpNode* on_success) {
|
2012-06-11 12:42:31 +00:00
|
|
|
return new(on_success->zone()) AssertionNode(AT_NON_BOUNDARY, on_success);
|
2009-01-19 18:56:47 +00:00
|
|
|
}
|
|
|
|
static AssertionNode* AfterNewline(RegExpNode* on_success) {
|
2012-06-11 12:42:31 +00:00
|
|
|
return new(on_success->zone()) AssertionNode(AFTER_NEWLINE, on_success);
|
2009-01-19 18:56:47 +00:00
|
|
|
}
|
|
|
|
virtual void Accept(NodeVisitor* visitor);
|
2009-01-26 20:09:35 +00:00
|
|
|
virtual void Emit(RegExpCompiler* compiler, Trace* trace);
|
2013-03-01 14:50:14 +00:00
|
|
|
virtual int EatsAtLeast(int still_to_find, int budget, bool not_at_start);
|
2009-01-19 18:56:47 +00:00
|
|
|
virtual void GetQuickCheckDetails(QuickCheckDetails* details,
|
|
|
|
RegExpCompiler* compiler,
|
2009-02-03 11:43:55 +00:00
|
|
|
int filled_in,
|
|
|
|
bool not_at_start);
|
2012-05-31 11:59:04 +00:00
|
|
|
virtual void FillInBMInfo(int offset,
|
2012-06-01 11:28:52 +00:00
|
|
|
int budget,
|
2012-05-31 11:59:04 +00:00
|
|
|
BoyerMooreLookahead* bm,
|
|
|
|
bool not_at_start);
|
2013-06-06 13:28:22 +00:00
|
|
|
AssertionType assertion_type() { return assertion_type_; }
|
2011-09-08 19:57:14 +00:00
|
|
|
|
2009-01-19 18:56:47 +00:00
|
|
|
private:
|
2012-04-17 08:06:53 +00:00
|
|
|
void EmitBoundaryCheck(RegExpCompiler* compiler, Trace* trace);
|
|
|
|
enum IfPrevious { kIsNonWord, kIsWord };
|
|
|
|
void BacktrackIfPrevious(RegExpCompiler* compiler,
|
|
|
|
Trace* trace,
|
|
|
|
IfPrevious backtrack_if_previous);
|
2013-06-06 13:28:22 +00:00
|
|
|
AssertionNode(AssertionType t, RegExpNode* on_success)
|
|
|
|
: SeqRegExpNode(on_success), assertion_type_(t) { }
|
|
|
|
AssertionType assertion_type_;
|
2009-01-19 18:56:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-11-25 11:07:48 +00:00
|
|
|
class BackReferenceNode: public SeqRegExpNode {
|
|
|
|
public:
|
|
|
|
BackReferenceNode(int start_reg,
|
|
|
|
int end_reg,
|
2008-12-08 09:22:12 +00:00
|
|
|
RegExpNode* on_success)
|
2008-11-26 08:03:55 +00:00
|
|
|
: SeqRegExpNode(on_success),
|
|
|
|
start_reg_(start_reg),
|
|
|
|
end_reg_(end_reg) { }
|
2008-11-25 11:07:48 +00:00
|
|
|
virtual void Accept(NodeVisitor* visitor);
|
|
|
|
int start_register() { return start_reg_; }
|
|
|
|
int end_register() { return end_reg_; }
|
2009-01-26 20:09:35 +00:00
|
|
|
virtual void Emit(RegExpCompiler* compiler, Trace* trace);
|
2010-12-03 09:54:06 +00:00
|
|
|
virtual int EatsAtLeast(int still_to_find,
|
|
|
|
int recursion_depth,
|
|
|
|
bool not_at_start);
|
2008-12-19 12:02:34 +00:00
|
|
|
virtual void GetQuickCheckDetails(QuickCheckDetails* details,
|
|
|
|
RegExpCompiler* compiler,
|
2009-02-03 11:43:55 +00:00
|
|
|
int characters_filled_in,
|
|
|
|
bool not_at_start) {
|
2008-12-19 12:02:34 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-05-31 11:59:04 +00:00
|
|
|
virtual void FillInBMInfo(int offset,
|
2012-06-01 11:28:52 +00:00
|
|
|
int budget,
|
2012-05-31 11:59:04 +00:00
|
|
|
BoyerMooreLookahead* bm,
|
|
|
|
bool not_at_start);
|
2011-09-08 19:57:14 +00:00
|
|
|
|
2008-11-25 11:07:48 +00:00
|
|
|
private:
|
|
|
|
int start_reg_;
|
|
|
|
int end_reg_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class EndNode: public RegExpNode {
|
|
|
|
public:
|
2008-12-08 09:22:12 +00:00
|
|
|
enum Action { ACCEPT, BACKTRACK, NEGATIVE_SUBMATCH_SUCCESS };
|
2012-06-04 14:42:58 +00:00
|
|
|
explicit EndNode(Action action, Zone* zone)
|
|
|
|
: RegExpNode(zone), action_(action) { }
|
2008-11-25 11:07:48 +00:00
|
|
|
virtual void Accept(NodeVisitor* visitor);
|
2009-01-26 20:09:35 +00:00
|
|
|
virtual void Emit(RegExpCompiler* compiler, Trace* trace);
|
2010-12-03 09:54:06 +00:00
|
|
|
virtual int EatsAtLeast(int still_to_find,
|
|
|
|
int recursion_depth,
|
|
|
|
bool not_at_start) { return 0; }
|
2008-12-19 12:02:34 +00:00
|
|
|
virtual void GetQuickCheckDetails(QuickCheckDetails* details,
|
|
|
|
RegExpCompiler* compiler,
|
2009-02-03 11:43:55 +00:00
|
|
|
int characters_filled_in,
|
|
|
|
bool not_at_start) {
|
2008-12-19 12:02:34 +00:00
|
|
|
// Returning 0 from EatsAtLeast should ensure we never get here.
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
2012-05-31 11:59:04 +00:00
|
|
|
virtual void FillInBMInfo(int offset,
|
2012-06-01 11:28:52 +00:00
|
|
|
int budget,
|
2012-05-31 11:59:04 +00:00
|
|
|
BoyerMooreLookahead* bm,
|
|
|
|
bool not_at_start) {
|
2012-04-02 09:38:07 +00:00
|
|
|
// Returning 0 from EatsAtLeast should ensure we never get here.
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
2012-04-26 09:11:19 +00:00
|
|
|
|
2008-11-25 11:07:48 +00:00
|
|
|
private:
|
|
|
|
Action action_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-12-08 09:22:12 +00:00
|
|
|
class NegativeSubmatchSuccess: public EndNode {
|
|
|
|
public:
|
2009-01-26 14:38:17 +00:00
|
|
|
NegativeSubmatchSuccess(int stack_pointer_reg,
|
|
|
|
int position_reg,
|
|
|
|
int clear_capture_count,
|
2012-06-04 14:42:58 +00:00
|
|
|
int clear_capture_start,
|
|
|
|
Zone* zone)
|
|
|
|
: EndNode(NEGATIVE_SUBMATCH_SUCCESS, zone),
|
2008-12-08 09:22:12 +00:00
|
|
|
stack_pointer_register_(stack_pointer_reg),
|
2009-01-26 14:38:17 +00:00
|
|
|
current_position_register_(position_reg),
|
|
|
|
clear_capture_count_(clear_capture_count),
|
|
|
|
clear_capture_start_(clear_capture_start) { }
|
2009-01-26 20:09:35 +00:00
|
|
|
virtual void Emit(RegExpCompiler* compiler, Trace* trace);
|
2008-12-08 09:22:12 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
int stack_pointer_register_;
|
|
|
|
int current_position_register_;
|
2009-01-26 14:38:17 +00:00
|
|
|
int clear_capture_count_;
|
|
|
|
int clear_capture_start_;
|
2008-12-08 09:22:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-11-25 11:07:48 +00:00
|
|
|
class Guard: public ZoneObject {
|
|
|
|
public:
|
|
|
|
enum Relation { LT, GEQ };
|
|
|
|
Guard(int reg, Relation op, int value)
|
2008-11-26 08:03:55 +00:00
|
|
|
: reg_(reg),
|
|
|
|
op_(op),
|
|
|
|
value_(value) { }
|
2008-11-25 11:07:48 +00:00
|
|
|
int reg() { return reg_; }
|
|
|
|
Relation op() { return op_; }
|
|
|
|
int value() { return value_; }
|
2008-12-01 15:42:35 +00:00
|
|
|
|
2008-11-25 11:07:48 +00:00
|
|
|
private:
|
|
|
|
int reg_;
|
|
|
|
Relation op_;
|
|
|
|
int value_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class GuardedAlternative {
|
|
|
|
public:
|
|
|
|
explicit GuardedAlternative(RegExpNode* node) : node_(node), guards_(NULL) { }
|
2012-06-11 12:42:31 +00:00
|
|
|
void AddGuard(Guard* guard, Zone* zone);
|
2008-11-25 11:07:48 +00:00
|
|
|
RegExpNode* node() { return node_; }
|
|
|
|
void set_node(RegExpNode* node) { node_ = node; }
|
|
|
|
ZoneList<Guard*>* guards() { return guards_; }
|
2008-12-01 15:42:35 +00:00
|
|
|
|
2008-11-25 11:07:48 +00:00
|
|
|
private:
|
|
|
|
RegExpNode* node_;
|
|
|
|
ZoneList<Guard*>* guards_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-12-19 12:02:34 +00:00
|
|
|
class AlternativeGeneration;
|
|
|
|
|
|
|
|
|
2008-11-25 11:07:48 +00:00
|
|
|
class ChoiceNode: public RegExpNode {
|
|
|
|
public:
|
2012-06-04 14:42:58 +00:00
|
|
|
explicit ChoiceNode(int expected_size, Zone* zone)
|
|
|
|
: RegExpNode(zone),
|
2012-06-11 12:42:31 +00:00
|
|
|
alternatives_(new(zone)
|
|
|
|
ZoneList<GuardedAlternative>(expected_size, zone)),
|
2008-12-01 15:42:35 +00:00
|
|
|
table_(NULL),
|
2009-02-03 11:43:55 +00:00
|
|
|
not_at_start_(false),
|
2008-11-26 08:03:55 +00:00
|
|
|
being_calculated_(false) { }
|
2008-11-25 11:07:48 +00:00
|
|
|
virtual void Accept(NodeVisitor* visitor);
|
2012-06-04 14:42:58 +00:00
|
|
|
void AddAlternative(GuardedAlternative node) {
|
|
|
|
alternatives()->Add(node, zone());
|
|
|
|
}
|
2008-11-25 11:07:48 +00:00
|
|
|
ZoneList<GuardedAlternative>* alternatives() { return alternatives_; }
|
2008-12-01 15:42:35 +00:00
|
|
|
DispatchTable* GetTable(bool ignore_case);
|
2009-01-26 20:09:35 +00:00
|
|
|
virtual void Emit(RegExpCompiler* compiler, Trace* trace);
|
2013-03-01 14:50:14 +00:00
|
|
|
virtual int EatsAtLeast(int still_to_find, int budget, bool not_at_start);
|
2009-01-26 19:38:26 +00:00
|
|
|
int EatsAtLeastHelper(int still_to_find,
|
2013-03-01 14:50:14 +00:00
|
|
|
int budget,
|
2010-12-03 09:54:06 +00:00
|
|
|
RegExpNode* ignore_this_node,
|
|
|
|
bool not_at_start);
|
2008-12-19 12:02:34 +00:00
|
|
|
virtual void GetQuickCheckDetails(QuickCheckDetails* details,
|
|
|
|
RegExpCompiler* compiler,
|
2009-02-03 11:43:55 +00:00
|
|
|
int characters_filled_in,
|
|
|
|
bool not_at_start);
|
2012-05-31 11:59:04 +00:00
|
|
|
virtual void FillInBMInfo(int offset,
|
2012-06-01 11:28:52 +00:00
|
|
|
int budget,
|
2012-05-31 11:59:04 +00:00
|
|
|
BoyerMooreLookahead* bm,
|
|
|
|
bool not_at_start);
|
2008-12-01 15:42:35 +00:00
|
|
|
|
2008-11-25 11:07:48 +00:00
|
|
|
bool being_calculated() { return being_calculated_; }
|
2009-02-03 11:43:55 +00:00
|
|
|
bool not_at_start() { return not_at_start_; }
|
|
|
|
void set_not_at_start() { not_at_start_ = true; }
|
2008-11-25 11:07:48 +00:00
|
|
|
void set_being_calculated(bool b) { being_calculated_ = b; }
|
2014-09-04 12:47:13 +00:00
|
|
|
virtual bool try_to_emit_quick_check_for_alternative(bool is_first) {
|
|
|
|
return true;
|
|
|
|
}
|
2013-01-09 10:30:54 +00:00
|
|
|
virtual RegExpNode* FilterASCII(int depth, bool ignore_case);
|
2008-12-01 15:42:35 +00:00
|
|
|
|
2008-12-08 09:22:12 +00:00
|
|
|
protected:
|
2011-09-06 07:41:45 +00:00
|
|
|
int GreedyLoopTextLengthForAlternative(GuardedAlternative* alternative);
|
2008-12-08 09:22:12 +00:00
|
|
|
ZoneList<GuardedAlternative>* alternatives_;
|
|
|
|
|
2008-11-25 11:07:48 +00:00
|
|
|
private:
|
2008-12-01 15:42:35 +00:00
|
|
|
friend class DispatchTableConstructor;
|
2008-12-18 14:30:53 +00:00
|
|
|
friend class Analysis;
|
2008-11-25 11:07:48 +00:00
|
|
|
void GenerateGuard(RegExpMacroAssembler* macro_assembler,
|
2009-04-27 09:27:18 +00:00
|
|
|
Guard* guard,
|
2009-01-15 12:45:48 +00:00
|
|
|
Trace* trace);
|
2012-04-02 09:38:07 +00:00
|
|
|
int CalculatePreloadCharacters(RegExpCompiler* compiler, int eats_at_least);
|
2009-01-26 20:09:35 +00:00
|
|
|
void EmitOutOfLineContinuation(RegExpCompiler* compiler,
|
2009-01-15 12:45:48 +00:00
|
|
|
Trace* trace,
|
2008-12-19 12:02:34 +00:00
|
|
|
GuardedAlternative alternative,
|
|
|
|
AlternativeGeneration* alt_gen,
|
|
|
|
int preload_characters,
|
|
|
|
bool next_expects_preload);
|
2014-09-04 12:47:13 +00:00
|
|
|
void SetUpPreLoad(RegExpCompiler* compiler,
|
|
|
|
Trace* current_trace,
|
|
|
|
PreloadState* preloads);
|
|
|
|
void AssertGuardsMentionRegisters(Trace* trace);
|
|
|
|
int EmitOptimizedUnanchoredSearch(RegExpCompiler* compiler, Trace* trace);
|
|
|
|
Trace* EmitGreedyLoop(RegExpCompiler* compiler,
|
|
|
|
Trace* trace,
|
|
|
|
AlternativeGenerationList* alt_gens,
|
|
|
|
PreloadState* preloads,
|
|
|
|
GreedyLoopState* greedy_loop_state,
|
|
|
|
int text_length);
|
|
|
|
void EmitChoices(RegExpCompiler* compiler,
|
|
|
|
AlternativeGenerationList* alt_gens,
|
|
|
|
int first_choice,
|
|
|
|
Trace* trace,
|
|
|
|
bool is_greedy_loop,
|
|
|
|
PreloadState* preloads);
|
2008-12-01 15:42:35 +00:00
|
|
|
DispatchTable* table_;
|
2009-02-03 11:43:55 +00:00
|
|
|
// If true, this node is never checked at the start of the input.
|
|
|
|
// Allows a new trace to start with at_start() set to false.
|
|
|
|
bool not_at_start_;
|
2008-11-25 11:07:48 +00:00
|
|
|
bool being_calculated_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-01-20 11:36:28 +00:00
|
|
|
class NegativeLookaheadChoiceNode: public ChoiceNode {
|
|
|
|
public:
|
|
|
|
explicit NegativeLookaheadChoiceNode(GuardedAlternative this_must_fail,
|
2012-06-04 14:42:58 +00:00
|
|
|
GuardedAlternative then_do_this,
|
|
|
|
Zone* zone)
|
|
|
|
: ChoiceNode(2, zone) {
|
2009-01-20 11:36:28 +00:00
|
|
|
AddAlternative(this_must_fail);
|
|
|
|
AddAlternative(then_do_this);
|
|
|
|
}
|
2013-03-01 14:50:14 +00:00
|
|
|
virtual int EatsAtLeast(int still_to_find, int budget, bool not_at_start);
|
2009-01-20 11:36:28 +00:00
|
|
|
virtual void GetQuickCheckDetails(QuickCheckDetails* details,
|
|
|
|
RegExpCompiler* compiler,
|
2009-02-03 11:43:55 +00:00
|
|
|
int characters_filled_in,
|
|
|
|
bool not_at_start);
|
2012-05-31 11:59:04 +00:00
|
|
|
virtual void FillInBMInfo(int offset,
|
2012-06-01 11:28:52 +00:00
|
|
|
int budget,
|
2012-05-31 11:59:04 +00:00
|
|
|
BoyerMooreLookahead* bm,
|
|
|
|
bool not_at_start) {
|
|
|
|
alternatives_->at(1).node()->FillInBMInfo(
|
2013-03-01 14:50:14 +00:00
|
|
|
offset, budget - 1, bm, not_at_start);
|
2012-04-17 08:06:53 +00:00
|
|
|
if (offset == 0) set_bm_info(not_at_start, bm);
|
2012-04-02 09:38:07 +00:00
|
|
|
}
|
2009-01-20 11:36:28 +00:00
|
|
|
// For a negative lookahead we don't emit the quick check for the
|
|
|
|
// alternative that is expected to fail. This is because quick check code
|
|
|
|
// starts by loading enough characters for the alternative that takes fewest
|
|
|
|
// characters, but on a negative lookahead the negative branch did not take
|
|
|
|
// part in that calculation (EatsAtLeast) so the assumptions don't hold.
|
2014-09-04 12:47:13 +00:00
|
|
|
virtual bool try_to_emit_quick_check_for_alternative(bool is_first) {
|
|
|
|
return !is_first;
|
|
|
|
}
|
2013-01-09 10:30:54 +00:00
|
|
|
virtual RegExpNode* FilterASCII(int depth, bool ignore_case);
|
2009-01-20 11:36:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-12-08 09:22:12 +00:00
|
|
|
class LoopChoiceNode: public ChoiceNode {
|
|
|
|
public:
|
2012-06-04 14:42:58 +00:00
|
|
|
explicit LoopChoiceNode(bool body_can_be_zero_length, Zone* zone)
|
|
|
|
: ChoiceNode(2, zone),
|
2008-12-17 13:16:38 +00:00
|
|
|
loop_node_(NULL),
|
2008-12-19 12:02:34 +00:00
|
|
|
continue_node_(NULL),
|
2014-09-04 12:47:13 +00:00
|
|
|
body_can_be_zero_length_(body_can_be_zero_length)
|
|
|
|
{ }
|
2008-12-17 13:16:38 +00:00
|
|
|
void AddLoopAlternative(GuardedAlternative alt);
|
|
|
|
void AddContinueAlternative(GuardedAlternative alt);
|
2009-01-26 20:09:35 +00:00
|
|
|
virtual void Emit(RegExpCompiler* compiler, Trace* trace);
|
2013-03-01 14:50:14 +00:00
|
|
|
virtual int EatsAtLeast(int still_to_find, int budget, bool not_at_start);
|
2008-12-19 12:02:34 +00:00
|
|
|
virtual void GetQuickCheckDetails(QuickCheckDetails* details,
|
|
|
|
RegExpCompiler* compiler,
|
2009-02-03 11:43:55 +00:00
|
|
|
int characters_filled_in,
|
|
|
|
bool not_at_start);
|
2012-05-31 11:59:04 +00:00
|
|
|
virtual void FillInBMInfo(int offset,
|
2012-06-01 11:28:52 +00:00
|
|
|
int budget,
|
2012-05-31 11:59:04 +00:00
|
|
|
BoyerMooreLookahead* bm,
|
|
|
|
bool not_at_start);
|
2008-12-17 13:16:38 +00:00
|
|
|
RegExpNode* loop_node() { return loop_node_; }
|
|
|
|
RegExpNode* continue_node() { return continue_node_; }
|
2008-12-19 12:02:34 +00:00
|
|
|
bool body_can_be_zero_length() { return body_can_be_zero_length_; }
|
2008-12-17 13:16:38 +00:00
|
|
|
virtual void Accept(NodeVisitor* visitor);
|
2013-01-09 10:30:54 +00:00
|
|
|
virtual RegExpNode* FilterASCII(int depth, bool ignore_case);
|
2008-12-17 13:16:38 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
// AddAlternative is made private for loop nodes because alternatives
|
|
|
|
// should not be added freely, we need to keep track of which node
|
|
|
|
// goes back to the node itself.
|
|
|
|
void AddAlternative(GuardedAlternative node) {
|
|
|
|
ChoiceNode::AddAlternative(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
RegExpNode* loop_node_;
|
|
|
|
RegExpNode* continue_node_;
|
2008-12-19 12:02:34 +00:00
|
|
|
bool body_can_be_zero_length_;
|
2008-12-08 09:22:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-04-17 08:06:53 +00:00
|
|
|
// Improve the speed that we scan for an initial point where a non-anchored
|
|
|
|
// regexp can match by using a Boyer-Moore-like table. This is done by
|
|
|
|
// identifying non-greedy non-capturing loops in the nodes that eat any
|
|
|
|
// character one at a time. For example in the middle of the regexp
|
|
|
|
// /foo[\s\S]*?bar/ we find such a loop. There is also such a loop implicitly
|
|
|
|
// inserted at the start of any non-anchored regexp.
|
|
|
|
//
|
|
|
|
// When we have found such a loop we look ahead in the nodes to find the set of
|
|
|
|
// characters that can come at given distances. For example for the regexp
|
|
|
|
// /.?foo/ we know that there are at least 3 characters ahead of us, and the
|
|
|
|
// sets of characters that can occur are [any, [f, o], [o]]. We find a range in
|
|
|
|
// the lookahead info where the set of characters is reasonably constrained. In
|
|
|
|
// our example this is from index 1 to 2 (0 is not constrained). We can now
|
|
|
|
// look 3 characters ahead and if we don't find one of [f, o] (the union of
|
|
|
|
// [f, o] and [o]) then we can skip forwards by the range size (in this case 2).
|
|
|
|
//
|
|
|
|
// For Unicode input strings we do the same, but modulo 128.
|
|
|
|
//
|
|
|
|
// We also look at the first string fed to the regexp and use that to get a hint
|
|
|
|
// of the character frequencies in the inputs. This affects the assessment of
|
|
|
|
// whether the set of characters is 'reasonably constrained'.
|
|
|
|
//
|
|
|
|
// We also have another lookahead mechanism (called quick check in the code),
|
|
|
|
// which uses a wide load of multiple characters followed by a mask and compare
|
|
|
|
// to determine whether a match is possible at this point.
|
|
|
|
enum ContainedInLattice {
|
|
|
|
kNotYet = 0,
|
|
|
|
kLatticeIn = 1,
|
|
|
|
kLatticeOut = 2,
|
|
|
|
kLatticeUnknown = 3 // Can also mean both in and out.
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
inline ContainedInLattice Combine(ContainedInLattice a, ContainedInLattice b) {
|
|
|
|
return static_cast<ContainedInLattice>(a | b);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ContainedInLattice AddRange(ContainedInLattice a,
|
|
|
|
const int* ranges,
|
|
|
|
int ranges_size,
|
|
|
|
Interval new_range);
|
|
|
|
|
|
|
|
|
|
|
|
class BoyerMoorePositionInfo : public ZoneObject {
|
|
|
|
public:
|
2012-06-04 14:42:58 +00:00
|
|
|
explicit BoyerMoorePositionInfo(Zone* zone)
|
2012-06-11 12:42:31 +00:00
|
|
|
: map_(new(zone) ZoneList<bool>(kMapSize, zone)),
|
2012-04-17 08:06:53 +00:00
|
|
|
map_count_(0),
|
|
|
|
w_(kNotYet),
|
|
|
|
s_(kNotYet),
|
|
|
|
d_(kNotYet),
|
|
|
|
surrogate_(kNotYet) {
|
|
|
|
for (int i = 0; i < kMapSize; i++) {
|
2012-06-04 14:42:58 +00:00
|
|
|
map_->Add(false, zone);
|
2012-04-17 08:06:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool& at(int i) { return map_->at(i); }
|
|
|
|
|
|
|
|
static const int kMapSize = 128;
|
|
|
|
static const int kMask = kMapSize - 1;
|
|
|
|
|
|
|
|
int map_count() const { return map_count_; }
|
|
|
|
|
|
|
|
void Set(int character);
|
|
|
|
void SetInterval(const Interval& interval);
|
|
|
|
void SetAll();
|
|
|
|
bool is_non_word() { return w_ == kLatticeOut; }
|
|
|
|
bool is_word() { return w_ == kLatticeIn; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
ZoneList<bool>* map_;
|
|
|
|
int map_count_; // Number of set bits in the map.
|
|
|
|
ContainedInLattice w_; // The \w character class.
|
|
|
|
ContainedInLattice s_; // The \s character class.
|
|
|
|
ContainedInLattice d_; // The \d character class.
|
|
|
|
ContainedInLattice surrogate_; // Surrogate UTF-16 code units.
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class BoyerMooreLookahead : public ZoneObject {
|
|
|
|
public:
|
2012-06-04 14:42:58 +00:00
|
|
|
BoyerMooreLookahead(int length, RegExpCompiler* compiler, Zone* zone);
|
2012-04-17 08:06:53 +00:00
|
|
|
|
|
|
|
int length() { return length_; }
|
|
|
|
int max_char() { return max_char_; }
|
|
|
|
RegExpCompiler* compiler() { return compiler_; }
|
|
|
|
|
|
|
|
int Count(int map_number) {
|
|
|
|
return bitmaps_->at(map_number)->map_count();
|
|
|
|
}
|
|
|
|
|
|
|
|
BoyerMoorePositionInfo* at(int i) { return bitmaps_->at(i); }
|
|
|
|
|
|
|
|
void Set(int map_number, int character) {
|
|
|
|
if (character > max_char_) return;
|
|
|
|
BoyerMoorePositionInfo* info = bitmaps_->at(map_number);
|
|
|
|
info->Set(character);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetInterval(int map_number, const Interval& interval) {
|
|
|
|
if (interval.from() > max_char_) return;
|
|
|
|
BoyerMoorePositionInfo* info = bitmaps_->at(map_number);
|
|
|
|
if (interval.to() > max_char_) {
|
|
|
|
info->SetInterval(Interval(interval.from(), max_char_));
|
|
|
|
} else {
|
|
|
|
info->SetInterval(interval);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetAll(int map_number) {
|
|
|
|
bitmaps_->at(map_number)->SetAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetRest(int from_map) {
|
|
|
|
for (int i = from_map; i < length_; i++) SetAll(i);
|
|
|
|
}
|
2014-09-04 12:47:13 +00:00
|
|
|
void EmitSkipInstructions(RegExpMacroAssembler* masm);
|
2012-04-17 08:06:53 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
// This is the value obtained by EatsAtLeast. If we do not have at least this
|
|
|
|
// many characters left in the sample string then the match is bound to fail.
|
|
|
|
// Therefore it is OK to read a character this far ahead of the current match
|
|
|
|
// point.
|
|
|
|
int length_;
|
|
|
|
RegExpCompiler* compiler_;
|
|
|
|
// 0x7f for ASCII, 0xffff for UTF-16.
|
|
|
|
int max_char_;
|
|
|
|
ZoneList<BoyerMoorePositionInfo*>* bitmaps_;
|
|
|
|
|
|
|
|
int GetSkipTable(int min_lookahead,
|
|
|
|
int max_lookahead,
|
|
|
|
Handle<ByteArray> boolean_skip_table);
|
|
|
|
bool FindWorthwhileInterval(int* from, int* to);
|
|
|
|
int FindBestInterval(
|
|
|
|
int max_number_of_chars, int old_biggest_points, int* from, int* to);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-12-08 09:22:12 +00:00
|
|
|
// There are many ways to generate code for a node. This class encapsulates
|
|
|
|
// the current way we should be generating. In other words it encapsulates
|
2009-01-15 12:45:48 +00:00
|
|
|
// the current state of the code generator. The effect of this is that we
|
|
|
|
// generate code for paths that the matcher can take through the regular
|
|
|
|
// expression. A given node in the regexp can be code-generated several times
|
|
|
|
// as it can be part of several traces. For example for the regexp:
|
|
|
|
// /foo(bar|ip)baz/ the code to match baz will be generated twice, once as part
|
|
|
|
// of the foo-bar-baz trace and once as part of the foo-ip-baz trace. The code
|
|
|
|
// to match foo is generated only once (the traces have a common prefix). The
|
|
|
|
// code to store the capture is deferred and generated (twice) after the places
|
|
|
|
// where baz has been matched.
|
|
|
|
class Trace {
|
2008-12-08 09:22:12 +00:00
|
|
|
public:
|
2009-02-03 11:43:55 +00:00
|
|
|
// A value for a property that is either known to be true, know to be false,
|
|
|
|
// or not known.
|
|
|
|
enum TriBool {
|
2013-07-09 07:19:51 +00:00
|
|
|
UNKNOWN = -1, FALSE_VALUE = 0, TRUE_VALUE = 1
|
2009-02-03 11:43:55 +00:00
|
|
|
};
|
|
|
|
|
2008-12-08 09:22:12 +00:00
|
|
|
class DeferredAction {
|
|
|
|
public:
|
2013-06-06 13:28:22 +00:00
|
|
|
DeferredAction(ActionNode::ActionType action_type, int reg)
|
|
|
|
: action_type_(action_type), reg_(reg), next_(NULL) { }
|
2008-12-08 09:22:12 +00:00
|
|
|
DeferredAction* next() { return next_; }
|
2009-01-14 11:32:23 +00:00
|
|
|
bool Mentions(int reg);
|
2008-12-08 09:22:12 +00:00
|
|
|
int reg() { return reg_; }
|
2013-06-06 13:28:22 +00:00
|
|
|
ActionNode::ActionType action_type() { return action_type_; }
|
2008-12-08 09:22:12 +00:00
|
|
|
private:
|
2013-06-06 13:28:22 +00:00
|
|
|
ActionNode::ActionType action_type_;
|
2008-12-08 09:22:12 +00:00
|
|
|
int reg_;
|
|
|
|
DeferredAction* next_;
|
2009-01-15 12:45:48 +00:00
|
|
|
friend class Trace;
|
2008-12-08 09:22:12 +00:00
|
|
|
};
|
|
|
|
|
2009-01-26 14:38:17 +00:00
|
|
|
class DeferredCapture : public DeferredAction {
|
2008-12-08 09:22:12 +00:00
|
|
|
public:
|
2009-01-26 14:38:17 +00:00
|
|
|
DeferredCapture(int reg, bool is_capture, Trace* trace)
|
2008-12-08 09:22:12 +00:00
|
|
|
: DeferredAction(ActionNode::STORE_POSITION, reg),
|
2009-01-26 15:26:44 +00:00
|
|
|
cp_offset_(trace->cp_offset()),
|
|
|
|
is_capture_(is_capture) { }
|
2008-12-08 09:22:12 +00:00
|
|
|
int cp_offset() { return cp_offset_; }
|
2009-01-26 14:38:17 +00:00
|
|
|
bool is_capture() { return is_capture_; }
|
2008-12-08 09:22:12 +00:00
|
|
|
private:
|
|
|
|
int cp_offset_;
|
2009-01-26 14:38:17 +00:00
|
|
|
bool is_capture_;
|
2008-12-08 09:22:12 +00:00
|
|
|
void set_cp_offset(int cp_offset) { cp_offset_ = cp_offset; }
|
|
|
|
};
|
|
|
|
|
2009-01-26 14:38:17 +00:00
|
|
|
class DeferredSetRegister : public DeferredAction {
|
2008-12-08 09:22:12 +00:00
|
|
|
public:
|
|
|
|
DeferredSetRegister(int reg, int value)
|
|
|
|
: DeferredAction(ActionNode::SET_REGISTER, reg),
|
|
|
|
value_(value) { }
|
|
|
|
int value() { return value_; }
|
|
|
|
private:
|
|
|
|
int value_;
|
|
|
|
};
|
|
|
|
|
2009-01-14 11:32:23 +00:00
|
|
|
class DeferredClearCaptures : public DeferredAction {
|
|
|
|
public:
|
|
|
|
explicit DeferredClearCaptures(Interval range)
|
|
|
|
: DeferredAction(ActionNode::CLEAR_CAPTURES, -1),
|
|
|
|
range_(range) { }
|
|
|
|
Interval range() { return range_; }
|
|
|
|
private:
|
|
|
|
Interval range_;
|
|
|
|
};
|
|
|
|
|
2009-01-26 14:38:17 +00:00
|
|
|
class DeferredIncrementRegister : public DeferredAction {
|
2008-12-08 09:22:12 +00:00
|
|
|
public:
|
|
|
|
explicit DeferredIncrementRegister(int reg)
|
|
|
|
: DeferredAction(ActionNode::INCREMENT_REGISTER, reg) { }
|
|
|
|
};
|
|
|
|
|
2009-01-15 12:45:48 +00:00
|
|
|
Trace()
|
2008-12-08 09:22:12 +00:00
|
|
|
: cp_offset_(0),
|
|
|
|
actions_(NULL),
|
|
|
|
backtrack_(NULL),
|
|
|
|
stop_node_(NULL),
|
2008-12-19 12:02:34 +00:00
|
|
|
loop_label_(NULL),
|
2008-12-22 12:48:14 +00:00
|
|
|
characters_preloaded_(0),
|
2009-02-03 11:43:55 +00:00
|
|
|
bound_checked_up_to_(0),
|
2009-02-19 08:24:28 +00:00
|
|
|
flush_budget_(100),
|
2009-02-03 11:43:55 +00:00
|
|
|
at_start_(UNKNOWN) { }
|
|
|
|
|
2009-01-15 13:09:23 +00:00
|
|
|
// End the trace. This involves flushing the deferred actions in the trace
|
|
|
|
// and pushing a backtrack location onto the backtrack stack. Once this is
|
|
|
|
// done we can start a new trace or go to one that has already been
|
|
|
|
// generated.
|
2009-01-26 20:09:35 +00:00
|
|
|
void Flush(RegExpCompiler* compiler, RegExpNode* successor);
|
2008-12-08 09:22:12 +00:00
|
|
|
int cp_offset() { return cp_offset_; }
|
|
|
|
DeferredAction* actions() { return actions_; }
|
2009-01-15 12:45:48 +00:00
|
|
|
// A trivial trace is one that has no deferred actions or other state that
|
|
|
|
// affects the assumptions used when generating code. There is no recorded
|
|
|
|
// backtrack location in a trivial trace, so with a trivial trace we will
|
|
|
|
// generate code that, on a failure to match, gets the backtrack location
|
|
|
|
// from the backtrack stack rather than using a direct jump instruction. We
|
|
|
|
// always start code generation with a trivial trace and non-trivial traces
|
|
|
|
// are created as we emit code for nodes or add to the list of deferred
|
|
|
|
// actions in the trace. The location of the code generated for a node using
|
|
|
|
// a trivial trace is recorded in a label in the node so that gotos can be
|
|
|
|
// generated to that code.
|
2008-12-08 09:22:12 +00:00
|
|
|
bool is_trivial() {
|
2008-12-19 12:02:34 +00:00
|
|
|
return backtrack_ == NULL &&
|
|
|
|
actions_ == NULL &&
|
|
|
|
cp_offset_ == 0 &&
|
|
|
|
characters_preloaded_ == 0 &&
|
2008-12-22 12:48:14 +00:00
|
|
|
bound_checked_up_to_ == 0 &&
|
2009-02-03 11:43:55 +00:00
|
|
|
quick_check_performed_.characters() == 0 &&
|
|
|
|
at_start_ == UNKNOWN;
|
2008-12-08 09:22:12 +00:00
|
|
|
}
|
2009-02-03 11:43:55 +00:00
|
|
|
TriBool at_start() { return at_start_; }
|
2013-07-09 07:19:51 +00:00
|
|
|
void set_at_start(bool at_start) {
|
|
|
|
at_start_ = at_start ? TRUE_VALUE : FALSE_VALUE;
|
|
|
|
}
|
2008-12-08 09:22:12 +00:00
|
|
|
Label* backtrack() { return backtrack_; }
|
|
|
|
Label* loop_label() { return loop_label_; }
|
|
|
|
RegExpNode* stop_node() { return stop_node_; }
|
2008-12-19 12:02:34 +00:00
|
|
|
int characters_preloaded() { return characters_preloaded_; }
|
2008-12-22 12:48:14 +00:00
|
|
|
int bound_checked_up_to() { return bound_checked_up_to_; }
|
2009-02-19 08:24:28 +00:00
|
|
|
int flush_budget() { return flush_budget_; }
|
2008-12-19 12:02:34 +00:00
|
|
|
QuickCheckDetails* quick_check_performed() { return &quick_check_performed_; }
|
|
|
|
bool mentions_reg(int reg);
|
2009-01-08 12:40:47 +00:00
|
|
|
// Returns true if a deferred position store exists to the specified
|
|
|
|
// register and stores the offset in the out-parameter. Otherwise
|
|
|
|
// returns false.
|
|
|
|
bool GetStoredPosition(int reg, int* cp_offset);
|
2009-01-15 12:45:48 +00:00
|
|
|
// These set methods and AdvanceCurrentPositionInTrace should be used only on
|
|
|
|
// new traces - the intention is that traces are immutable after creation.
|
2008-12-08 09:22:12 +00:00
|
|
|
void add_action(DeferredAction* new_action) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(new_action->next_ == NULL);
|
2008-12-08 09:22:12 +00:00
|
|
|
new_action->next_ = actions_;
|
|
|
|
actions_ = new_action;
|
|
|
|
}
|
|
|
|
void set_backtrack(Label* backtrack) { backtrack_ = backtrack; }
|
|
|
|
void set_stop_node(RegExpNode* node) { stop_node_ = node; }
|
|
|
|
void set_loop_label(Label* label) { loop_label_ = label; }
|
2010-01-07 19:01:23 +00:00
|
|
|
void set_characters_preloaded(int count) { characters_preloaded_ = count; }
|
2008-12-22 12:48:14 +00:00
|
|
|
void set_bound_checked_up_to(int to) { bound_checked_up_to_ = to; }
|
2009-02-19 08:24:28 +00:00
|
|
|
void set_flush_budget(int to) { flush_budget_ = to; }
|
2008-12-19 12:02:34 +00:00
|
|
|
void set_quick_check_performed(QuickCheckDetails* d) {
|
|
|
|
quick_check_performed_ = *d;
|
|
|
|
}
|
2009-01-19 18:56:47 +00:00
|
|
|
void InvalidateCurrentCharacter();
|
2009-01-26 20:09:35 +00:00
|
|
|
void AdvanceCurrentPositionInTrace(int by, RegExpCompiler* compiler);
|
2011-09-08 19:57:14 +00:00
|
|
|
|
2008-12-08 09:22:12 +00:00
|
|
|
private:
|
2012-06-11 12:42:31 +00:00
|
|
|
int FindAffectedRegisters(OutSet* affected_registers, Zone* zone);
|
2008-12-08 09:22:12 +00:00
|
|
|
void PerformDeferredActions(RegExpMacroAssembler* macro,
|
2012-06-11 12:42:31 +00:00
|
|
|
int max_register,
|
2014-06-03 15:45:38 +00:00
|
|
|
const OutSet& affected_registers,
|
2012-06-11 12:42:31 +00:00
|
|
|
OutSet* registers_to_pop,
|
|
|
|
OutSet* registers_to_clear,
|
|
|
|
Zone* zone);
|
2008-12-08 09:22:12 +00:00
|
|
|
void RestoreAffectedRegisters(RegExpMacroAssembler* macro,
|
|
|
|
int max_register,
|
2014-06-03 15:45:38 +00:00
|
|
|
const OutSet& registers_to_pop,
|
|
|
|
const OutSet& registers_to_clear);
|
2008-12-08 09:22:12 +00:00
|
|
|
int cp_offset_;
|
|
|
|
DeferredAction* actions_;
|
|
|
|
Label* backtrack_;
|
|
|
|
RegExpNode* stop_node_;
|
|
|
|
Label* loop_label_;
|
2008-12-19 12:02:34 +00:00
|
|
|
int characters_preloaded_;
|
2008-12-22 12:48:14 +00:00
|
|
|
int bound_checked_up_to_;
|
2008-12-19 12:02:34 +00:00
|
|
|
QuickCheckDetails quick_check_performed_;
|
2009-02-19 08:24:28 +00:00
|
|
|
int flush_budget_;
|
2009-02-03 11:43:55 +00:00
|
|
|
TriBool at_start_;
|
2008-12-08 09:22:12 +00:00
|
|
|
};
|
2008-12-19 12:02:34 +00:00
|
|
|
|
|
|
|
|
2014-09-04 12:47:13 +00:00
|
|
|
class GreedyLoopState {
|
|
|
|
public:
|
|
|
|
explicit GreedyLoopState(bool not_at_start);
|
|
|
|
|
|
|
|
Label* label() { return &label_; }
|
|
|
|
Trace* counter_backtrack_trace() { return &counter_backtrack_trace_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
Label label_;
|
|
|
|
Trace counter_backtrack_trace_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct PreloadState {
|
|
|
|
static const int kEatsAtLeastNotYetInitialized = -1;
|
|
|
|
bool preload_is_current_;
|
|
|
|
bool preload_has_checked_bounds_;
|
|
|
|
int preload_characters_;
|
|
|
|
int eats_at_least_;
|
|
|
|
void init() {
|
|
|
|
eats_at_least_ = kEatsAtLeastNotYetInitialized;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-11-25 11:07:48 +00:00
|
|
|
class NodeVisitor {
|
|
|
|
public:
|
|
|
|
virtual ~NodeVisitor() { }
|
|
|
|
#define DECLARE_VISIT(Type) \
|
|
|
|
virtual void Visit##Type(Type##Node* that) = 0;
|
|
|
|
FOR_EACH_NODE_TYPE(DECLARE_VISIT)
|
|
|
|
#undef DECLARE_VISIT
|
2008-12-17 13:16:38 +00:00
|
|
|
virtual void VisitLoopChoice(LoopChoiceNode* that) { VisitChoice(that); }
|
2008-11-25 11:07:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Node visitor used to add the start set of the alternatives to the
|
|
|
|
// dispatch table of a choice node.
|
|
|
|
class DispatchTableConstructor: public NodeVisitor {
|
|
|
|
public:
|
2012-06-11 12:42:31 +00:00
|
|
|
DispatchTableConstructor(DispatchTable* table, bool ignore_case,
|
|
|
|
Zone* zone)
|
2008-11-26 08:03:55 +00:00
|
|
|
: table_(table),
|
2008-12-01 15:42:35 +00:00
|
|
|
choice_index_(-1),
|
2012-06-11 12:42:31 +00:00
|
|
|
ignore_case_(ignore_case),
|
|
|
|
zone_(zone) { }
|
2008-11-25 11:07:48 +00:00
|
|
|
|
|
|
|
void BuildTable(ChoiceNode* node);
|
|
|
|
|
|
|
|
void AddRange(CharacterRange range) {
|
2012-06-11 12:42:31 +00:00
|
|
|
table()->AddRange(range, choice_index_, zone_);
|
2008-11-25 11:07:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AddInverse(ZoneList<CharacterRange>* ranges);
|
|
|
|
|
|
|
|
#define DECLARE_VISIT(Type) \
|
|
|
|
virtual void Visit##Type(Type##Node* that);
|
|
|
|
FOR_EACH_NODE_TYPE(DECLARE_VISIT)
|
|
|
|
#undef DECLARE_VISIT
|
|
|
|
|
|
|
|
DispatchTable* table() { return table_; }
|
|
|
|
void set_choice_index(int value) { choice_index_ = value; }
|
|
|
|
|
|
|
|
protected:
|
2009-04-27 09:27:18 +00:00
|
|
|
DispatchTable* table_;
|
2008-11-25 11:07:48 +00:00
|
|
|
int choice_index_;
|
2008-12-01 15:42:35 +00:00
|
|
|
bool ignore_case_;
|
2012-06-11 12:42:31 +00:00
|
|
|
Zone* zone_;
|
2008-11-25 11:07:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-12-11 11:13:13 +00:00
|
|
|
// Assertion propagation moves information about assertions such as
|
|
|
|
// \b to the affected nodes. For instance, in /.\b./ information must
|
|
|
|
// be propagated to the first '.' that whatever follows needs to know
|
|
|
|
// if it matched a word or a non-word, and to the second '.' that it
|
|
|
|
// has to check if it succeeds a word or non-word. In this case the
|
|
|
|
// result will be something like:
|
|
|
|
//
|
|
|
|
// +-------+ +------------+
|
|
|
|
// | . | | . |
|
|
|
|
// +-------+ ---> +------------+
|
|
|
|
// | word? | | check word |
|
|
|
|
// +-------+ +------------+
|
2008-12-18 14:30:53 +00:00
|
|
|
class Analysis: public NodeVisitor {
|
2008-11-25 11:07:48 +00:00
|
|
|
public:
|
2009-11-09 10:01:23 +00:00
|
|
|
Analysis(bool ignore_case, bool is_ascii)
|
|
|
|
: ignore_case_(ignore_case),
|
|
|
|
is_ascii_(is_ascii),
|
|
|
|
error_message_(NULL) { }
|
2008-11-25 11:07:48 +00:00
|
|
|
void EnsureAnalyzed(RegExpNode* node);
|
|
|
|
|
|
|
|
#define DECLARE_VISIT(Type) \
|
|
|
|
virtual void Visit##Type(Type##Node* that);
|
|
|
|
FOR_EACH_NODE_TYPE(DECLARE_VISIT)
|
|
|
|
#undef DECLARE_VISIT
|
2008-12-17 13:16:38 +00:00
|
|
|
virtual void VisitLoopChoice(LoopChoiceNode* that);
|
2008-11-27 07:27:08 +00:00
|
|
|
|
2009-05-27 11:23:26 +00:00
|
|
|
bool has_failed() { return error_message_ != NULL; }
|
|
|
|
const char* error_message() {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(error_message_ != NULL);
|
2009-05-27 11:23:26 +00:00
|
|
|
return error_message_;
|
|
|
|
}
|
|
|
|
void fail(const char* error_message) {
|
|
|
|
error_message_ = error_message;
|
|
|
|
}
|
2011-09-08 19:57:14 +00:00
|
|
|
|
2008-11-27 07:27:08 +00:00
|
|
|
private:
|
|
|
|
bool ignore_case_;
|
2009-11-09 10:01:23 +00:00
|
|
|
bool is_ascii_;
|
2009-05-27 11:23:26 +00:00
|
|
|
const char* error_message_;
|
2008-11-27 07:27:08 +00:00
|
|
|
|
2008-12-18 14:30:53 +00:00
|
|
|
DISALLOW_IMPLICIT_CONSTRUCTORS(Analysis);
|
2008-11-25 11:07:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-12-11 11:13:13 +00:00
|
|
|
struct RegExpCompileData {
|
|
|
|
RegExpCompileData()
|
|
|
|
: tree(NULL),
|
|
|
|
node(NULL),
|
2008-12-12 10:22:56 +00:00
|
|
|
simple(true),
|
2009-02-03 11:43:55 +00:00
|
|
|
contains_anchor(false),
|
2008-12-11 11:13:13 +00:00
|
|
|
capture_count(0) { }
|
2008-11-25 11:07:48 +00:00
|
|
|
RegExpTree* tree;
|
2008-12-11 11:13:13 +00:00
|
|
|
RegExpNode* node;
|
2008-12-12 10:22:56 +00:00
|
|
|
bool simple;
|
2009-02-03 11:43:55 +00:00
|
|
|
bool contains_anchor;
|
2008-11-25 11:07:48 +00:00
|
|
|
Handle<String> error;
|
|
|
|
int capture_count;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class RegExpEngine: public AllStatic {
|
|
|
|
public:
|
2009-03-11 14:00:55 +00:00
|
|
|
struct CompilationResult {
|
2013-09-11 08:39:38 +00:00
|
|
|
CompilationResult(Isolate* isolate, const char* error_message)
|
2009-03-11 14:00:55 +00:00
|
|
|
: error_message(error_message),
|
2013-09-11 08:39:38 +00:00
|
|
|
code(isolate->heap()->the_hole_value()),
|
2009-03-11 14:00:55 +00:00
|
|
|
num_registers(0) {}
|
|
|
|
CompilationResult(Object* code, int registers)
|
|
|
|
: error_message(NULL),
|
|
|
|
code(code),
|
|
|
|
num_registers(registers) {}
|
|
|
|
const char* error_message;
|
|
|
|
Object* code;
|
|
|
|
int num_registers;
|
|
|
|
};
|
|
|
|
|
|
|
|
static CompilationResult Compile(RegExpCompileData* input,
|
|
|
|
bool ignore_case,
|
2012-05-22 14:05:44 +00:00
|
|
|
bool global,
|
2009-03-11 14:00:55 +00:00
|
|
|
bool multiline,
|
|
|
|
Handle<String> pattern,
|
2012-04-02 09:38:07 +00:00
|
|
|
Handle<String> sample_subject,
|
2012-06-04 14:42:58 +00:00
|
|
|
bool is_ascii, Zone* zone);
|
2008-12-08 12:43:01 +00:00
|
|
|
|
2008-12-01 15:42:35 +00:00
|
|
|
static void DotPrint(const char* label, RegExpNode* node, bool ignore_case);
|
2008-11-25 11:07:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
} } // namespace v8::internal
|
|
|
|
|
|
|
|
#endif // V8_JSREGEXP_H_
|