• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2008-2009 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 #include "src/regexp/regexp-bytecode-generator.h"
6 
7 #include "src/ast/ast.h"
8 #include "src/objects/objects-inl.h"
9 #include "src/regexp/regexp-bytecode-generator-inl.h"
10 #include "src/regexp/regexp-bytecode-peephole.h"
11 #include "src/regexp/regexp-bytecodes.h"
12 #include "src/regexp/regexp-macro-assembler.h"
13 
14 namespace v8 {
15 namespace internal {
16 
RegExpBytecodeGenerator(Isolate * isolate,Zone * zone)17 RegExpBytecodeGenerator::RegExpBytecodeGenerator(Isolate* isolate, Zone* zone)
18     : RegExpMacroAssembler(isolate, zone),
19       buffer_(Vector<byte>::New(1024)),
20       pc_(0),
21       advance_current_end_(kInvalidPC),
22       jump_edges_(zone),
23       isolate_(isolate) {}
24 
~RegExpBytecodeGenerator()25 RegExpBytecodeGenerator::~RegExpBytecodeGenerator() {
26   if (backtrack_.is_linked()) backtrack_.Unuse();
27   buffer_.Dispose();
28 }
29 
30 RegExpBytecodeGenerator::IrregexpImplementation
Implementation()31 RegExpBytecodeGenerator::Implementation() {
32   return kBytecodeImplementation;
33 }
34 
Bind(Label * l)35 void RegExpBytecodeGenerator::Bind(Label* l) {
36   advance_current_end_ = kInvalidPC;
37   DCHECK(!l->is_bound());
38   if (l->is_linked()) {
39     int pos = l->pos();
40     while (pos != 0) {
41       int fixup = pos;
42       pos = *reinterpret_cast<int32_t*>(buffer_.begin() + fixup);
43       *reinterpret_cast<uint32_t*>(buffer_.begin() + fixup) = pc_;
44       jump_edges_.emplace(fixup, pc_);
45     }
46   }
47   l->bind_to(pc_);
48 }
49 
EmitOrLink(Label * l)50 void RegExpBytecodeGenerator::EmitOrLink(Label* l) {
51   if (l == nullptr) l = &backtrack_;
52   int pos = 0;
53   if (l->is_bound()) {
54     pos = l->pos();
55     jump_edges_.emplace(pc_, pos);
56   } else {
57     if (l->is_linked()) {
58       pos = l->pos();
59     }
60     l->link_to(pc_);
61   }
62   Emit32(pos);
63 }
64 
PopRegister(int register_index)65 void RegExpBytecodeGenerator::PopRegister(int register_index) {
66   DCHECK_LE(0, register_index);
67   DCHECK_GE(kMaxRegister, register_index);
68   Emit(BC_POP_REGISTER, register_index);
69 }
70 
PushRegister(int register_index,StackCheckFlag check_stack_limit)71 void RegExpBytecodeGenerator::PushRegister(int register_index,
72                                            StackCheckFlag check_stack_limit) {
73   DCHECK_LE(0, register_index);
74   DCHECK_GE(kMaxRegister, register_index);
75   Emit(BC_PUSH_REGISTER, register_index);
76 }
77 
WriteCurrentPositionToRegister(int register_index,int cp_offset)78 void RegExpBytecodeGenerator::WriteCurrentPositionToRegister(int register_index,
79                                                              int cp_offset) {
80   DCHECK_LE(0, register_index);
81   DCHECK_GE(kMaxRegister, register_index);
82   Emit(BC_SET_REGISTER_TO_CP, register_index);
83   Emit32(cp_offset);  // Current position offset.
84 }
85 
ClearRegisters(int reg_from,int reg_to)86 void RegExpBytecodeGenerator::ClearRegisters(int reg_from, int reg_to) {
87   DCHECK(reg_from <= reg_to);
88   for (int reg = reg_from; reg <= reg_to; reg++) {
89     SetRegister(reg, -1);
90   }
91 }
92 
ReadCurrentPositionFromRegister(int register_index)93 void RegExpBytecodeGenerator::ReadCurrentPositionFromRegister(
94     int register_index) {
95   DCHECK_LE(0, register_index);
96   DCHECK_GE(kMaxRegister, register_index);
97   Emit(BC_SET_CP_TO_REGISTER, register_index);
98 }
99 
WriteStackPointerToRegister(int register_index)100 void RegExpBytecodeGenerator::WriteStackPointerToRegister(int register_index) {
101   DCHECK_LE(0, register_index);
102   DCHECK_GE(kMaxRegister, register_index);
103   Emit(BC_SET_REGISTER_TO_SP, register_index);
104 }
105 
ReadStackPointerFromRegister(int register_index)106 void RegExpBytecodeGenerator::ReadStackPointerFromRegister(int register_index) {
107   DCHECK_LE(0, register_index);
108   DCHECK_GE(kMaxRegister, register_index);
109   Emit(BC_SET_SP_TO_REGISTER, register_index);
110 }
111 
SetCurrentPositionFromEnd(int by)112 void RegExpBytecodeGenerator::SetCurrentPositionFromEnd(int by) {
113   DCHECK(is_uint24(by));
114   Emit(BC_SET_CURRENT_POSITION_FROM_END, by);
115 }
116 
SetRegister(int register_index,int to)117 void RegExpBytecodeGenerator::SetRegister(int register_index, int to) {
118   DCHECK_LE(0, register_index);
119   DCHECK_GE(kMaxRegister, register_index);
120   Emit(BC_SET_REGISTER, register_index);
121   Emit32(to);
122 }
123 
AdvanceRegister(int register_index,int by)124 void RegExpBytecodeGenerator::AdvanceRegister(int register_index, int by) {
125   DCHECK_LE(0, register_index);
126   DCHECK_GE(kMaxRegister, register_index);
127   Emit(BC_ADVANCE_REGISTER, register_index);
128   Emit32(by);
129 }
130 
PopCurrentPosition()131 void RegExpBytecodeGenerator::PopCurrentPosition() { Emit(BC_POP_CP, 0); }
132 
PushCurrentPosition()133 void RegExpBytecodeGenerator::PushCurrentPosition() { Emit(BC_PUSH_CP, 0); }
134 
Backtrack()135 void RegExpBytecodeGenerator::Backtrack() {
136   int error_code =
137       can_fallback() ? RegExp::RE_FALLBACK_TO_EXPERIMENTAL : RegExp::RE_FAILURE;
138   Emit(BC_POP_BT, error_code);
139 }
140 
GoTo(Label * l)141 void RegExpBytecodeGenerator::GoTo(Label* l) {
142   if (advance_current_end_ == pc_) {
143     // Combine advance current and goto.
144     pc_ = advance_current_start_;
145     Emit(BC_ADVANCE_CP_AND_GOTO, advance_current_offset_);
146     EmitOrLink(l);
147     advance_current_end_ = kInvalidPC;
148   } else {
149     // Regular goto.
150     Emit(BC_GOTO, 0);
151     EmitOrLink(l);
152   }
153 }
154 
PushBacktrack(Label * l)155 void RegExpBytecodeGenerator::PushBacktrack(Label* l) {
156   Emit(BC_PUSH_BT, 0);
157   EmitOrLink(l);
158 }
159 
Succeed()160 bool RegExpBytecodeGenerator::Succeed() {
161   Emit(BC_SUCCEED, 0);
162   return false;  // Restart matching for global regexp not supported.
163 }
164 
Fail()165 void RegExpBytecodeGenerator::Fail() { Emit(BC_FAIL, 0); }
166 
AdvanceCurrentPosition(int by)167 void RegExpBytecodeGenerator::AdvanceCurrentPosition(int by) {
168   DCHECK_LE(kMinCPOffset, by);
169   DCHECK_GE(kMaxCPOffset, by);
170   advance_current_start_ = pc_;
171   advance_current_offset_ = by;
172   Emit(BC_ADVANCE_CP, by);
173   advance_current_end_ = pc_;
174 }
175 
CheckGreedyLoop(Label * on_tos_equals_current_position)176 void RegExpBytecodeGenerator::CheckGreedyLoop(
177     Label* on_tos_equals_current_position) {
178   Emit(BC_CHECK_GREEDY, 0);
179   EmitOrLink(on_tos_equals_current_position);
180 }
181 
LoadCurrentCharacterImpl(int cp_offset,Label * on_failure,bool check_bounds,int characters,int eats_at_least)182 void RegExpBytecodeGenerator::LoadCurrentCharacterImpl(int cp_offset,
183                                                        Label* on_failure,
184                                                        bool check_bounds,
185                                                        int characters,
186                                                        int eats_at_least) {
187   DCHECK_GE(eats_at_least, characters);
188   if (eats_at_least > characters && check_bounds) {
189     DCHECK(is_int24(cp_offset + eats_at_least));
190     Emit(BC_CHECK_CURRENT_POSITION, cp_offset + eats_at_least);
191     EmitOrLink(on_failure);
192     check_bounds = false;  // Load below doesn't need to check.
193   }
194 
195   DCHECK_LE(kMinCPOffset, cp_offset);
196   DCHECK_GE(kMaxCPOffset, cp_offset);
197   int bytecode;
198   if (check_bounds) {
199     if (characters == 4) {
200       bytecode = BC_LOAD_4_CURRENT_CHARS;
201     } else if (characters == 2) {
202       bytecode = BC_LOAD_2_CURRENT_CHARS;
203     } else {
204       DCHECK_EQ(1, characters);
205       bytecode = BC_LOAD_CURRENT_CHAR;
206     }
207   } else {
208     if (characters == 4) {
209       bytecode = BC_LOAD_4_CURRENT_CHARS_UNCHECKED;
210     } else if (characters == 2) {
211       bytecode = BC_LOAD_2_CURRENT_CHARS_UNCHECKED;
212     } else {
213       DCHECK_EQ(1, characters);
214       bytecode = BC_LOAD_CURRENT_CHAR_UNCHECKED;
215     }
216   }
217   Emit(bytecode, cp_offset);
218   if (check_bounds) EmitOrLink(on_failure);
219 }
220 
CheckCharacterLT(uc16 limit,Label * on_less)221 void RegExpBytecodeGenerator::CheckCharacterLT(uc16 limit, Label* on_less) {
222   Emit(BC_CHECK_LT, limit);
223   EmitOrLink(on_less);
224 }
225 
CheckCharacterGT(uc16 limit,Label * on_greater)226 void RegExpBytecodeGenerator::CheckCharacterGT(uc16 limit, Label* on_greater) {
227   Emit(BC_CHECK_GT, limit);
228   EmitOrLink(on_greater);
229 }
230 
CheckCharacter(uint32_t c,Label * on_equal)231 void RegExpBytecodeGenerator::CheckCharacter(uint32_t c, Label* on_equal) {
232   if (c > MAX_FIRST_ARG) {
233     Emit(BC_CHECK_4_CHARS, 0);
234     Emit32(c);
235   } else {
236     Emit(BC_CHECK_CHAR, c);
237   }
238   EmitOrLink(on_equal);
239 }
240 
CheckAtStart(int cp_offset,Label * on_at_start)241 void RegExpBytecodeGenerator::CheckAtStart(int cp_offset, Label* on_at_start) {
242   Emit(BC_CHECK_AT_START, cp_offset);
243   EmitOrLink(on_at_start);
244 }
245 
CheckNotAtStart(int cp_offset,Label * on_not_at_start)246 void RegExpBytecodeGenerator::CheckNotAtStart(int cp_offset,
247                                               Label* on_not_at_start) {
248   Emit(BC_CHECK_NOT_AT_START, cp_offset);
249   EmitOrLink(on_not_at_start);
250 }
251 
CheckNotCharacter(uint32_t c,Label * on_not_equal)252 void RegExpBytecodeGenerator::CheckNotCharacter(uint32_t c,
253                                                 Label* on_not_equal) {
254   if (c > MAX_FIRST_ARG) {
255     Emit(BC_CHECK_NOT_4_CHARS, 0);
256     Emit32(c);
257   } else {
258     Emit(BC_CHECK_NOT_CHAR, c);
259   }
260   EmitOrLink(on_not_equal);
261 }
262 
CheckCharacterAfterAnd(uint32_t c,uint32_t mask,Label * on_equal)263 void RegExpBytecodeGenerator::CheckCharacterAfterAnd(uint32_t c, uint32_t mask,
264                                                      Label* on_equal) {
265   if (c > MAX_FIRST_ARG) {
266     Emit(BC_AND_CHECK_4_CHARS, 0);
267     Emit32(c);
268   } else {
269     Emit(BC_AND_CHECK_CHAR, c);
270   }
271   Emit32(mask);
272   EmitOrLink(on_equal);
273 }
274 
CheckNotCharacterAfterAnd(uint32_t c,uint32_t mask,Label * on_not_equal)275 void RegExpBytecodeGenerator::CheckNotCharacterAfterAnd(uint32_t c,
276                                                         uint32_t mask,
277                                                         Label* on_not_equal) {
278   if (c > MAX_FIRST_ARG) {
279     Emit(BC_AND_CHECK_NOT_4_CHARS, 0);
280     Emit32(c);
281   } else {
282     Emit(BC_AND_CHECK_NOT_CHAR, c);
283   }
284   Emit32(mask);
285   EmitOrLink(on_not_equal);
286 }
287 
CheckNotCharacterAfterMinusAnd(uc16 c,uc16 minus,uc16 mask,Label * on_not_equal)288 void RegExpBytecodeGenerator::CheckNotCharacterAfterMinusAnd(
289     uc16 c, uc16 minus, uc16 mask, Label* on_not_equal) {
290   Emit(BC_MINUS_AND_CHECK_NOT_CHAR, c);
291   Emit16(minus);
292   Emit16(mask);
293   EmitOrLink(on_not_equal);
294 }
295 
CheckCharacterInRange(uc16 from,uc16 to,Label * on_in_range)296 void RegExpBytecodeGenerator::CheckCharacterInRange(uc16 from, uc16 to,
297                                                     Label* on_in_range) {
298   Emit(BC_CHECK_CHAR_IN_RANGE, 0);
299   Emit16(from);
300   Emit16(to);
301   EmitOrLink(on_in_range);
302 }
303 
CheckCharacterNotInRange(uc16 from,uc16 to,Label * on_not_in_range)304 void RegExpBytecodeGenerator::CheckCharacterNotInRange(uc16 from, uc16 to,
305                                                        Label* on_not_in_range) {
306   Emit(BC_CHECK_CHAR_NOT_IN_RANGE, 0);
307   Emit16(from);
308   Emit16(to);
309   EmitOrLink(on_not_in_range);
310 }
311 
CheckBitInTable(Handle<ByteArray> table,Label * on_bit_set)312 void RegExpBytecodeGenerator::CheckBitInTable(Handle<ByteArray> table,
313                                               Label* on_bit_set) {
314   Emit(BC_CHECK_BIT_IN_TABLE, 0);
315   EmitOrLink(on_bit_set);
316   for (int i = 0; i < kTableSize; i += kBitsPerByte) {
317     int byte = 0;
318     for (int j = 0; j < kBitsPerByte; j++) {
319       if (table->get(i + j) != 0) byte |= 1 << j;
320     }
321     Emit8(byte);
322   }
323 }
324 
CheckNotBackReference(int start_reg,bool read_backward,Label * on_not_equal)325 void RegExpBytecodeGenerator::CheckNotBackReference(int start_reg,
326                                                     bool read_backward,
327                                                     Label* on_not_equal) {
328   DCHECK_LE(0, start_reg);
329   DCHECK_GE(kMaxRegister, start_reg);
330   Emit(read_backward ? BC_CHECK_NOT_BACK_REF_BACKWARD : BC_CHECK_NOT_BACK_REF,
331        start_reg);
332   EmitOrLink(on_not_equal);
333 }
334 
CheckNotBackReferenceIgnoreCase(int start_reg,bool read_backward,bool unicode,Label * on_not_equal)335 void RegExpBytecodeGenerator::CheckNotBackReferenceIgnoreCase(
336     int start_reg, bool read_backward, bool unicode, Label* on_not_equal) {
337   DCHECK_LE(0, start_reg);
338   DCHECK_GE(kMaxRegister, start_reg);
339   Emit(read_backward ? (unicode ? BC_CHECK_NOT_BACK_REF_NO_CASE_UNICODE_BACKWARD
340                                 : BC_CHECK_NOT_BACK_REF_NO_CASE_BACKWARD)
341                      : (unicode ? BC_CHECK_NOT_BACK_REF_NO_CASE_UNICODE
342                                 : BC_CHECK_NOT_BACK_REF_NO_CASE),
343        start_reg);
344   EmitOrLink(on_not_equal);
345 }
346 
IfRegisterLT(int register_index,int comparand,Label * on_less_than)347 void RegExpBytecodeGenerator::IfRegisterLT(int register_index, int comparand,
348                                            Label* on_less_than) {
349   DCHECK_LE(0, register_index);
350   DCHECK_GE(kMaxRegister, register_index);
351   Emit(BC_CHECK_REGISTER_LT, register_index);
352   Emit32(comparand);
353   EmitOrLink(on_less_than);
354 }
355 
IfRegisterGE(int register_index,int comparand,Label * on_greater_or_equal)356 void RegExpBytecodeGenerator::IfRegisterGE(int register_index, int comparand,
357                                            Label* on_greater_or_equal) {
358   DCHECK_LE(0, register_index);
359   DCHECK_GE(kMaxRegister, register_index);
360   Emit(BC_CHECK_REGISTER_GE, register_index);
361   Emit32(comparand);
362   EmitOrLink(on_greater_or_equal);
363 }
364 
IfRegisterEqPos(int register_index,Label * on_eq)365 void RegExpBytecodeGenerator::IfRegisterEqPos(int register_index,
366                                               Label* on_eq) {
367   DCHECK_LE(0, register_index);
368   DCHECK_GE(kMaxRegister, register_index);
369   Emit(BC_CHECK_REGISTER_EQ_POS, register_index);
370   EmitOrLink(on_eq);
371 }
372 
GetCode(Handle<String> source)373 Handle<HeapObject> RegExpBytecodeGenerator::GetCode(Handle<String> source) {
374   Bind(&backtrack_);
375   Backtrack();
376 
377   Handle<ByteArray> array;
378   if (FLAG_regexp_peephole_optimization) {
379     array = RegExpBytecodePeepholeOptimization::OptimizeBytecode(
380         isolate_, zone(), source, buffer_.begin(), length(), jump_edges_);
381   } else {
382     array = isolate_->factory()->NewByteArray(length());
383     Copy(array->GetDataStartAddress());
384   }
385 
386   return array;
387 }
388 
length()389 int RegExpBytecodeGenerator::length() { return pc_; }
390 
Copy(byte * a)391 void RegExpBytecodeGenerator::Copy(byte* a) {
392   MemCopy(a, buffer_.begin(), length());
393 }
394 
Expand()395 void RegExpBytecodeGenerator::Expand() {
396   Vector<byte> old_buffer = buffer_;
397   buffer_ = Vector<byte>::New(old_buffer.length() * 2);
398   MemCopy(buffer_.begin(), old_buffer.begin(), old_buffer.length());
399   old_buffer.Dispose();
400 }
401 
402 }  // namespace internal
403 }  // namespace v8
404