v8  3.25.30(node0.11.13)
V8 is Google's open source JavaScript engine
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
regexp-macro-assembler-tracer.cc
Go to the documentation of this file.
1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 #include "v8.h"
29 #include "ast.h"
30 #include "regexp-macro-assembler.h"
32 
33 namespace v8 {
34 namespace internal {
35 
37  RegExpMacroAssembler* assembler) :
38  RegExpMacroAssembler(assembler->zone()),
39  assembler_(assembler) {
40  unsigned int type = assembler->Implementation();
41  ASSERT(type < 6);
42  const char* impl_names[] = {"IA32", "ARM", "ARM64",
43  "MIPS", "X64", "Bytecode"};
44  PrintF("RegExpMacroAssembler%s();\n", impl_names[type]);
45 }
46 
47 
49 }
50 
51 
52 // This is used for printing out debugging information. It makes an integer
53 // that is closely related to the address of an object.
54 static int LabelToInt(Label* label) {
55  return static_cast<int>(reinterpret_cast<intptr_t>(label));
56 }
57 
58 
60  PrintF("label[%08x]: (Bind)\n", LabelToInt(label));
61  assembler_->Bind(label);
62 }
63 
64 
66  PrintF(" AdvanceCurrentPosition(by=%d);\n", by);
67  assembler_->AdvanceCurrentPosition(by);
68 }
69 
70 
72  PrintF(" CheckGreedyLoop(label[%08x]);\n\n", LabelToInt(label));
73  assembler_->CheckGreedyLoop(label);
74 }
75 
76 
78  PrintF(" PopCurrentPosition();\n");
79  assembler_->PopCurrentPosition();
80 }
81 
82 
84  PrintF(" PushCurrentPosition();\n");
85  assembler_->PushCurrentPosition();
86 }
87 
88 
90  PrintF(" Backtrack();\n");
91  assembler_->Backtrack();
92 }
93 
94 
96  PrintF(" GoTo(label[%08x]);\n\n", LabelToInt(label));
97  assembler_->GoTo(label);
98 }
99 
100 
102  PrintF(" PushBacktrack(label[%08x]);\n", LabelToInt(label));
103  assembler_->PushBacktrack(label);
104 }
105 
106 
108  bool restart = assembler_->Succeed();
109  PrintF(" Succeed();%s\n", restart ? " [restart for global match]" : "");
110  return restart;
111 }
112 
113 
115  PrintF(" Fail();");
116  assembler_->Fail();
117 }
118 
119 
120 void RegExpMacroAssemblerTracer::PopRegister(int register_index) {
121  PrintF(" PopRegister(register=%d);\n", register_index);
122  assembler_->PopRegister(register_index);
123 }
124 
125 
127  int register_index,
128  StackCheckFlag check_stack_limit) {
129  PrintF(" PushRegister(register=%d, %s);\n",
130  register_index,
131  check_stack_limit ? "check stack limit" : "");
132  assembler_->PushRegister(register_index, check_stack_limit);
133 }
134 
135 
137  PrintF(" AdvanceRegister(register=%d, by=%d);\n", reg, by);
138  assembler_->AdvanceRegister(reg, by);
139 }
140 
141 
143  PrintF(" SetCurrentPositionFromEnd(by=%d);\n", by);
144  assembler_->SetCurrentPositionFromEnd(by);
145 }
146 
147 
148 void RegExpMacroAssemblerTracer::SetRegister(int register_index, int to) {
149  PrintF(" SetRegister(register=%d, to=%d);\n", register_index, to);
150  assembler_->SetRegister(register_index, to);
151 }
152 
153 
155  int cp_offset) {
156  PrintF(" WriteCurrentPositionToRegister(register=%d,cp_offset=%d);\n",
157  reg,
158  cp_offset);
159  assembler_->WriteCurrentPositionToRegister(reg, cp_offset);
160 }
161 
162 
163 void RegExpMacroAssemblerTracer::ClearRegisters(int reg_from, int reg_to) {
164  PrintF(" ClearRegister(from=%d, to=%d);\n", reg_from, reg_to);
165  assembler_->ClearRegisters(reg_from, reg_to);
166 }
167 
168 
170  PrintF(" ReadCurrentPositionFromRegister(register=%d);\n", reg);
171  assembler_->ReadCurrentPositionFromRegister(reg);
172 }
173 
174 
176  PrintF(" WriteStackPointerToRegister(register=%d);\n", reg);
177  assembler_->WriteStackPointerToRegister(reg);
178 }
179 
180 
182  PrintF(" ReadStackPointerFromRegister(register=%d);\n", reg);
183  assembler_->ReadStackPointerFromRegister(reg);
184 }
185 
186 
188  Label* on_end_of_input,
189  bool check_bounds,
190  int characters) {
191  const char* check_msg = check_bounds ? "" : " (unchecked)";
192  PrintF(" LoadCurrentCharacter(cp_offset=%d, label[%08x]%s (%d chars));\n",
193  cp_offset,
194  LabelToInt(on_end_of_input),
195  check_msg,
196  characters);
197  assembler_->LoadCurrentCharacter(cp_offset,
198  on_end_of_input,
199  check_bounds,
200  characters);
201 }
202 
203 
205  public:
206  explicit PrintablePrinter(uc16 character) : character_(character) { }
207 
208  const char* operator*() {
209  if (character_ >= ' ' && character_ <= '~') {
210  buffer_[0] = '(';
211  buffer_[1] = static_cast<char>(character_);
212  buffer_[2] = ')';
213  buffer_[3] = '\0';
214  } else {
215  buffer_[0] = '\0';
216  }
217  return &buffer_[0];
218  };
219 
220  private:
221  uc16 character_;
222  char buffer_[4];
223 };
224 
225 
227  PrintablePrinter printable(limit);
228  PrintF(" CheckCharacterLT(c=0x%04x%s, label[%08x]);\n",
229  limit,
230  *printable,
231  LabelToInt(on_less));
232  assembler_->CheckCharacterLT(limit, on_less);
233 }
234 
235 
237  Label* on_greater) {
238  PrintablePrinter printable(limit);
239  PrintF(" CheckCharacterGT(c=0x%04x%s, label[%08x]);\n",
240  limit,
241  *printable,
242  LabelToInt(on_greater));
243  assembler_->CheckCharacterGT(limit, on_greater);
244 }
245 
246 
247 void RegExpMacroAssemblerTracer::CheckCharacter(unsigned c, Label* on_equal) {
248  PrintablePrinter printable(c);
249  PrintF(" CheckCharacter(c=0x%04x%s, label[%08x]);\n",
250  c,
251  *printable,
252  LabelToInt(on_equal));
253  assembler_->CheckCharacter(c, on_equal);
254 }
255 
256 
258  PrintF(" CheckAtStart(label[%08x]);\n", LabelToInt(on_at_start));
259  assembler_->CheckAtStart(on_at_start);
260 }
261 
262 
263 void RegExpMacroAssemblerTracer::CheckNotAtStart(Label* on_not_at_start) {
264  PrintF(" CheckNotAtStart(label[%08x]);\n", LabelToInt(on_not_at_start));
265  assembler_->CheckNotAtStart(on_not_at_start);
266 }
267 
268 
270  Label* on_not_equal) {
271  PrintablePrinter printable(c);
272  PrintF(" CheckNotCharacter(c=0x%04x%s, label[%08x]);\n",
273  c,
274  *printable,
275  LabelToInt(on_not_equal));
276  assembler_->CheckNotCharacter(c, on_not_equal);
277 }
278 
279 
281  unsigned c,
282  unsigned mask,
283  Label* on_equal) {
284  PrintablePrinter printable(c);
285  PrintF(" CheckCharacterAfterAnd(c=0x%04x%s, mask=0x%04x, label[%08x]);\n",
286  c,
287  *printable,
288  mask,
289  LabelToInt(on_equal));
290  assembler_->CheckCharacterAfterAnd(c, mask, on_equal);
291 }
292 
293 
295  unsigned c,
296  unsigned mask,
297  Label* on_not_equal) {
298  PrintablePrinter printable(c);
299  PrintF(" CheckNotCharacterAfterAnd(c=0x%04x%s, mask=0x%04x, label[%08x]);\n",
300  c,
301  *printable,
302  mask,
303  LabelToInt(on_not_equal));
304  assembler_->CheckNotCharacterAfterAnd(c, mask, on_not_equal);
305 }
306 
307 
309  uc16 c,
310  uc16 minus,
311  uc16 mask,
312  Label* on_not_equal) {
313  PrintF(" CheckNotCharacterAfterMinusAnd(c=0x%04x, minus=%04x, mask=0x%04x, "
314  "label[%08x]);\n",
315  c,
316  minus,
317  mask,
318  LabelToInt(on_not_equal));
319  assembler_->CheckNotCharacterAfterMinusAnd(c, minus, mask, on_not_equal);
320 }
321 
322 
324  uc16 from,
325  uc16 to,
326  Label* on_not_in_range) {
327  PrintablePrinter printable_from(from);
328  PrintablePrinter printable_to(to);
329  PrintF(" CheckCharacterInRange(from=0x%04x%s, to=0x%04x%s, label[%08x]);\n",
330  from,
331  *printable_from,
332  to,
333  *printable_to,
334  LabelToInt(on_not_in_range));
335  assembler_->CheckCharacterInRange(from, to, on_not_in_range);
336 }
337 
338 
340  uc16 from,
341  uc16 to,
342  Label* on_in_range) {
343  PrintablePrinter printable_from(from);
344  PrintablePrinter printable_to(to);
345  PrintF(
346  " CheckCharacterNotInRange(from=0x%04x%s," " to=%04x%s, label[%08x]);\n",
347  from,
348  *printable_from,
349  to,
350  *printable_to,
351  LabelToInt(on_in_range));
352  assembler_->CheckCharacterNotInRange(from, to, on_in_range);
353 }
354 
355 
357  Handle<ByteArray> table, Label* on_bit_set) {
358  PrintF(" CheckBitInTable(label[%08x] ", LabelToInt(on_bit_set));
359  for (int i = 0; i < kTableSize; i++) {
360  PrintF("%c", table->get(i) != 0 ? 'X' : '.');
361  if (i % 32 == 31 && i != kTableMask) {
362  PrintF("\n ");
363  }
364  }
365  PrintF(");\n");
366  assembler_->CheckBitInTable(table, on_bit_set);
367 }
368 
369 
371  Label* on_no_match) {
372  PrintF(" CheckNotBackReference(register=%d, label[%08x]);\n", start_reg,
373  LabelToInt(on_no_match));
374  assembler_->CheckNotBackReference(start_reg, on_no_match);
375 }
376 
377 
379  int start_reg,
380  Label* on_no_match) {
381  PrintF(" CheckNotBackReferenceIgnoreCase(register=%d, label[%08x]);\n",
382  start_reg, LabelToInt(on_no_match));
383  assembler_->CheckNotBackReferenceIgnoreCase(start_reg, on_no_match);
384 }
385 
386 
388  uc16 type,
389  Label* on_no_match) {
390  bool supported = assembler_->CheckSpecialCharacterClass(type,
391  on_no_match);
392  PrintF(" CheckSpecialCharacterClass(type='%c', label[%08x]): %s;\n",
393  type,
394  LabelToInt(on_no_match),
395  supported ? "true" : "false");
396  return supported;
397 }
398 
399 
401  int comparand, Label* if_lt) {
402  PrintF(" IfRegisterLT(register=%d, number=%d, label[%08x]);\n",
403  register_index, comparand, LabelToInt(if_lt));
404  assembler_->IfRegisterLT(register_index, comparand, if_lt);
405 }
406 
407 
409  Label* if_eq) {
410  PrintF(" IfRegisterEqPos(register=%d, label[%08x]);\n",
411  register_index, LabelToInt(if_eq));
412  assembler_->IfRegisterEqPos(register_index, if_eq);
413 }
414 
415 
417  int comparand, Label* if_ge) {
418  PrintF(" IfRegisterGE(register=%d, number=%d, label[%08x]);\n",
419  register_index, comparand, LabelToInt(if_ge));
420  assembler_->IfRegisterGE(register_index, comparand, if_ge);
421 }
422 
423 
426  return assembler_->Implementation();
427 }
428 
429 
431  PrintF(" GetCode(%s);\n", source->ToCString().get());
432  return assembler_->GetCode(source);
433 }
434 
435 }} // namespace v8::internal
virtual void WriteStackPointerToRegister(int reg)=0
virtual IrregexpImplementation Implementation()=0
virtual void CheckNotBackReferenceIgnoreCase(int start_reg, Label *on_no_match)
virtual void GoTo(Label *label)=0
virtual void CheckCharacterLT(uc16 limit, Label *on_less)
virtual void IfRegisterLT(int reg, int comparand, Label *if_lt)
virtual void ClearRegisters(int reg_from, int reg_to)
void PrintF(const char *format,...)
Definition: v8utils.cc:40
virtual void CheckNotBackReference(int start_reg, Label *on_no_match)=0
virtual void IfRegisterEqPos(int reg, Label *if_eq)
virtual void SetRegister(int register_index, int to)=0
virtual void ClearRegisters(int reg_from, int reg_to)=0
virtual void CheckCharacterNotInRange(uc16 from, uc16 to, Label *on_not_in_range)
virtual void LoadCurrentCharacter(int cp_offset, Label *on_end_of_input, bool check_bounds=true, int characters=1)=0
#define ASSERT(condition)
Definition: checks.h:329
virtual void ReadCurrentPositionFromRegister(int reg)=0
virtual void CheckNotCharacterAfterAnd(unsigned c, unsigned and_with, Label *on_not_equal)
virtual void AdvanceCurrentPosition(int by)=0
virtual void CheckNotCharacterAfterMinusAnd(uc16 c, uc16 minus, uc16 and_with, Label *on_not_equal)
virtual void WriteCurrentPositionToRegister(int reg, int cp_offset)
virtual void CheckBitInTable(Handle< ByteArray > table, Label *on_bit_set)
virtual void CheckCharacterGT(uc16 limit, Label *on_greater)=0
virtual void CheckGreedyLoop(Label *on_tos_equals_current_position)
virtual void CheckNotAtStart(Label *on_not_at_start)
virtual void PushRegister(int register_index, StackCheckFlag check_stack_limit)=0
virtual void ReadStackPointerFromRegister(int reg)=0
virtual void CheckAtStart(Label *on_at_start)=0
virtual void CheckCharacterNotInRange(uc16 from, uc16 to, Label *on_not_in_range)=0
virtual void IfRegisterLT(int reg, int comparand, Label *if_lt)=0
virtual void CheckNotBackReferenceIgnoreCase(int start_reg, Label *on_no_match)=0
virtual void LoadCurrentCharacter(int cp_offset, Label *on_end_of_input, bool check_bounds=true, int characters=1)
virtual void CheckCharacter(unsigned c, Label *on_equal)
virtual void CheckNotCharacterAfterMinusAnd(uc16 c, uc16 minus, uc16 and_with, Label *on_not_equal)=0
virtual void CheckNotAtStart(Label *on_not_at_start)=0
virtual void CheckNotBackReference(int start_reg, Label *on_no_match)
virtual void CheckCharacterAfterAnd(unsigned c, unsigned and_with, Label *on_equal)
virtual void CheckCharacter(unsigned c, Label *on_equal)=0
virtual Handle< HeapObject > GetCode(Handle< String > source)
virtual void AdvanceRegister(int reg, int by)=0
virtual void PopRegister(int register_index)=0
virtual void IfRegisterGE(int reg, int comparand, Label *if_ge)
virtual bool CheckSpecialCharacterClass(uc16 type, Label *on_no_match)
virtual void WriteCurrentPositionToRegister(int reg, int cp_offset)=0
virtual bool CheckSpecialCharacterClass(uc16 type, Label *on_no_match)
virtual void Bind(Label *label)=0
uint16_t uc16
Definition: globals.h:309
virtual void CheckCharacterLT(uc16 limit, Label *on_less)=0
virtual void SetCurrentPositionFromEnd(int by)=0
virtual void CheckCharacterAfterAnd(unsigned c, unsigned and_with, Label *on_equal)=0
virtual void IfRegisterEqPos(int reg, Label *if_eq)=0
virtual void CheckBitInTable(Handle< ByteArray > table, Label *on_bit_set)=0
virtual void CheckCharacterInRange(uc16 from, uc16 to, Label *on_in_range)
virtual void CheckNotCharacter(unsigned c, Label *on_not_equal)
virtual void CheckGreedyLoop(Label *on_tos_equals_current_position)=0
virtual void PushRegister(int register_index, StackCheckFlag check_stack_limit)
RegExpMacroAssemblerTracer(RegExpMacroAssembler *assembler)
virtual void CheckNotCharacter(unsigned c, Label *on_not_equal)=0
virtual void SetRegister(int register_index, int to)
virtual void CheckNotCharacterAfterAnd(unsigned c, unsigned and_with, Label *on_not_equal)=0
virtual void CheckCharacterInRange(uc16 from, uc16 to, Label *on_in_range)=0
virtual Handle< HeapObject > GetCode(Handle< String > source)=0
virtual void IfRegisterGE(int reg, int comparand, Label *if_ge)=0
virtual void PushBacktrack(Label *label)=0
virtual void CheckCharacterGT(uc16 limit, Label *on_greater)