V8 Project
regexp-macro-assembler-x64.h
Go to the documentation of this file.
1 // Copyright 2012 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_X64_REGEXP_MACRO_ASSEMBLER_X64_H_
6 #define V8_X64_REGEXP_MACRO_ASSEMBLER_X64_H_
7 
8 #include "src/macro-assembler.h"
10 #include "src/x64/assembler-x64.h"
12 
13 namespace v8 {
14 namespace internal {
15 
16 #ifndef V8_INTERPRETED_REGEXP
17 
19  public:
20  RegExpMacroAssemblerX64(Mode mode, int registers_to_save, Zone* zone);
22  virtual int stack_limit_slack();
23  virtual void AdvanceCurrentPosition(int by);
24  virtual void AdvanceRegister(int reg, int by);
25  virtual void Backtrack();
26  virtual void Bind(Label* label);
27  virtual void CheckAtStart(Label* on_at_start);
28  virtual void CheckCharacter(uint32_t c, Label* on_equal);
30  uint32_t mask,
31  Label* on_equal);
32  virtual void CheckCharacterGT(uc16 limit, Label* on_greater);
33  virtual void CheckCharacterLT(uc16 limit, Label* on_less);
34  // A "greedy loop" is a loop that is both greedy and with a simple
35  // body. It has a particularly simple implementation.
36  virtual void CheckGreedyLoop(Label* on_tos_equals_current_position);
37  virtual void CheckNotAtStart(Label* on_not_at_start);
38  virtual void CheckNotBackReference(int start_reg, Label* on_no_match);
39  virtual void CheckNotBackReferenceIgnoreCase(int start_reg,
40  Label* on_no_match);
41  virtual void CheckNotCharacter(uint32_t c, Label* on_not_equal);
43  uint32_t mask,
44  Label* on_not_equal);
46  uc16 minus,
47  uc16 mask,
48  Label* on_not_equal);
49  virtual void CheckCharacterInRange(uc16 from,
50  uc16 to,
51  Label* on_in_range);
52  virtual void CheckCharacterNotInRange(uc16 from,
53  uc16 to,
54  Label* on_not_in_range);
55  virtual void CheckBitInTable(Handle<ByteArray> table, Label* on_bit_set);
56 
57  // Checks whether the given offset from the current position is before
58  // the end of the string.
59  virtual void CheckPosition(int cp_offset, Label* on_outside_input);
60  virtual bool CheckSpecialCharacterClass(uc16 type,
61  Label* on_no_match);
62  virtual void Fail();
64  virtual void GoTo(Label* label);
65  virtual void IfRegisterGE(int reg, int comparand, Label* if_ge);
66  virtual void IfRegisterLT(int reg, int comparand, Label* if_lt);
67  virtual void IfRegisterEqPos(int reg, Label* if_eq);
69  virtual void LoadCurrentCharacter(int cp_offset,
70  Label* on_end_of_input,
71  bool check_bounds = true,
72  int characters = 1);
73  virtual void PopCurrentPosition();
74  virtual void PopRegister(int register_index);
75  virtual void PushBacktrack(Label* label);
76  virtual void PushCurrentPosition();
77  virtual void PushRegister(int register_index,
78  StackCheckFlag check_stack_limit);
79  virtual void ReadCurrentPositionFromRegister(int reg);
80  virtual void ReadStackPointerFromRegister(int reg);
81  virtual void SetCurrentPositionFromEnd(int by);
82  virtual void SetRegister(int register_index, int to);
83  virtual bool Succeed();
84  virtual void WriteCurrentPositionToRegister(int reg, int cp_offset);
85  virtual void ClearRegisters(int reg_from, int reg_to);
86  virtual void WriteStackPointerToRegister(int reg);
87 
88  static Result Match(Handle<Code> regexp,
89  Handle<String> subject,
90  int* offsets_vector,
91  int offsets_vector_length,
92  int previous_index,
93  Isolate* isolate);
94 
95  static Result Execute(Code* code,
96  String* input,
97  int start_offset,
98  const byte* input_start,
99  const byte* input_end,
100  int* output,
101  bool at_start);
102 
103  // Called from RegExp if the stack-guard is triggered.
104  // If the code object is relocated, the return address is fixed before
105  // returning.
106  static int CheckStackGuardState(Address* return_address,
107  Code* re_code,
108  Address re_frame);
109 
110  private:
111  // Offsets from rbp of function parameters and stored registers.
112  static const int kFramePointer = 0;
113  // Above the frame pointer - function parameters and return address.
115  static const int kFrameAlign = kReturn_eip + kRegisterSize;
116 
117 #ifdef _WIN64
118  // Parameters (first four passed as registers, but with room on stack).
119  // In Microsoft 64-bit Calling Convention, there is room on the callers
120  // stack (before the return address) to spill parameter registers. We
121  // use this space to store the register passed parameters.
122  static const int kInputString = kFrameAlign;
123  // StartIndex is passed as 32 bit int.
124  static const int kStartIndex = kInputString + kRegisterSize;
125  static const int kInputStart = kStartIndex + kRegisterSize;
126  static const int kInputEnd = kInputStart + kRegisterSize;
127  static const int kRegisterOutput = kInputEnd + kRegisterSize;
128  // For the case of global regular expression, we have room to store at least
129  // one set of capture results. For the case of non-global regexp, we ignore
130  // this value. NumOutputRegisters is passed as 32-bit value. The upper
131  // 32 bit of this 64-bit stack slot may contain garbage.
133  static const int kStackHighEnd = kNumOutputRegisters + kRegisterSize;
134  // DirectCall is passed as 32 bit int (values 0 or 1).
135  static const int kDirectCall = kStackHighEnd + kRegisterSize;
136  static const int kIsolate = kDirectCall + kRegisterSize;
137 #else
138  // In AMD64 ABI Calling Convention, the first six integer parameters
139  // are passed as registers, and caller must allocate space on the stack
140  // if it wants them stored. We push the parameters after the frame pointer.
142  static const int kStartIndex = kInputString - kRegisterSize;
143  static const int kInputStart = kStartIndex - kRegisterSize;
144  static const int kInputEnd = kInputStart - kRegisterSize;
146 
147  // For the case of global regular expression, we have room to store at least
148  // one set of capture results. For the case of non-global regexp, we ignore
149  // this value.
151  static const int kStackHighEnd = kFrameAlign;
153  static const int kIsolate = kDirectCall + kRegisterSize;
154 #endif
155 
156 #ifdef _WIN64
157  // Microsoft calling convention has three callee-saved registers
158  // (that we are using). We push these after the frame pointer.
159  static const int kBackup_rsi = kFramePointer - kRegisterSize;
160  static const int kBackup_rdi = kBackup_rsi - kRegisterSize;
161  static const int kBackup_rbx = kBackup_rdi - kRegisterSize;
162  static const int kLastCalleeSaveRegister = kBackup_rbx;
163 #else
164  // AMD64 Calling Convention has only one callee-save register that
165  // we use. We push this after the frame pointer (and after the
166  // parameters).
169 #endif
170 
172  // When adding local variables remember to push space for them in
173  // the frame in GetCode.
175 
176  // First register address. Following registers are below it on the stack.
178 
179  // Initial size of code buffer.
180  static const size_t kRegExpCodeSize = 1024;
181 
182  // Load a number of characters at the given offset from the
183  // current position, into the current-character register.
184  void LoadCurrentCharacterUnchecked(int cp_offset, int character_count);
185 
186  // Check whether preemption has been requested.
188 
189  // Check whether we are exceeding the stack limit on the backtrack stack.
191 
192  // Generate a call to CheckStackGuardState.
194 
195  // The rbp-relative location of a regexp register.
196  Operand register_location(int register_index);
197 
198  // The register containing the current character after LoadCurrentCharacter.
199  inline Register current_character() { return rdx; }
200 
201  // The register containing the backtrack stack top. Provides a meaningful
202  // name to the register.
203  inline Register backtrack_stackpointer() { return rcx; }
204 
205  // The registers containing a self pointer to this code's Code object.
206  inline Register code_object_pointer() { return r8; }
207 
208  // Byte size of chars in the string to match (decided by the Mode argument)
209  inline int char_size() { return static_cast<int>(mode_); }
210 
211  // Equivalent to a conditional branch to the label, unless the label
212  // is NULL, in which case it is a conditional Backtrack.
213  void BranchOrBacktrack(Condition condition, Label* to);
214 
217  }
218 
220 
221  // Call and return internally in the generated code in a way that
222  // is GC-safe (i.e., doesn't leave absolute code addresses on the stack)
223  inline void SafeCall(Label* to);
224  inline void SafeCallTarget(Label* label);
225  inline void SafeReturn();
226 
227  // Pushes the value of a register on the backtrack stack. Decrements the
228  // stack pointer (rcx) by a word size and stores the register's value there.
229  inline void Push(Register source);
230 
231  // Pushes a value on the backtrack stack. Decrements the stack pointer (rcx)
232  // by a word size and stores the value there.
233  inline void Push(Immediate value);
234 
235  // Pushes the Code object relative offset of a label on the backtrack stack
236  // (i.e., a backtrack target). Decrements the stack pointer (rcx)
237  // by a word size and stores the value there.
238  inline void Push(Label* label);
239 
240  // Pops a value from the backtrack stack. Reads the word at the stack pointer
241  // (rcx) and increments it by a word size.
242  inline void Pop(Register target);
243 
244  // Drops the top value from the backtrack stack without reading it.
245  // Increments the stack pointer (rcx) by a word size.
246  inline void Drop();
247 
248  inline void ReadPositionFromRegister(Register dst, int reg);
249 
250  Isolate* isolate() const { return masm_.isolate(); }
251 
253  MacroAssembler::NoRootArrayScope no_root_array_scope_;
254 
256 
257  // Which mode to generate code for (LATIN1 or UC16).
259 
260  // One greater than maximal register index actually used.
262 
263  // Number of registers to output at the end (the saved registers
264  // are always 0..num_saved_registers_-1)
266 
267  // Labels used internally.
272  Label exit_label_;
275 };
276 
277 #endif // V8_INTERPRETED_REGEXP
278 
279 }} // namespace v8::internal
280 
281 #endif // V8_X64_REGEXP_MACRO_ASSEMBLER_X64_H_
Isolate * isolate() const
Definition: assembler.h:62
void Add(const T &element, AllocationPolicy allocator=AllocationPolicy())
Definition: list-inl.h:17
virtual void Bind(Label *label)
Operand register_location(int register_index)
virtual void LoadCurrentCharacter(int cp_offset, Label *on_end_of_input, bool check_bounds=true, int characters=1)
virtual void IfRegisterEqPos(int reg, Label *if_eq)
virtual IrregexpImplementation Implementation()
RegExpMacroAssemblerX64(Mode mode, int registers_to_save, Zone *zone)
virtual void AdvanceCurrentPosition(int by)
virtual Handle< HeapObject > GetCode(Handle< String > source)
virtual void CheckCharacter(uint32_t c, Label *on_equal)
virtual void CheckNotCharacter(uint32_t c, Label *on_not_equal)
virtual void CheckNotCharacterAfterMinusAnd(uc16 c, uc16 minus, uc16 mask, Label *on_not_equal)
virtual void ClearRegisters(int reg_from, int reg_to)
virtual void GoTo(Label *label)
virtual void IfRegisterGE(int reg, int comparand, Label *if_ge)
virtual void SetRegister(int register_index, int to)
virtual void WriteCurrentPositionToRegister(int reg, int cp_offset)
static Result Match(Handle< Code > regexp, Handle< String > subject, int *offsets_vector, int offsets_vector_length, int previous_index, Isolate *isolate)
virtual void CheckCharacterLT(uc16 limit, Label *on_less)
virtual void ReadStackPointerFromRegister(int reg)
virtual void PushBacktrack(Label *label)
virtual void CheckNotBackReferenceIgnoreCase(int start_reg, Label *on_no_match)
virtual void CheckCharacterGT(uc16 limit, Label *on_greater)
virtual void AdvanceRegister(int reg, int by)
void LoadCurrentCharacterUnchecked(int cp_offset, int character_count)
virtual void CheckNotBackReference(int start_reg, Label *on_no_match)
virtual void CheckCharacterInRange(uc16 from, uc16 to, Label *on_in_range)
virtual void CheckNotCharacterAfterAnd(uint32_t c, uint32_t mask, Label *on_not_equal)
virtual void CheckPosition(int cp_offset, Label *on_outside_input)
virtual void IfRegisterLT(int reg, int comparand, Label *if_lt)
virtual void ReadCurrentPositionFromRegister(int reg)
virtual void PopRegister(int register_index)
static Result Execute(Code *code, String *input, int start_offset, const byte *input_start, const byte *input_end, int *output, bool at_start)
MacroAssembler::NoRootArrayScope no_root_array_scope_
virtual void CheckGreedyLoop(Label *on_tos_equals_current_position)
virtual void CheckAtStart(Label *on_at_start)
virtual void SetCurrentPositionFromEnd(int by)
virtual void CheckNotAtStart(Label *on_not_at_start)
virtual void CheckCharacterAfterAnd(uint32_t c, uint32_t mask, Label *on_equal)
void BranchOrBacktrack(Condition condition, Label *to)
virtual void CheckCharacterNotInRange(uc16 from, uc16 to, Label *on_not_in_range)
static int CheckStackGuardState(Address *return_address, Code *re_code, Address re_frame)
virtual bool CheckSpecialCharacterClass(uc16 type, Label *on_no_match)
virtual void PushRegister(int register_index, StackCheckFlag check_stack_limit)
virtual void CheckBitInTable(Handle< ByteArray > table, Label *on_bit_set)
void ReadPositionFromRegister(Register dst, int reg)
virtual void WriteStackPointerToRegister(int reg)
enable harmony numeric enable harmony object literal extensions Optimize object Array DOM strings and string trace pretenuring decisions of HAllocate instructions Enables optimizations which favor memory size over execution speed maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining trace the tracking of allocation sites deoptimize every n garbage collections perform array bounds checks elimination analyze liveness of environment slots and zap dead values flushes the cache of optimized code for closures on every GC allow uint32 values on optimize frames if they are used only in safe operations track concurrent recompilation artificial compilation delay in ms do not emit check maps for constant values that have a leaf deoptimize the optimized code if the layout of the maps changes enable context specialization in TurboFan execution budget before interrupt is triggered max percentage of megamorphic generic ICs to allow optimization enable use of SAHF instruction if enable use of VFP3 instructions if available enable use of NEON instructions if enable use of SDIV and UDIV instructions if enable use of MLS 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 enable alignment of csp to bytes on platforms which prefer the register to always be expose gc extension under the specified name show built in functions in stack traces use random jit cookie to mask large constants minimum length for automatic enable preparsing CPU profiler sampling interval in microseconds trace out of bounds accesses to external arrays default size of stack region v8 is allowed to maximum length of function source code printed in a stack trace min size of a semi the new space consists of two semi spaces print one trace line following each garbage collection do not print trace line after scavenger collection print cumulative GC statistics in only print modified registers Trace simulator debug messages Implied by trace sim abort randomize hashes to avoid predictable hash Fixed seed to use to hash property Print the time it takes to deserialize the snapshot A filename with extra code to be included in the A file to write the raw snapshot bytes to(mksnapshot only)") DEFINE_STRING(raw_context_file
enable harmony numeric enable harmony object literal extensions Optimize object Array DOM strings and string trace pretenuring decisions of HAllocate instructions Enables optimizations which favor memory size over execution speed maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining trace the tracking of allocation sites deoptimize every n garbage collections perform array bounds checks elimination analyze liveness of environment slots and zap dead values flushes the cache of optimized code for closures on every GC allow uint32 values on optimize frames if they are used only in safe operations track concurrent recompilation artificial compilation delay in ms do not emit check maps for constant values that have a leaf deoptimize the optimized code if the layout of the maps changes enable context specialization in TurboFan execution budget before interrupt is triggered max percentage of megamorphic generic ICs to allow optimization enable use of SAHF instruction if enable use of VFP3 instructions if available enable use of NEON instructions if enable use of SDIV and UDIV instructions if enable use of MLS 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_BOOL(enable_always_align_csp
const int kPointerSize
Definition: globals.h:129
const int kRegisterSize
Definition: globals.h:133
byte * Address
Definition: globals.h:101
const Register r8
const Register rdx
uint16_t uc16
Definition: globals.h:184
const Register rcx
Debugger support for the V8 JavaScript engine.
Definition: accessors.cc:20