• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_REGEXP_X64_REGEXP_MACRO_ASSEMBLER_X64_H_
6 #define V8_REGEXP_X64_REGEXP_MACRO_ASSEMBLER_X64_H_
7 
8 #include "src/macro-assembler.h"
9 #include "src/regexp/regexp-macro-assembler.h"
10 #include "src/x64/assembler-x64.h"
11 
12 namespace v8 {
13 namespace internal {
14 
15 #ifndef V8_INTERPRETED_REGEXP
16 
17 class RegExpMacroAssemblerX64: public NativeRegExpMacroAssembler {
18  public:
19   RegExpMacroAssemblerX64(Isolate* isolate, Zone* zone, Mode mode,
20                           int registers_to_save);
21   virtual ~RegExpMacroAssemblerX64();
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);
29   virtual void CheckCharacterAfterAnd(uint32_t c,
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(int cp_offset, Label* on_not_at_start);
38   virtual void CheckNotBackReference(int start_reg, bool read_backward,
39                                      Label* on_no_match);
40   virtual void CheckNotBackReferenceIgnoreCase(int start_reg,
41                                                bool read_backward, bool unicode,
42                                                Label* on_no_match);
43   virtual void CheckNotCharacter(uint32_t c, Label* on_not_equal);
44   virtual void CheckNotCharacterAfterAnd(uint32_t c,
45                                          uint32_t mask,
46                                          Label* on_not_equal);
47   virtual void CheckNotCharacterAfterMinusAnd(uc16 c,
48                                               uc16 minus,
49                                               uc16 mask,
50                                               Label* on_not_equal);
51   virtual void CheckCharacterInRange(uc16 from,
52                                      uc16 to,
53                                      Label* on_in_range);
54   virtual void CheckCharacterNotInRange(uc16 from,
55                                         uc16 to,
56                                         Label* on_not_in_range);
57   virtual void CheckBitInTable(Handle<ByteArray> table, Label* on_bit_set);
58 
59   // Checks whether the given offset from the current position is before
60   // the end of the string.
61   virtual void CheckPosition(int cp_offset, Label* on_outside_input);
62   virtual bool CheckSpecialCharacterClass(uc16 type,
63                                           Label* on_no_match);
64   virtual void Fail();
65   virtual Handle<HeapObject> GetCode(Handle<String> source);
66   virtual void GoTo(Label* label);
67   virtual void IfRegisterGE(int reg, int comparand, Label* if_ge);
68   virtual void IfRegisterLT(int reg, int comparand, Label* if_lt);
69   virtual void IfRegisterEqPos(int reg, Label* if_eq);
70   virtual IrregexpImplementation Implementation();
71   virtual void LoadCurrentCharacter(int cp_offset,
72                                     Label* on_end_of_input,
73                                     bool check_bounds = true,
74                                     int characters = 1);
75   virtual void PopCurrentPosition();
76   virtual void PopRegister(int register_index);
77   virtual void PushBacktrack(Label* label);
78   virtual void PushCurrentPosition();
79   virtual void PushRegister(int register_index,
80                             StackCheckFlag check_stack_limit);
81   virtual void ReadCurrentPositionFromRegister(int reg);
82   virtual void ReadStackPointerFromRegister(int reg);
83   virtual void SetCurrentPositionFromEnd(int by);
84   virtual void SetRegister(int register_index, int to);
85   virtual bool Succeed();
86   virtual void WriteCurrentPositionToRegister(int reg, int cp_offset);
87   virtual void ClearRegisters(int reg_from, int reg_to);
88   virtual void WriteStackPointerToRegister(int reg);
89 
90   static Result Match(Handle<Code> regexp,
91                       Handle<String> subject,
92                       int* offsets_vector,
93                       int offsets_vector_length,
94                       int previous_index,
95                       Isolate* isolate);
96 
97   static Result Execute(Code* code,
98                         String* input,
99                         int start_offset,
100                         const byte* input_start,
101                         const byte* input_end,
102                         int* output,
103                         bool at_start);
104 
105   // Called from RegExp if the stack-guard is triggered.
106   // If the code object is relocated, the return address is fixed before
107   // returning.
108   static int CheckStackGuardState(Address* return_address,
109                                   Code* re_code,
110                                   Address re_frame);
111 
112  private:
113   // Offsets from rbp of function parameters and stored registers.
114   static const int kFramePointer = 0;
115   // Above the frame pointer - function parameters and return address.
116   static const int kReturn_eip = kFramePointer + kRegisterSize;
117   static const int kFrameAlign = kReturn_eip + kRegisterSize;
118 
119 #ifdef _WIN64
120   // Parameters (first four passed as registers, but with room on stack).
121   // In Microsoft 64-bit Calling Convention, there is room on the callers
122   // stack (before the return address) to spill parameter registers. We
123   // use this space to store the register passed parameters.
124   static const int kInputString = kFrameAlign;
125   // StartIndex is passed as 32 bit int.
126   static const int kStartIndex = kInputString + kRegisterSize;
127   static const int kInputStart = kStartIndex + kRegisterSize;
128   static const int kInputEnd = kInputStart + kRegisterSize;
129   static const int kRegisterOutput = kInputEnd + kRegisterSize;
130   // For the case of global regular expression, we have room to store at least
131   // one set of capture results.  For the case of non-global regexp, we ignore
132   // this value. NumOutputRegisters is passed as 32-bit value.  The upper
133   // 32 bit of this 64-bit stack slot may contain garbage.
134   static const int kNumOutputRegisters = kRegisterOutput + kRegisterSize;
135   static const int kStackHighEnd = kNumOutputRegisters + kRegisterSize;
136   // DirectCall is passed as 32 bit int (values 0 or 1).
137   static const int kDirectCall = kStackHighEnd + kRegisterSize;
138   static const int kIsolate = kDirectCall + kRegisterSize;
139 #else
140   // In AMD64 ABI Calling Convention, the first six integer parameters
141   // are passed as registers, and caller must allocate space on the stack
142   // if it wants them stored. We push the parameters after the frame pointer.
143   static const int kInputString = kFramePointer - kRegisterSize;
144   static const int kStartIndex = kInputString - kRegisterSize;
145   static const int kInputStart = kStartIndex - kRegisterSize;
146   static const int kInputEnd = kInputStart - kRegisterSize;
147   static const int kRegisterOutput = kInputEnd - kRegisterSize;
148 
149   // For the case of global regular expression, we have room to store at least
150   // one set of capture results.  For the case of non-global regexp, we ignore
151   // this value.
152   static const int kNumOutputRegisters = kRegisterOutput - kRegisterSize;
153   static const int kStackHighEnd = kFrameAlign;
154   static const int kDirectCall = kStackHighEnd + kRegisterSize;
155   static const int kIsolate = kDirectCall + kRegisterSize;
156 #endif
157 
158 #ifdef _WIN64
159   // Microsoft calling convention has three callee-saved registers
160   // (that we are using). We push these after the frame pointer.
161   static const int kBackup_rsi = kFramePointer - kRegisterSize;
162   static const int kBackup_rdi = kBackup_rsi - kRegisterSize;
163   static const int kBackup_rbx = kBackup_rdi - kRegisterSize;
164   static const int kLastCalleeSaveRegister = kBackup_rbx;
165 #else
166   // AMD64 Calling Convention has only one callee-save register that
167   // we use. We push this after the frame pointer (and after the
168   // parameters).
169   static const int kBackup_rbx = kNumOutputRegisters - kRegisterSize;
170   static const int kLastCalleeSaveRegister = kBackup_rbx;
171 #endif
172 
173   static const int kSuccessfulCaptures = kLastCalleeSaveRegister - kPointerSize;
174   // When adding local variables remember to push space for them in
175   // the frame in GetCode.
176   static const int kStringStartMinusOne = kSuccessfulCaptures - kPointerSize;
177 
178   // First register address. Following registers are below it on the stack.
179   static const int kRegisterZero = kStringStartMinusOne - kPointerSize;
180 
181   // Initial size of code buffer.
182   static const size_t kRegExpCodeSize = 1024;
183 
184   // Load a number of characters at the given offset from the
185   // current position, into the current-character register.
186   void LoadCurrentCharacterUnchecked(int cp_offset, int character_count);
187 
188   // Check whether preemption has been requested.
189   void CheckPreemption();
190 
191   // Check whether we are exceeding the stack limit on the backtrack stack.
192   void CheckStackLimit();
193 
194   // Generate a call to CheckStackGuardState.
195   void CallCheckStackGuardState();
196 
197   // The rbp-relative location of a regexp register.
198   Operand register_location(int register_index);
199 
200   // The register containing the current character after LoadCurrentCharacter.
current_character()201   inline Register current_character() { return rdx; }
202 
203   // The register containing the backtrack stack top. Provides a meaningful
204   // name to the register.
backtrack_stackpointer()205   inline Register backtrack_stackpointer() { return rcx; }
206 
207   // The registers containing a self pointer to this code's Code object.
code_object_pointer()208   inline Register code_object_pointer() { return r8; }
209 
210   // Byte size of chars in the string to match (decided by the Mode argument)
char_size()211   inline int char_size() { return static_cast<int>(mode_); }
212 
213   // Equivalent to a conditional branch to the label, unless the label
214   // is NULL, in which case it is a conditional Backtrack.
215   void BranchOrBacktrack(Condition condition, Label* to);
216 
MarkPositionForCodeRelativeFixup()217   void MarkPositionForCodeRelativeFixup() {
218     code_relative_fixup_positions_.Add(masm_.pc_offset(), zone());
219   }
220 
221   void FixupCodeRelativePositions();
222 
223   // Call and return internally in the generated code in a way that
224   // is GC-safe (i.e., doesn't leave absolute code addresses on the stack)
225   inline void SafeCall(Label* to);
226   inline void SafeCallTarget(Label* label);
227   inline void SafeReturn();
228 
229   // Pushes the value of a register on the backtrack stack. Decrements the
230   // stack pointer (rcx) by a word size and stores the register's value there.
231   inline void Push(Register source);
232 
233   // Pushes a value on the backtrack stack. Decrements the stack pointer (rcx)
234   // by a word size and stores the value there.
235   inline void Push(Immediate value);
236 
237   // Pushes the Code object relative offset of a label on the backtrack stack
238   // (i.e., a backtrack target). Decrements the stack pointer (rcx)
239   // by a word size and stores the value there.
240   inline void Push(Label* label);
241 
242   // Pops a value from the backtrack stack. Reads the word at the stack pointer
243   // (rcx) and increments it by a word size.
244   inline void Pop(Register target);
245 
246   // Drops the top value from the backtrack stack without reading it.
247   // Increments the stack pointer (rcx) by a word size.
248   inline void Drop();
249 
250   inline void ReadPositionFromRegister(Register dst, int reg);
251 
isolate()252   Isolate* isolate() const { return masm_.isolate(); }
253 
254   MacroAssembler masm_;
255   MacroAssembler::NoRootArrayScope no_root_array_scope_;
256 
257   ZoneList<int> code_relative_fixup_positions_;
258 
259   // Which mode to generate code for (LATIN1 or UC16).
260   Mode mode_;
261 
262   // One greater than maximal register index actually used.
263   int num_registers_;
264 
265   // Number of registers to output at the end (the saved registers
266   // are always 0..num_saved_registers_-1)
267   int num_saved_registers_;
268 
269   // Labels used internally.
270   Label entry_label_;
271   Label start_label_;
272   Label success_label_;
273   Label backtrack_label_;
274   Label exit_label_;
275   Label check_preempt_label_;
276   Label stack_overflow_label_;
277 };
278 
279 #endif  // V8_INTERPRETED_REGEXP
280 
281 }  // namespace internal
282 }  // namespace v8
283 
284 #endif  // V8_REGEXP_X64_REGEXP_MACRO_ASSEMBLER_X64_H_
285