1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
* vim: set ts=8 sts=4 et sw=4 tw=99: */
// Copyright 2012 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef V8_REGEXP_MACRO_ASSEMBLER_H_
#define V8_REGEXP_MACRO_ASSEMBLER_H_
#include "irregexp/RegExpAST.h"
#include "irregexp/RegExpEngine.h"
#include "jit/MacroAssembler.h"
namespace js {
namespace irregexp {
class MOZ_STACK_CLASS RegExpMacroAssembler
{
public:
RegExpMacroAssembler(LifoAlloc& alloc, RegExpShared* shared, size_t numSavedRegisters)
: slow_safe_compiler_(false),
global_mode_(NOT_GLOBAL),
alloc_(alloc),
num_registers_(numSavedRegisters),
num_saved_registers_(numSavedRegisters),
shared(shared)
{}
enum StackCheckFlag {
kNoStackLimitCheck = false,
kCheckStackLimit = true
};
// The implementation must be able to handle at least:
static const int kMaxRegister = (1 << 16) - 1;
static const int kMaxCPOffset = (1 << 15) - 1;
static const int kMinCPOffset = -(1 << 15);
static const int kTableSizeBits = 7;
static const int kTableSize = 1 << kTableSizeBits;
static const int kTableMask = kTableSize - 1;
// Controls the generation of large inlined constants in the code.
void set_slow_safe(bool ssc) { slow_safe_compiler_ = ssc; }
bool slow_safe() { return slow_safe_compiler_; }
enum GlobalMode { NOT_GLOBAL, GLOBAL, GLOBAL_NO_ZERO_LENGTH_CHECK };
// Set whether the regular expression has the global flag. Exiting due to
// a failure in a global regexp may still mean success overall.
inline void set_global_mode(GlobalMode mode) { global_mode_ = mode; }
inline bool global() { return global_mode_ != NOT_GLOBAL; }
inline bool global_with_zero_length_check() {
return global_mode_ == GLOBAL;
}
LifoAlloc& alloc() { return alloc_; }
virtual RegExpCode GenerateCode(JSContext* cx, bool match_only) = 0;
// The maximal number of pushes between stack checks. Users must supply
// kCheckStackLimit flag to push operations (instead of kNoStackLimitCheck)
// at least once for every stack_limit() pushes that are executed.
virtual int stack_limit_slack() = 0;
virtual bool CanReadUnaligned() { return false; }
virtual void AdvanceCurrentPosition(int by) = 0; // Signed cp change.
virtual void AdvanceRegister(int reg, int by) = 0; // r[reg] += by.
// Continues execution from the position pushed on the top of the backtrack
// stack by an earlier PushBacktrack.
virtual void Backtrack() = 0;
virtual void Bind(jit::Label* label) = 0;
virtual void CheckAtStart(jit::Label* on_at_start) = 0;
// Dispatch after looking the current character up in a 2-bits-per-entry
// map. The destinations vector has up to 4 labels.
virtual void CheckCharacter(unsigned c, jit::Label* on_equal) = 0;
// Bitwise and the current character with the given constant and then
// check for a match with c.
virtual void CheckCharacterAfterAnd(unsigned c, unsigned and_with, jit::Label* on_equal) = 0;
virtual void CheckCharacterGT(char16_t limit, jit::Label* on_greater) = 0;
virtual void CheckCharacterLT(char16_t limit, jit::Label* on_less) = 0;
virtual void CheckGreedyLoop(jit::Label* on_tos_equals_current_position) = 0;
virtual void CheckNotAtStart(int cp_offset, jit::Label* on_not_at_start) = 0;
virtual void CheckNotBackReference(int start_reg, bool read_backward, jit::Label* on_no_match) = 0;
virtual void CheckNotBackReferenceIgnoreCase(int start_reg, bool read_backward,
jit::Label* on_no_match, bool unicode) = 0;
// Check the current character for a match with a literal character. If we
// fail to match then goto the on_failure label. End of input always
// matches. If the label is nullptr then we should pop a backtrack address off
// the stack and go to that.
virtual void CheckNotCharacter(unsigned c, jit::Label* on_not_equal) = 0;
virtual void CheckNotCharacterAfterAnd(unsigned c, unsigned and_with, jit::Label* on_not_equal) = 0;
// Subtract a constant from the current character, then and with the given
// constant and then check for a match with c.
virtual void CheckNotCharacterAfterMinusAnd(char16_t c,
char16_t minus,
char16_t and_with,
jit::Label* on_not_equal) = 0;
virtual void CheckCharacterInRange(char16_t from, char16_t to, // Both inclusive.
jit::Label* on_in_range) = 0;
virtual void CheckCharacterNotInRange(char16_t from, char16_t to, // Both inclusive.
jit::Label* on_not_in_range) = 0;
// The current character (modulus the kTableSize) is looked up in the byte
// array, and if the found byte is non-zero, we jump to the on_bit_set label.
virtual void CheckBitInTable(uint8_t* table, jit::Label* on_bit_set) = 0;
// Checks whether the given offset from the current position is before
// the end of the string. May overwrite the current character.
virtual void CheckPosition(int cp_offset, jit::Label* on_outside_input) {
LoadCurrentCharacter(cp_offset, on_outside_input, true);
}
// Jump to either the target label or the top of the backtrack stack.
virtual void JumpOrBacktrack(jit::Label* to) = 0;
// Check whether a standard/default character class matches the current
// character. Returns false if the type of special character class does
// not have custom support.
// May clobber the current loaded character.
virtual bool CheckSpecialCharacterClass(char16_t type, jit::Label* on_no_match) {
return false;
}
virtual void Fail() = 0;
// Check whether a register is >= a given constant and go to a label if it
// is. Backtracks instead if the label is nullptr.
virtual void IfRegisterGE(int reg, int comparand, jit::Label* if_ge) = 0;
// Check whether a register is < a given constant and go to a label if it is.
// Backtracks instead if the label is nullptr.
virtual void IfRegisterLT(int reg, int comparand, jit::Label* if_lt) = 0;
// Check whether a register is == to the current position and go to a
// label if it is.
virtual void IfRegisterEqPos(int reg, jit::Label* if_eq) = 0;
virtual void LoadCurrentCharacter(int cp_offset,
jit::Label* on_end_of_input,
bool check_bounds = true,
int characters = 1) = 0;
virtual void PopCurrentPosition() = 0;
virtual void PopRegister(int register_index) = 0;
virtual void PushCurrentPosition() = 0;
virtual void PushRegister(int register_index, StackCheckFlag check_stack_limit) = 0;
virtual void ReadCurrentPositionFromRegister(int reg) = 0;
virtual void ReadBacktrackStackPointerFromRegister(int reg) = 0;
virtual void SetCurrentPositionFromEnd(int by) = 0;
virtual void SetRegister(int register_index, int to) = 0;
// Return whether the matching (with a global regexp) will be restarted.
virtual bool Succeed() = 0;
virtual void WriteCurrentPositionToRegister(int reg, int cp_offset) = 0;
virtual void ClearRegisters(int reg_from, int reg_to) = 0;
virtual void WriteBacktrackStackPointerToRegister(int reg) = 0;
// Pushes the label on the backtrack stack, so that a following Backtrack
// will go to this label. Always checks the backtrack stack limit.
virtual void PushBacktrack(jit::Label* label) = 0;
// Bind a label that was previously used by PushBacktrack.
virtual void BindBacktrack(jit::Label* label) = 0;
private:
bool slow_safe_compiler_;
GlobalMode global_mode_;
LifoAlloc& alloc_;
protected:
int num_registers_;
int num_saved_registers_;
void checkRegister(int reg) {
MOZ_ASSERT(reg >= 0);
MOZ_ASSERT(reg <= kMaxRegister);
if (num_registers_ <= reg)
num_registers_ = reg + 1;
}
public:
RegExpShared* shared;
};
template <typename CharT>
int
CaseInsensitiveCompareStrings(const CharT* substring1, const CharT* substring2, size_t byteLength);
template <typename CharT>
int
CaseInsensitiveCompareUCStrings(const CharT* substring1, const CharT* substring2,
size_t byteLength);
class MOZ_STACK_CLASS InterpretedRegExpMacroAssembler final : public RegExpMacroAssembler
{
public:
InterpretedRegExpMacroAssembler(LifoAlloc* alloc, RegExpShared* shared, size_t numSavedRegisters);
~InterpretedRegExpMacroAssembler();
// Inherited virtual methods.
RegExpCode GenerateCode(JSContext* cx, bool match_only);
void AdvanceCurrentPosition(int by);
void AdvanceRegister(int reg, int by);
void Backtrack();
void Bind(jit::Label* label);
void CheckAtStart(jit::Label* on_at_start);
void CheckCharacter(unsigned c, jit::Label* on_equal);
void CheckCharacterAfterAnd(unsigned c, unsigned and_with, jit::Label* on_equal);
void CheckCharacterGT(char16_t limit, jit::Label* on_greater);
void CheckCharacterLT(char16_t limit, jit::Label* on_less);
void CheckGreedyLoop(jit::Label* on_tos_equals_current_position);
void CheckNotAtStart(int cp_offset, jit::Label* on_not_at_start);
void CheckNotBackReference(int start_reg, bool read_backward, jit::Label* on_no_match);
void CheckNotBackReferenceIgnoreCase(int start_reg, bool read_backward,
jit::Label* on_no_match, bool unicode);
void CheckNotCharacter(unsigned c, jit::Label* on_not_equal);
void CheckNotCharacterAfterAnd(unsigned c, unsigned and_with, jit::Label* on_not_equal);
void CheckNotCharacterAfterMinusAnd(char16_t c, char16_t minus, char16_t and_with,
jit::Label* on_not_equal);
void CheckCharacterInRange(char16_t from, char16_t to,
jit::Label* on_in_range);
void CheckCharacterNotInRange(char16_t from, char16_t to,
jit::Label* on_not_in_range);
void CheckBitInTable(uint8_t* table, jit::Label* on_bit_set);
void JumpOrBacktrack(jit::Label* to);
void Fail();
void IfRegisterGE(int reg, int comparand, jit::Label* if_ge);
void IfRegisterLT(int reg, int comparand, jit::Label* if_lt);
void IfRegisterEqPos(int reg, jit::Label* if_eq);
void LoadCurrentCharacter(int cp_offset, jit::Label* on_end_of_input,
bool check_bounds = true, int characters = 1);
void PopCurrentPosition();
void PopRegister(int register_index);
void PushCurrentPosition();
void PushRegister(int register_index, StackCheckFlag check_stack_limit);
void ReadCurrentPositionFromRegister(int reg);
void ReadBacktrackStackPointerFromRegister(int reg);
void SetCurrentPositionFromEnd(int by);
void SetRegister(int register_index, int to);
bool Succeed();
void WriteCurrentPositionToRegister(int reg, int cp_offset);
void ClearRegisters(int reg_from, int reg_to);
void WriteBacktrackStackPointerToRegister(int reg);
void PushBacktrack(jit::Label* label);
void BindBacktrack(jit::Label* label);
// The byte-code interpreter checks on each push anyway.
int stack_limit_slack() { return 1; }
private:
void Expand();
// Code and bitmap emission.
void EmitOrLink(jit::Label* label);
void Emit32(uint32_t x);
void Emit16(uint32_t x);
void Emit8(uint32_t x);
void Emit(uint32_t bc, uint32_t arg);
jit::Label backtrack_;
// The program counter.
int pc_;
int advance_current_start_;
int advance_current_offset_;
int advance_current_end_;
static const int kInvalidPC = -1;
uint8_t* buffer_;
int length_;
};
} } // namespace js::irregexp
#endif // V8_REGEXP_MACRO_ASSEMBLER_H_
|