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.h
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 #ifndef V8_REGEXP_MACRO_ASSEMBLER_H_
29 #define V8_REGEXP_MACRO_ASSEMBLER_H_
30 
31 #include "ast.h"
32 
33 namespace v8 {
34 namespace internal {
35 
37  RegExpCharacterClass cc;
38  Label* on_match;
39 };
40 
41 
43  public:
44  // The implementation must be able to handle at least:
45  static const int kMaxRegister = (1 << 16) - 1;
46  static const int kMaxCPOffset = (1 << 15) - 1;
47  static const int kMinCPOffset = -(1 << 15);
48 
49  static const int kTableSizeBits = 7;
50  static const int kTableSize = 1 << kTableSizeBits;
51  static const int kTableMask = kTableSize - 1;
52 
60  };
61 
65  };
66 
67  explicit RegExpMacroAssembler(Zone* zone);
68  virtual ~RegExpMacroAssembler();
69  // The maximal number of pushes between stack checks. Users must supply
70  // kCheckStackLimit flag to push operations (instead of kNoStackLimitCheck)
71  // at least once for every stack_limit() pushes that are executed.
72  virtual int stack_limit_slack() = 0;
73  virtual bool CanReadUnaligned();
74  virtual void AdvanceCurrentPosition(int by) = 0; // Signed cp change.
75  virtual void AdvanceRegister(int reg, int by) = 0; // r[reg] += by.
76  // Continues execution from the position pushed on the top of the backtrack
77  // stack by an earlier PushBacktrack(Label*).
78  virtual void Backtrack() = 0;
79  virtual void Bind(Label* label) = 0;
80  virtual void CheckAtStart(Label* on_at_start) = 0;
81  // Dispatch after looking the current character up in a 2-bits-per-entry
82  // map. The destinations vector has up to 4 labels.
83  virtual void CheckCharacter(unsigned c, Label* on_equal) = 0;
84  // Bitwise and the current character with the given constant and then
85  // check for a match with c.
86  virtual void CheckCharacterAfterAnd(unsigned c,
87  unsigned and_with,
88  Label* on_equal) = 0;
89  virtual void CheckCharacterGT(uc16 limit, Label* on_greater) = 0;
90  virtual void CheckCharacterLT(uc16 limit, Label* on_less) = 0;
91  virtual void CheckGreedyLoop(Label* on_tos_equals_current_position) = 0;
92  virtual void CheckNotAtStart(Label* on_not_at_start) = 0;
93  virtual void CheckNotBackReference(int start_reg, Label* on_no_match) = 0;
94  virtual void CheckNotBackReferenceIgnoreCase(int start_reg,
95  Label* on_no_match) = 0;
96  // Check the current character for a match with a literal character. If we
97  // fail to match then goto the on_failure label. End of input always
98  // matches. If the label is NULL then we should pop a backtrack address off
99  // the stack and go to that.
100  virtual void CheckNotCharacter(unsigned c, Label* on_not_equal) = 0;
101  virtual void CheckNotCharacterAfterAnd(unsigned c,
102  unsigned and_with,
103  Label* on_not_equal) = 0;
104  // Subtract a constant from the current character, then and with the given
105  // constant and then check for a match with c.
106  virtual void CheckNotCharacterAfterMinusAnd(uc16 c,
107  uc16 minus,
108  uc16 and_with,
109  Label* on_not_equal) = 0;
110  virtual void CheckCharacterInRange(uc16 from,
111  uc16 to, // Both inclusive.
112  Label* on_in_range) = 0;
113  virtual void CheckCharacterNotInRange(uc16 from,
114  uc16 to, // Both inclusive.
115  Label* on_not_in_range) = 0;
116 
117  // The current character (modulus the kTableSize) is looked up in the byte
118  // array, and if the found byte is non-zero, we jump to the on_bit_set label.
119  virtual void CheckBitInTable(Handle<ByteArray> table, Label* on_bit_set) = 0;
120 
121  // Checks whether the given offset from the current position is before
122  // the end of the string. May overwrite the current character.
123  virtual void CheckPosition(int cp_offset, Label* on_outside_input) {
124  LoadCurrentCharacter(cp_offset, on_outside_input, true);
125  }
126  // Check whether a standard/default character class matches the current
127  // character. Returns false if the type of special character class does
128  // not have custom support.
129  // May clobber the current loaded character.
130  virtual bool CheckSpecialCharacterClass(uc16 type,
131  Label* on_no_match) {
132  return false;
133  }
134  virtual void Fail() = 0;
135  virtual Handle<HeapObject> GetCode(Handle<String> source) = 0;
136  virtual void GoTo(Label* label) = 0;
137  // Check whether a register is >= a given constant and go to a label if it
138  // is. Backtracks instead if the label is NULL.
139  virtual void IfRegisterGE(int reg, int comparand, Label* if_ge) = 0;
140  // Check whether a register is < a given constant and go to a label if it is.
141  // Backtracks instead if the label is NULL.
142  virtual void IfRegisterLT(int reg, int comparand, Label* if_lt) = 0;
143  // Check whether a register is == to the current position and go to a
144  // label if it is.
145  virtual void IfRegisterEqPos(int reg, Label* if_eq) = 0;
147  virtual void LoadCurrentCharacter(int cp_offset,
148  Label* on_end_of_input,
149  bool check_bounds = true,
150  int characters = 1) = 0;
151  virtual void PopCurrentPosition() = 0;
152  virtual void PopRegister(int register_index) = 0;
153  // Pushes the label on the backtrack stack, so that a following Backtrack
154  // will go to this label. Always checks the backtrack stack limit.
155  virtual void PushBacktrack(Label* label) = 0;
156  virtual void PushCurrentPosition() = 0;
157  virtual void PushRegister(int register_index,
158  StackCheckFlag check_stack_limit) = 0;
159  virtual void ReadCurrentPositionFromRegister(int reg) = 0;
160  virtual void ReadStackPointerFromRegister(int reg) = 0;
161  virtual void SetCurrentPositionFromEnd(int by) = 0;
162  virtual void SetRegister(int register_index, int to) = 0;
163  // Return whether the matching (with a global regexp) will be restarted.
164  virtual bool Succeed() = 0;
165  virtual void WriteCurrentPositionToRegister(int reg, int cp_offset) = 0;
166  virtual void ClearRegisters(int reg_from, int reg_to) = 0;
167  virtual void WriteStackPointerToRegister(int reg) = 0;
168 
169  // Controls the generation of large inlined constants in the code.
170  void set_slow_safe(bool ssc) { slow_safe_compiler_ = ssc; }
171  bool slow_safe() { return slow_safe_compiler_; }
172 
174  // Set whether the regular expression has the global flag. Exiting due to
175  // a failure in a global regexp may still mean success overall.
176  inline void set_global_mode(GlobalMode mode) { global_mode_ = mode; }
177  inline bool global() { return global_mode_ != NOT_GLOBAL; }
179  return global_mode_ == GLOBAL;
180  }
181 
182  Zone* zone() const { return zone_; }
183 
184  private:
185  bool slow_safe_compiler_;
186  bool global_mode_;
187  Zone* zone_;
188 };
189 
190 
191 #ifndef V8_INTERPRETED_REGEXP // Avoid compiling unused code.
192 
194  public:
195  // Type of input string to generate code for.
196  enum Mode { ASCII = 1, UC16 = 2 };
197 
198  // Result of calling generated native RegExp code.
199  // RETRY: Something significant changed during execution, and the matching
200  // should be retried from scratch.
201  // EXCEPTION: Something failed during execution. If no exception has been
202  // thrown, it's an internal out-of-memory, and the caller should
203  // throw the exception.
204  // FAILURE: Matching failed.
205  // SUCCESS: Matching succeeded, and the output array has been filled with
206  // capture positions.
207  enum Result { RETRY = -2, EXCEPTION = -1, FAILURE = 0, SUCCESS = 1 };
208 
210  virtual ~NativeRegExpMacroAssembler();
211  virtual bool CanReadUnaligned();
212 
213  static Result Match(Handle<Code> regexp,
214  Handle<String> subject,
215  int* offsets_vector,
216  int offsets_vector_length,
217  int previous_index,
218  Isolate* isolate);
219 
220  // Compares two-byte strings case insensitively.
221  // Called from generated RegExp code.
222  static int CaseInsensitiveCompareUC16(Address byte_offset1,
223  Address byte_offset2,
224  size_t byte_length,
225  Isolate* isolate);
226 
227  // Called from RegExp if the backtrack stack limit is hit.
228  // Tries to expand the stack. Returns the new stack-pointer if
229  // successful, and updates the stack_top address, or returns 0 if unable
230  // to grow the stack.
231  // This function must not trigger a garbage collection.
232  static Address GrowStack(Address stack_pointer, Address* stack_top,
233  Isolate* isolate);
234 
235  static const byte* StringCharacterPosition(String* subject, int start_index);
236 
237  // Byte map of one byte characters with a 0xff if the character is a word
238  // character (digit, letter or underscore) and 0x00 otherwise.
239  // Used by generated RegExp code.
240  static const byte word_character_map[256];
241 
243  return const_cast<Address>(&word_character_map[0]);
244  }
245 
246  static Result Execute(Code* code,
247  String* input,
248  int start_offset,
249  const byte* input_start,
250  const byte* input_end,
251  int* output,
252  int output_size,
253  Isolate* isolate);
254 };
255 
256 #endif // V8_INTERPRETED_REGEXP
257 
258 } } // namespace v8::internal
259 
260 #endif // V8_REGEXP_MACRO_ASSEMBLER_H_
byte * Address
Definition: globals.h:186
virtual void WriteStackPointerToRegister(int reg)=0
virtual IrregexpImplementation Implementation()=0
virtual void GoTo(Label *label)=0
static Result Execute(Code *code, String *input, int start_offset, const byte *input_start, const byte *input_end, int *output, int output_size, Isolate *isolate)
virtual void CheckNotBackReference(int start_reg, Label *on_no_match)=0
virtual void SetRegister(int register_index, int to)=0
virtual void ClearRegisters(int reg_from, int reg_to)=0
virtual void LoadCurrentCharacter(int cp_offset, Label *on_end_of_input, bool check_bounds=true, int characters=1)=0
virtual void ReadCurrentPositionFromRegister(int reg)=0
virtual void AdvanceCurrentPosition(int by)=0
uint8_t byte
Definition: globals.h:185
enable upcoming ES6 features enable harmony block scoping enable harmony enable harmony proxies enable harmony generators enable harmony numeric enable harmony string enable harmony math functions harmony_scoping harmony_symbols harmony_collections harmony_iteration harmony_strings harmony_scoping harmony_maths tracks arrays with only smi values Optimize object Array DOM strings and string pretenure call new trace pretenuring decisions of HAllocate instructions track fields with only smi values track fields with heap values track_fields track_fields Enables optimizations which favor memory size over execution speed use string slices optimization filter maximum number of GVN fix point iterations use function inlining use allocation folding eliminate write barriers targeting allocations in optimized code maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining crankshaft harvests type feedback from stub cache trace check elimination phase hydrogen tracing filter trace hydrogen to given file name trace inlining decisions trace store elimination trace all use positions trace global value numbering trace hydrogen escape analysis trace the tracking of allocation sites trace map generalization environment for every instruction deoptimize every n garbage collections put a break point before deoptimizing deoptimize uncommon cases use on stack replacement trace array bounds check elimination perform array index dehoisting use load elimination use store elimination use constant folding eliminate unreachable code number of stress runs when picking a function to watch for shared function not JSFunction itself flushes the cache of optimized code for closures on every GC functions with arguments object maximum number of escape analysis fix point iterations allow uint32 values on optimize frames if they are used only in safe operations track concurrent recompilation artificial compilation delay in ms concurrent on stack replacement do not emit check maps for constant values that have a leaf deoptimize the optimized code if the layout of the maps changes number of stack frames inspected by the profiler percentage of ICs that must have type info to allow optimization extra verbose compilation tracing generate extra emit comments in code disassembly enable use of SSE3 instructions if available enable use of CMOV instruction if available enable use of VFP3 instructions if available enable use of NEON instructions if enable use of SDIV and UDIV instructions if enable loading bit constant by means of movw movt instruction enable unaligned accesses for enable use of d16 d31 registers on ARM this requires VFP3 force all emitted branches to be in long mode(MIPS only)") DEFINE_string(expose_natives_as
virtual void CheckCharacterGT(uc16 limit, Label *on_greater)=0
static int CaseInsensitiveCompareUC16(Address byte_offset1, Address byte_offset2, size_t byte_length, Isolate *isolate)
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
enable upcoming ES6 features enable harmony block scoping enable harmony enable harmony proxies enable harmony generators enable harmony numeric enable harmony string enable harmony math functions harmony_scoping harmony_symbols harmony_collections harmony_iteration harmony_strings harmony_scoping harmony_maths tracks arrays with only smi values Optimize object Array DOM strings and string pretenure call new trace pretenuring decisions of HAllocate instructions track fields with only smi values track fields with heap values track_fields track_fields Enables optimizations which favor memory size over execution speed use string slices optimization filter maximum number of GVN fix point iterations use function inlining use allocation folding eliminate write barriers targeting allocations in optimized code maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining crankshaft harvests type feedback from stub cache trace check elimination phase hydrogen tracing filter trace hydrogen to given file name trace inlining decisions trace store elimination trace all use positions trace global value numbering trace hydrogen escape analysis trace the tracking of allocation sites trace map generalization environment for every instruction deoptimize every n garbage collections put a break point before deoptimizing deoptimize uncommon cases use on stack replacement trace array bounds check elimination perform array index dehoisting use load elimination use store elimination use constant folding eliminate unreachable code number of stress runs when picking a function to watch for shared function not JSFunction itself flushes the cache of optimized code for closures on every GC functions with arguments object maximum number of escape analysis fix point iterations allow uint32 values on optimize frames if they are used only in safe operations track concurrent recompilation artificial compilation delay in ms concurrent on stack replacement do not emit check maps for constant values that have a leaf deoptimize the optimized code if the layout of the maps changes number of stack frames inspected by the profiler percentage of ICs that must have type info to allow optimization extra verbose compilation tracing generate extra code(assertions) for debugging") DEFINE_bool(code_comments
virtual void CheckNotBackReferenceIgnoreCase(int start_reg, Label *on_no_match)=0
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 CheckCharacter(unsigned c, Label *on_equal)=0
virtual void AdvanceRegister(int reg, int by)=0
virtual void PopRegister(int register_index)=0
static const byte * StringCharacterPosition(String *subject, int start_index)
virtual bool CheckSpecialCharacterClass(uc16 type, Label *on_no_match)
virtual void WriteCurrentPositionToRegister(int reg, int cp_offset)=0
virtual void Bind(Label *label)=0
uint16_t uc16
Definition: globals.h:309
virtual void CheckCharacterLT(uc16 limit, Label *on_less)=0
static Result Match(Handle< Code > regexp, Handle< String > subject, int *offsets_vector, int offsets_vector_length, int previous_index, Isolate *isolate)
virtual void SetCurrentPositionFromEnd(int by)=0
static Address GrowStack(Address stack_pointer, Address *stack_top, Isolate *isolate)
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 CheckGreedyLoop(Label *on_tos_equals_current_position)=0
virtual void CheckNotCharacter(unsigned c, Label *on_not_equal)=0
virtual void CheckPosition(int cp_offset, Label *on_outside_input)
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