| 1 |
// Copyright 2019 the V8 project authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style license that can be |
| 3 |
// found in the LICENSE file. |
| 4 |
|
| 5 |
#ifndef V8_REGEXP_REGEXP_COMPILER_H_ |
| 6 |
#define V8_REGEXP_REGEXP_COMPILER_H_ |
| 7 |
|
| 8 |
#include <bitset> |
| 9 |
|
| 10 |
#include "irregexp/imported/regexp-nodes.h" |
| 11 |
|
| 12 |
namespace v8 { |
| 13 |
namespace internal { |
| 14 |
|
| 15 |
class DynamicBitSet; |
| 16 |
class Isolate; |
| 17 |
|
| 18 |
namespace regexp_compiler_constants { |
| 19 |
|
| 20 |
// The '2' variant is has inclusive from and exclusive to. |
| 21 |
// This covers \s as defined in ECMA-262 5.1, 15.10.2.12, |
| 22 |
// which include WhiteSpace (7.2) or LineTerminator (7.3) values. |
| 23 |
constexpr base::uc32 kRangeEndMarker = 0x110000; |
| 24 |
constexpr int kSpaceRanges[] = { |
| 25 |
'\t', '\r' + 1, ' ', ' ' + 1, 0x00A0, 0x00A1, 0x1680, |
| 26 |
0x1681, 0x2000, 0x200B, 0x2028, 0x202A, 0x202F, 0x2030, |
| 27 |
0x205F, 0x2060, 0x3000, 0x3001, 0xFEFF, 0xFF00, kRangeEndMarker}; |
| 28 |
constexpr int kSpaceRangeCount = arraysize(kSpaceRanges); |
| 29 |
|
| 30 |
constexpr int kWordRanges[] = {'0', '9' + 1, 'A', 'Z' + 1, '_', |
| 31 |
'_' + 1, 'a', 'z' + 1, kRangeEndMarker}; |
| 32 |
constexpr int kWordRangeCount = arraysize(kWordRanges); |
| 33 |
constexpr int kDigitRanges[] = {'0', '9' + 1, kRangeEndMarker}; |
| 34 |
constexpr int kDigitRangeCount = arraysize(kDigitRanges); |
| 35 |
constexpr int kSurrogateRanges[] = {kLeadSurrogateStart, |
| 36 |
kLeadSurrogateStart + 1, kRangeEndMarker}; |
| 37 |
constexpr int kSurrogateRangeCount = arraysize(kSurrogateRanges); |
| 38 |
constexpr int kLineTerminatorRanges[] = {0x000A, 0x000B, 0x000D, 0x000E, |
| 39 |
0x2028, 0x202A, kRangeEndMarker}; |
| 40 |
constexpr int kLineTerminatorRangeCount = arraysize(kLineTerminatorRanges); |
| 41 |
|
| 42 |
// More makes code generation slower, less makes V8 benchmark score lower. |
| 43 |
constexpr uint32_t kMaxLookaheadForBoyerMoore = 8; |
| 44 |
// In a 3-character pattern you can maximally step forwards 3 characters |
| 45 |
// at a time, which is not always enough to pay for the extra logic. |
| 46 |
constexpr uint32_t kPatternTooShortForBoyerMoore = 2; |
| 47 |
|
| 48 |
} // namespace regexp_compiler_constants |
| 49 |
|
| 50 |
inline bool NeedsUnicodeCaseEquivalents(RegExpFlags flags) { |
| 51 |
// Both unicode (or unicode sets) and ignore_case flags are set. We need to |
| 52 |
// use ICU to find the closure over case equivalents. |
| 53 |
return IsEitherUnicode(flags) && IsIgnoreCase(flags); |
| 54 |
} |
| 55 |
|
| 56 |
// Details of a quick mask-compare check that can look ahead in the |
| 57 |
// input stream. |
| 58 |
class QuickCheckDetails { |
| 59 |
public: |
| 60 |
QuickCheckDetails() |
| 61 |
: characters_(0), mask_(0), value_(0), cannot_match_(false) {} |
| 62 |
explicit QuickCheckDetails(int characters) |
| 63 |
: characters_(characters), mask_(0), value_(0), cannot_match_(false) {} |
| 64 |
bool Rationalize(bool one_byte); |
| 65 |
// Merge in the information from another branch of an alternation. |
| 66 |
void Merge(QuickCheckDetails* other, int from_index); |
| 67 |
// Advance the current position by some amount. |
| 68 |
void Advance(int by, bool one_byte); |
| 69 |
void Clear(); |
| 70 |
bool cannot_match() { return cannot_match_; } |
| 71 |
void set_cannot_match() { cannot_match_ = true; } |
| 72 |
struct Position { |
| 73 |
Position() : mask(0), value(0), determines_perfectly(false) {} |
| 74 |
base::uc32 mask; |
| 75 |
base::uc32 value; |
| 76 |
bool determines_perfectly; |
| 77 |
}; |
| 78 |
int characters() { return characters_; } |
| 79 |
void set_characters(int characters) { characters_ = characters; } |
| 80 |
Position* positions(int index) { |
| 81 |
DCHECK_LE(0, index); |
| 82 |
DCHECK_GT(characters_, index); |
| 83 |
return positions_ + index; |
| 84 |
} |
| 85 |
uint32_t mask() { return mask_; } |
| 86 |
uint32_t value() { return value_; } |
| 87 |
|
| 88 |
private: |
| 89 |
// How many characters do we have quick check information from. This is |
| 90 |
// the same for all branches of a choice node. |
| 91 |
int characters_; |
| 92 |
Position positions_[4]; |
| 93 |
// These values are the condensate of the above array after Rationalize(). |
| 94 |
uint32_t mask_; |
| 95 |
uint32_t value_; |
| 96 |
// If set to true, there is no way this quick check can match at all. |
| 97 |
// E.g., if it requires to be at the start of the input, and isn't. |
| 98 |
bool cannot_match_; |
| 99 |
}; |
| 100 |
|
| 101 |
// Improve the speed that we scan for an initial point where a non-anchored |
| 102 |
// regexp can match by using a Boyer-Moore-like table. This is done by |
| 103 |
// identifying non-greedy non-capturing loops in the nodes that eat any |
| 104 |
// character one at a time. For example in the middle of the regexp |
| 105 |
// /foo[\s\S]*?bar/ we find such a loop. There is also such a loop implicitly |
| 106 |
// inserted at the start of any non-anchored regexp. |
| 107 |
// |
| 108 |
// When we have found such a loop we look ahead in the nodes to find the set of |
| 109 |
// characters that can come at given distances. For example for the regexp |
| 110 |
// /.?foo/ we know that there are at least 3 characters ahead of us, and the |
| 111 |
// sets of characters that can occur are [any, [f, o], [o]]. We find a range in |
| 112 |
// the lookahead info where the set of characters is reasonably constrained. In |
| 113 |
// our example this is from index 1 to 2 (0 is not constrained). We can now |
| 114 |
// look 3 characters ahead and if we don't find one of [f, o] (the union of |
| 115 |
// [f, o] and [o]) then we can skip forwards by the range size (in this case 2). |
| 116 |
// |
| 117 |
// For Unicode input strings we do the same, but modulo 128. |
| 118 |
// |
| 119 |
// We also look at the first string fed to the regexp and use that to get a hint |
| 120 |
// of the character frequencies in the inputs. This affects the assessment of |
| 121 |
// whether the set of characters is 'reasonably constrained'. |
| 122 |
// |
| 123 |
// We also have another lookahead mechanism (called quick check in the code), |
| 124 |
// which uses a wide load of multiple characters followed by a mask and compare |
| 125 |
// to determine whether a match is possible at this point. |
| 126 |
enum ContainedInLattice { |
| 127 |
kNotYet = 0, |
| 128 |
kLatticeIn = 1, |
| 129 |
kLatticeOut = 2, |
| 130 |
kLatticeUnknown = 3 // Can also mean both in and out. |
| 131 |
}; |
| 132 |
|
| 133 |
inline ContainedInLattice Combine(ContainedInLattice a, ContainedInLattice b) { |
| 134 |
return static_cast<ContainedInLattice>(a | b); |
| 135 |
} |
| 136 |
|
| 137 |
class BoyerMoorePositionInfo : public ZoneObject { |
| 138 |
public: |
| 139 |
bool at(int i) const { return map_[i]; } |
| 140 |
|
| 141 |
static constexpr int kMapSize = 128; |
| 142 |
static constexpr int kMask = kMapSize - 1; |
| 143 |
|
| 144 |
int map_count() const { return map_count_; } |
| 145 |
|
| 146 |
void Set(int character); |
| 147 |
void SetInterval(const Interval& interval); |
| 148 |
void SetAll(); |
| 149 |
|
| 150 |
bool is_non_word() { return w_ == kLatticeOut; } |
| 151 |
bool is_word() { return w_ == kLatticeIn; } |
| 152 |
|
| 153 |
using Bitset = std::bitset<kMapSize>; |
| 154 |
Bitset raw_bitset() const { return map_; } |
| 155 |
|
| 156 |
private: |
| 157 |
Bitset map_; |
| 158 |
int map_count_ = 0; // Number of set bits in the map. |
| 159 |
ContainedInLattice w_ = kNotYet; // The \w character class. |
| 160 |
}; |
| 161 |
|
| 162 |
class BoyerMooreLookahead : public ZoneObject { |
| 163 |
public: |
| 164 |
BoyerMooreLookahead(int length, RegExpCompiler* compiler, Zone* zone); |
| 165 |
|
| 166 |
int length() { return length_; } |
| 167 |
int max_char() { return max_char_; } |
| 168 |
RegExpCompiler* compiler() { return compiler_; } |
| 169 |
|
| 170 |
int Count(int map_number) { return bitmaps_->at(map_number)->map_count(); } |
| 171 |
|
| 172 |
BoyerMoorePositionInfo* at(int i) { return bitmaps_->at(i); } |
| 173 |
|
| 174 |
void Set(int map_number, int character) { |
| 175 |
if (character > max_char_) return; |
| 176 |
BoyerMoorePositionInfo* info = bitmaps_->at(map_number); |
| 177 |
info->Set(character); |
| 178 |
} |
| 179 |
|
| 180 |
void SetInterval(int map_number, const Interval& interval) { |
| 181 |
if (interval.from() > max_char_) return; |
| 182 |
BoyerMoorePositionInfo* info = bitmaps_->at(map_number); |
| 183 |
if (interval.to() > max_char_) { |
| 184 |
info->SetInterval(Interval(interval.from(), max_char_)); |
| 185 |
} else { |
| 186 |
info->SetInterval(interval); |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
void SetAll(int map_number) { bitmaps_->at(map_number)->SetAll(); } |
| 191 |
|
| 192 |
void SetRest(int from_map) { |
| 193 |
for (int i = from_map; i < length_; i++) SetAll(i); |
| 194 |
} |
| 195 |
void EmitSkipInstructions(RegExpMacroAssembler* masm); |
| 196 |
|
| 197 |
private: |
| 198 |
// This is the value obtained by EatsAtLeast. If we do not have at least this |
| 199 |
// many characters left in the sample string then the match is bound to fail. |
| 200 |
// Therefore it is OK to read a character this far ahead of the current match |
| 201 |
// point. |
| 202 |
int length_; |
| 203 |
RegExpCompiler* compiler_; |
| 204 |
// 0xff for Latin1, 0xffff for UTF-16. |
| 205 |
int max_char_; |
| 206 |
ZoneList<BoyerMoorePositionInfo*>* bitmaps_; |
| 207 |
|
| 208 |
int GetSkipTable( |
| 209 |
int min_lookahead, int max_lookahead, |
| 210 |
DirectHandle<ByteArray> boolean_skip_table, |
| 211 |
DirectHandle<ByteArray> nibble_table = DirectHandle<ByteArray>{}); |
| 212 |
bool FindWorthwhileInterval(int* from, int* to); |
| 213 |
int FindBestInterval(int max_number_of_chars, int old_biggest_points, |
| 214 |
int* from, int* to); |
| 215 |
}; |
| 216 |
|
| 217 |
// There are many ways to generate code for a node. This class encapsulates |
| 218 |
// the current way we should be generating. In other words it encapsulates |
| 219 |
// the current state of the code generator. The effect of this is that we |
| 220 |
// generate code for paths that the matcher can take through the regular |
| 221 |
// expression. A given node in the regexp can be code-generated several times |
| 222 |
// as it can be part of several traces. For example for the regexp: |
| 223 |
// /foo(bar|ip)baz/ the code to match baz will be generated twice, once as part |
| 224 |
// of the foo-bar-baz trace and once as part of the foo-ip-baz trace. The code |
| 225 |
// to match foo is generated only once (the traces have a common prefix). The |
| 226 |
// code to store the capture is deferred and generated (twice) after the places |
| 227 |
// where baz has been matched. |
| 228 |
class Trace { |
| 229 |
public: |
| 230 |
// A value for a property that is either known to be true, know to be false, |
| 231 |
// or not known. |
| 232 |
enum TriBool { UNKNOWN = -1, FALSE_VALUE = 0, TRUE_VALUE = 1 }; |
| 233 |
|
| 234 |
class DeferredAction { |
| 235 |
public: |
| 236 |
DeferredAction(ActionNode::ActionType action_type, int reg) |
| 237 |
: action_type_(action_type), reg_(reg), next_(nullptr) {} |
| 238 |
DeferredAction* next() { return next_; } |
| 239 |
bool Mentions(int reg); |
| 240 |
int reg() { return reg_; } |
| 241 |
ActionNode::ActionType action_type() { return action_type_; } |
| 242 |
|
| 243 |
private: |
| 244 |
ActionNode::ActionType action_type_; |
| 245 |
int reg_; |
| 246 |
DeferredAction* next_; |
| 247 |
friend class Trace; |
| 248 |
}; |
| 249 |
|
| 250 |
class DeferredCapture : public DeferredAction { |
| 251 |
public: |
| 252 |
DeferredCapture(int reg, bool is_capture, Trace* trace) |
| 253 |
: DeferredAction(ActionNode::STORE_POSITION, reg), |
| 254 |
cp_offset_(trace->cp_offset()), |
| 255 |
is_capture_(is_capture) {} |
| 256 |
int cp_offset() { return cp_offset_; } |
| 257 |
bool is_capture() { return is_capture_; } |
| 258 |
|
| 259 |
private: |
| 260 |
int cp_offset_; |
| 261 |
bool is_capture_; |
| 262 |
void set_cp_offset(int cp_offset) { cp_offset_ = cp_offset; } |
| 263 |
}; |
| 264 |
|
| 265 |
class DeferredSetRegisterForLoop : public DeferredAction { |
| 266 |
public: |
| 267 |
DeferredSetRegisterForLoop(int reg, int value) |
| 268 |
: DeferredAction(ActionNode::SET_REGISTER_FOR_LOOP, reg), |
| 269 |
value_(value) {} |
| 270 |
int value() { return value_; } |
| 271 |
|
| 272 |
private: |
| 273 |
int value_; |
| 274 |
}; |
| 275 |
|
| 276 |
class DeferredClearCaptures : public DeferredAction { |
| 277 |
public: |
| 278 |
explicit DeferredClearCaptures(Interval range) |
| 279 |
: DeferredAction(ActionNode::CLEAR_CAPTURES, -1), range_(range) {} |
| 280 |
Interval range() { return range_; } |
| 281 |
|
| 282 |
private: |
| 283 |
Interval range_; |
| 284 |
}; |
| 285 |
|
| 286 |
class DeferredIncrementRegister : public DeferredAction { |
| 287 |
public: |
| 288 |
explicit DeferredIncrementRegister(int reg) |
| 289 |
: DeferredAction(ActionNode::INCREMENT_REGISTER, reg) {} |
| 290 |
}; |
| 291 |
|
| 292 |
Trace() |
| 293 |
: cp_offset_(0), |
| 294 |
actions_(nullptr), |
| 295 |
backtrack_(nullptr), |
| 296 |
stop_node_(nullptr), |
| 297 |
loop_label_(nullptr), |
| 298 |
characters_preloaded_(0), |
| 299 |
bound_checked_up_to_(0), |
| 300 |
flush_budget_(100), |
| 301 |
at_start_(UNKNOWN) {} |
| 302 |
|
| 303 |
// End the trace. This involves flushing the deferred actions in the trace |
| 304 |
// and pushing a backtrack location onto the backtrack stack. Once this is |
| 305 |
// done we can start a new trace or go to one that has already been |
| 306 |
// generated. |
| 307 |
void Flush(RegExpCompiler* compiler, RegExpNode* successor); |
| 308 |
int cp_offset() { return cp_offset_; } |
| 309 |
DeferredAction* actions() { return actions_; } |
| 310 |
// A trivial trace is one that has no deferred actions or other state that |
| 311 |
// affects the assumptions used when generating code. There is no recorded |
| 312 |
// backtrack location in a trivial trace, so with a trivial trace we will |
| 313 |
// generate code that, on a failure to match, gets the backtrack location |
| 314 |
// from the backtrack stack rather than using a direct jump instruction. We |
| 315 |
// always start code generation with a trivial trace and non-trivial traces |
| 316 |
// are created as we emit code for nodes or add to the list of deferred |
| 317 |
// actions in the trace. The location of the code generated for a node using |
| 318 |
// a trivial trace is recorded in a label in the node so that gotos can be |
| 319 |
// generated to that code. |
| 320 |
bool is_trivial() { |
| 321 |
return backtrack_ == nullptr && actions_ == nullptr && cp_offset_ == 0 && |
| 322 |
characters_preloaded_ == 0 && bound_checked_up_to_ == 0 && |
| 323 |
quick_check_performed_.characters() == 0 && at_start_ == UNKNOWN; |
| 324 |
} |
| 325 |
TriBool at_start() { return at_start_; } |
| 326 |
void set_at_start(TriBool at_start) { at_start_ = at_start; } |
| 327 |
Label* backtrack() { return backtrack_; } |
| 328 |
Label* loop_label() { return loop_label_; } |
| 329 |
RegExpNode* stop_node() { return stop_node_; } |
| 330 |
int characters_preloaded() { return characters_preloaded_; } |
| 331 |
int bound_checked_up_to() { return bound_checked_up_to_; } |
| 332 |
int flush_budget() { return flush_budget_; } |
| 333 |
QuickCheckDetails* quick_check_performed() { return &quick_check_performed_; } |
| 334 |
bool mentions_reg(int reg); |
| 335 |
// Returns true if a deferred position store exists to the specified |
| 336 |
// register and stores the offset in the out-parameter. Otherwise |
| 337 |
// returns false. |
| 338 |
bool GetStoredPosition(int reg, int* cp_offset); |
| 339 |
// These set methods and AdvanceCurrentPositionInTrace should be used only on |
| 340 |
// new traces - the intention is that traces are immutable after creation. |
| 341 |
void add_action(DeferredAction* new_action) { |
| 342 |
DCHECK(new_action->next_ == nullptr); |
| 343 |
new_action->next_ = actions_; |
| 344 |
actions_ = new_action; |
| 345 |
} |
| 346 |
void set_backtrack(Label* backtrack) { backtrack_ = backtrack; } |
| 347 |
void set_stop_node(RegExpNode* node) { stop_node_ = node; } |
| 348 |
void set_loop_label(Label* label) { loop_label_ = label; } |
| 349 |
void set_characters_preloaded(int count) { characters_preloaded_ = count; } |
| 350 |
void set_bound_checked_up_to(int to) { bound_checked_up_to_ = to; } |
| 351 |
void set_flush_budget(int to) { flush_budget_ = to; } |
| 352 |
void set_quick_check_performed(QuickCheckDetails* d) { |
| 353 |
quick_check_performed_ = *d; |
| 354 |
} |
| 355 |
void InvalidateCurrentCharacter(); |
| 356 |
void AdvanceCurrentPositionInTrace(int by, RegExpCompiler* compiler); |
| 357 |
|
| 358 |
private: |
| 359 |
int FindAffectedRegisters(DynamicBitSet* affected_registers, Zone* zone); |
| 360 |
void PerformDeferredActions(RegExpMacroAssembler* macro, int max_register, |
| 361 |
const DynamicBitSet& affected_registers, |
| 362 |
DynamicBitSet* registers_to_pop, |
| 363 |
DynamicBitSet* registers_to_clear, Zone* zone); |
| 364 |
void RestoreAffectedRegisters(RegExpMacroAssembler* macro, int max_register, |
| 365 |
const DynamicBitSet& registers_to_pop, |
| 366 |
const DynamicBitSet& registers_to_clear); |
| 367 |
int cp_offset_; |
| 368 |
DeferredAction* actions_; |
| 369 |
Label* backtrack_; |
| 370 |
RegExpNode* stop_node_; |
| 371 |
Label* loop_label_; |
| 372 |
int characters_preloaded_; |
| 373 |
int bound_checked_up_to_; |
| 374 |
QuickCheckDetails quick_check_performed_; |
| 375 |
int flush_budget_; |
| 376 |
TriBool at_start_; |
| 377 |
}; |
| 378 |
|
| 379 |
class GreedyLoopState { |
| 380 |
public: |
| 381 |
explicit GreedyLoopState(bool not_at_start); |
| 382 |
|
| 383 |
Label* label() { return &label_; } |
| 384 |
Trace* counter_backtrack_trace() { return &counter_backtrack_trace_; } |
| 385 |
|
| 386 |
private: |
| 387 |
Label label_; |
| 388 |
Trace counter_backtrack_trace_; |
| 389 |
}; |
| 390 |
|
| 391 |
struct PreloadState { |
| 392 |
static const int kEatsAtLeastNotYetInitialized = -1; |
| 393 |
bool preload_is_current_; |
| 394 |
bool preload_has_checked_bounds_; |
| 395 |
int preload_characters_; |
| 396 |
int eats_at_least_; |
| 397 |
void init() { eats_at_least_ = kEatsAtLeastNotYetInitialized; } |
| 398 |
}; |
| 399 |
|
| 400 |
// Analysis performs assertion propagation and computes eats_at_least_ values. |
| 401 |
// See the comments on AssertionPropagator and EatsAtLeastPropagator for more |
| 402 |
// details. |
| 403 |
RegExpError AnalyzeRegExp(Isolate* isolate, bool is_one_byte, RegExpFlags flags, |
| 404 |
RegExpNode* node); |
| 405 |
|
| 406 |
class FrequencyCollator { |
| 407 |
public: |
| 408 |
FrequencyCollator() : total_samples_(0) { |
| 409 |
for (int i = 0; i < RegExpMacroAssembler::kTableSize; i++) { |
| 410 |
frequencies_[i] = CharacterFrequency(i); |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
void CountCharacter(int character) { |
| 415 |
int index = (character & RegExpMacroAssembler::kTableMask); |
| 416 |
frequencies_[index].Increment(); |
| 417 |
total_samples_++; |
| 418 |
} |
| 419 |
|
| 420 |
// Does not measure in percent, but rather per-128 (the table size from the |
| 421 |
// regexp macro assembler). |
| 422 |
int Frequency(int in_character) { |
| 423 |
DCHECK((in_character & RegExpMacroAssembler::kTableMask) == in_character); |
| 424 |
if (total_samples_ < 1) return 1; // Division by zero. |
| 425 |
int freq_in_per128 = |
| 426 |
(frequencies_[in_character].counter() * 128) / total_samples_; |
| 427 |
return freq_in_per128; |
| 428 |
} |
| 429 |
|
| 430 |
private: |
| 431 |
class CharacterFrequency { |
| 432 |
public: |
| 433 |
CharacterFrequency() : counter_(0), character_(-1) {} |
| 434 |
explicit CharacterFrequency(int character) |
| 435 |
: counter_(0), character_(character) {} |
| 436 |
|
| 437 |
void Increment() { counter_++; } |
| 438 |
int counter() { return counter_; } |
| 439 |
int character() { return character_; } |
| 440 |
|
| 441 |
private: |
| 442 |
int counter_; |
| 443 |
int character_; |
| 444 |
}; |
| 445 |
|
| 446 |
private: |
| 447 |
CharacterFrequency frequencies_[RegExpMacroAssembler::kTableSize]; |
| 448 |
int total_samples_; |
| 449 |
}; |
| 450 |
|
| 451 |
class RegExpCompiler { |
| 452 |
public: |
| 453 |
RegExpCompiler(Isolate* isolate, Zone* zone, int capture_count, |
| 454 |
RegExpFlags flags, bool is_one_byte); |
| 455 |
|
| 456 |
int AllocateRegister() { |
| 457 |
if (next_register_ >= RegExpMacroAssembler::kMaxRegister) { |
| 458 |
reg_exp_too_big_ = true; |
| 459 |
return next_register_; |
| 460 |
} |
| 461 |
return next_register_++; |
| 462 |
} |
| 463 |
|
| 464 |
// Lookarounds to match lone surrogates for unicode character class matches |
| 465 |
// are never nested. We can therefore reuse registers. |
| 466 |
int UnicodeLookaroundStackRegister() { |
| 467 |
if (unicode_lookaround_stack_register_ == kNoRegister) { |
| 468 |
unicode_lookaround_stack_register_ = AllocateRegister(); |
| 469 |
} |
| 470 |
return unicode_lookaround_stack_register_; |
| 471 |
} |
| 472 |
|
| 473 |
int UnicodeLookaroundPositionRegister() { |
| 474 |
if (unicode_lookaround_position_register_ == kNoRegister) { |
| 475 |
unicode_lookaround_position_register_ = AllocateRegister(); |
| 476 |
} |
| 477 |
return unicode_lookaround_position_register_; |
| 478 |
} |
| 479 |
|
| 480 |
struct CompilationResult final { |
| 481 |
explicit CompilationResult(RegExpError err) : error(err) {} |
| 482 |
CompilationResult(DirectHandle<Object> code, int registers) |
| 483 |
: code(code), num_registers(registers) {} |
| 484 |
|
| 485 |
static CompilationResult RegExpTooBig() { |
| 486 |
return CompilationResult(RegExpError::kTooLarge); |
| 487 |
} |
| 488 |
|
| 489 |
bool Succeeded() const { return error == RegExpError::kNone; } |
| 490 |
|
| 491 |
const RegExpError error = RegExpError::kNone; |
| 492 |
DirectHandle<Object> code; |
| 493 |
int num_registers = 0; |
| 494 |
}; |
| 495 |
|
| 496 |
CompilationResult Assemble(Isolate* isolate, RegExpMacroAssembler* assembler, |
| 497 |
RegExpNode* start, int capture_count, |
| 498 |
DirectHandle<String> pattern); |
| 499 |
|
| 500 |
// Preprocessing is the final step of node creation before analysis |
| 501 |
// and assembly. It includes: |
| 502 |
// - Wrapping the body of the regexp in capture 0. |
| 503 |
// - Inserting the implicit .* before/after the regexp if necessary. |
| 504 |
// - If the input is a one-byte string, filtering out nodes that can't match. |
| 505 |
// - Fixing up regexp matches that start within a surrogate pair. |
| 506 |
RegExpNode* PreprocessRegExp(RegExpCompileData* data, bool is_one_byte); |
| 507 |
|
| 508 |
// If the regexp matching starts within a surrogate pair, step back to the |
| 509 |
// lead surrogate and start matching from there. |
| 510 |
RegExpNode* OptionallyStepBackToLeadSurrogate(RegExpNode* on_success); |
| 511 |
|
| 512 |
inline void AddWork(RegExpNode* node) { |
| 513 |
if (!node->on_work_list() && !node->label()->is_bound()) { |
| 514 |
node->set_on_work_list(true); |
| 515 |
work_list_->push_back(node); |
| 516 |
} |
| 517 |
} |
| 518 |
|
| 519 |
static const int kImplementationOffset = 0; |
| 520 |
static const int kNumberOfRegistersOffset = 0; |
| 521 |
static const int kCodeOffset = 1; |
| 522 |
|
| 523 |
RegExpMacroAssembler* macro_assembler() { return macro_assembler_; } |
| 524 |
EndNode* accept() { return accept_; } |
| 525 |
|
| 526 |
static const int kMaxRecursion = 100; |
| 527 |
inline int recursion_depth() { return recursion_depth_; } |
| 528 |
inline void IncrementRecursionDepth() { recursion_depth_++; } |
| 529 |
inline void DecrementRecursionDepth() { recursion_depth_--; } |
| 530 |
|
| 531 |
inline RegExpFlags flags() const { return flags_; } |
| 532 |
inline void set_flags(RegExpFlags flags) { flags_ = flags; } |
| 533 |
|
| 534 |
void SetRegExpTooBig() { reg_exp_too_big_ = true; } |
| 535 |
|
| 536 |
inline bool one_byte() { return one_byte_; } |
| 537 |
inline bool optimize() { return optimize_; } |
| 538 |
inline void set_optimize(bool value) { optimize_ = value; } |
| 539 |
inline bool limiting_recursion() { return limiting_recursion_; } |
| 540 |
inline void set_limiting_recursion(bool value) { |
| 541 |
limiting_recursion_ = value; |
| 542 |
} |
| 543 |
bool read_backward() { return read_backward_; } |
| 544 |
void set_read_backward(bool value) { read_backward_ = value; } |
| 545 |
FrequencyCollator* frequency_collator() { return &frequency_collator_; } |
| 546 |
|
| 547 |
int current_expansion_factor() { return current_expansion_factor_; } |
| 548 |
void set_current_expansion_factor(int value) { |
| 549 |
current_expansion_factor_ = value; |
| 550 |
} |
| 551 |
|
| 552 |
// The recursive nature of ToNode node generation means we may run into stack |
| 553 |
// overflow issues. We introduce periodic checks to detect these, and the |
| 554 |
// tick counter helps limit overhead of these checks. |
| 555 |
// TODO(jgruber): This is super hacky and should be replaced by an abort |
| 556 |
// mechanism or iterative node generation. |
| 557 |
void ToNodeMaybeCheckForStackOverflow() { |
| 558 |
if ((to_node_overflow_check_ticks_++ % 16 == 0)) { |
| 559 |
ToNodeCheckForStackOverflow(); |
| 560 |
} |
| 561 |
} |
| 562 |
void ToNodeCheckForStackOverflow(); |
| 563 |
|
| 564 |
Isolate* isolate() const { return isolate_; } |
| 565 |
Zone* zone() const { return zone_; } |
| 566 |
|
| 567 |
static const int kNoRegister = -1; |
| 568 |
|
| 569 |
private: |
| 570 |
EndNode* accept_; |
| 571 |
int next_register_; |
| 572 |
int unicode_lookaround_stack_register_; |
| 573 |
int unicode_lookaround_position_register_; |
| 574 |
ZoneVector<RegExpNode*>* work_list_; |
| 575 |
int recursion_depth_; |
| 576 |
RegExpFlags flags_; |
| 577 |
RegExpMacroAssembler* macro_assembler_; |
| 578 |
bool one_byte_; |
| 579 |
bool reg_exp_too_big_; |
| 580 |
bool limiting_recursion_; |
| 581 |
int to_node_overflow_check_ticks_ = 0; |
| 582 |
bool optimize_; |
| 583 |
bool read_backward_; |
| 584 |
int current_expansion_factor_; |
| 585 |
FrequencyCollator frequency_collator_; |
| 586 |
Isolate* isolate_; |
| 587 |
Zone* zone_; |
| 588 |
}; |
| 589 |
|
| 590 |
// Categorizes character ranges into BMP, non-BMP, lead, and trail surrogates. |
| 591 |
class UnicodeRangeSplitter { |
| 592 |
public: |
| 593 |
V8_EXPORT_PRIVATE UnicodeRangeSplitter(ZoneList<CharacterRange>* base); |
| 594 |
|
| 595 |
static constexpr int kInitialSize = 8; |
| 596 |
using CharacterRangeVector = base::SmallVector<CharacterRange, kInitialSize>; |
| 597 |
|
| 598 |
const CharacterRangeVector* bmp() const { return &bmp_; } |
| 599 |
const CharacterRangeVector* lead_surrogates() const { |
| 600 |
return &lead_surrogates_; |
| 601 |
} |
| 602 |
const CharacterRangeVector* trail_surrogates() const { |
| 603 |
return &trail_surrogates_; |
| 604 |
} |
| 605 |
const CharacterRangeVector* non_bmp() const { return &non_bmp_; } |
| 606 |
|
| 607 |
private: |
| 608 |
void AddRange(CharacterRange range); |
| 609 |
|
| 610 |
CharacterRangeVector bmp_; |
| 611 |
CharacterRangeVector lead_surrogates_; |
| 612 |
CharacterRangeVector trail_surrogates_; |
| 613 |
CharacterRangeVector non_bmp_; |
| 614 |
}; |
| 615 |
|
| 616 |
// We need to check for the following characters: 0x39C 0x3BC 0x178. |
| 617 |
// TODO(jgruber): Move to CharacterRange. |
| 618 |
bool RangeContainsLatin1Equivalents(CharacterRange range); |
| 619 |
|
| 620 |
} // namespace internal |
| 621 |
} // namespace v8 |
| 622 |
|
| 623 |
#endif // V8_REGEXP_REGEXP_COMPILER_H_ |