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