Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(9)

Side by Side Diff: src/x64/regexp-macro-assembler-x64.h

Issue 165443: X64: Implement RegExp natively. (Closed)
Patch Set: Addressed review comments. Created 11 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2009 the V8 project authors. All rights reserved. 1 // Copyright 2009 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution. 11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its 12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived 13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission. 14 // from this software without specific prior written permission.
15 // 15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 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. 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 27
28 #ifndef V8_X64_REGEXP_MACRO_ASSEMBLER_X64_H_
29 #define V8_X64_REGEXP_MACRO_ASSEMBLER_X64_H_
30
31 namespace v8 {
32 namespace internal {
33
34 class RegExpMacroAssemblerX64: public NativeRegExpMacroAssembler {
35 public:
36 RegExpMacroAssemblerX64(Mode mode, int registers_to_save);
37 virtual ~RegExpMacroAssemblerX64();
38 virtual int stack_limit_slack();
39 virtual void AdvanceCurrentPosition(int by);
40 virtual void AdvanceRegister(int reg, int by);
41 virtual void Backtrack();
42 virtual void Bind(Label* label);
43 virtual void CheckAtStart(Label* on_at_start);
44 virtual void CheckCharacter(uint32_t c, Label* on_equal);
45 virtual void CheckCharacterAfterAnd(uint32_t c,
46 uint32_t mask,
47 Label* on_equal);
48 virtual void CheckCharacterGT(uc16 limit, Label* on_greater);
49 virtual void CheckCharacterLT(uc16 limit, Label* on_less);
50 virtual void CheckCharacters(Vector<const uc16> str,
51 int cp_offset,
52 Label* on_failure,
53 bool check_end_of_string);
54 // A "greedy loop" is a loop that is both greedy and with a simple
55 // body. It has a particularly simple implementation.
56 virtual void CheckGreedyLoop(Label* on_tos_equals_current_position);
57 virtual void CheckNotAtStart(Label* on_not_at_start);
58 virtual void CheckNotBackReference(int start_reg, Label* on_no_match);
59 virtual void CheckNotBackReferenceIgnoreCase(int start_reg,
60 Label* on_no_match);
61 virtual void CheckNotRegistersEqual(int reg1, int reg2, Label* on_not_equal);
62 virtual void CheckNotCharacter(uint32_t c, Label* on_not_equal);
63 virtual void CheckNotCharacterAfterAnd(uint32_t c,
64 uint32_t mask,
65 Label* on_not_equal);
66 virtual void CheckNotCharacterAfterMinusAnd(uc16 c,
67 uc16 minus,
68 uc16 mask,
69 Label* on_not_equal);
70 // Checks whether the given offset from the current position is before
71 // the end of the string.
72 virtual void CheckPosition(int cp_offset, Label* on_outside_input);
73 virtual bool CheckSpecialCharacterClass(uc16 type,
74 int cp_offset,
75 bool check_offset,
76 Label* on_no_match);
77 virtual void Fail();
78 virtual Handle<Object> GetCode(Handle<String> source);
79 virtual void GoTo(Label* label);
80 virtual void IfRegisterGE(int reg, int comparand, Label* if_ge);
81 virtual void IfRegisterLT(int reg, int comparand, Label* if_lt);
82 virtual void IfRegisterEqPos(int reg, Label* if_eq);
83 virtual IrregexpImplementation Implementation();
84 virtual void LoadCurrentCharacter(int cp_offset,
85 Label* on_end_of_input,
86 bool check_bounds = true,
87 int characters = 1);
88 virtual void PopCurrentPosition();
89 virtual void PopRegister(int register_index);
90 virtual void PushBacktrack(Label* label);
91 virtual void PushCurrentPosition();
92 virtual void PushRegister(int register_index,
93 StackCheckFlag check_stack_limit);
94 virtual void ReadCurrentPositionFromRegister(int reg);
95 virtual void ReadStackPointerFromRegister(int reg);
96 virtual void SetRegister(int register_index, int to);
97 virtual void Succeed();
98 virtual void WriteCurrentPositionToRegister(int reg, int cp_offset);
99 virtual void ClearRegisters(int reg_from, int reg_to);
100 virtual void WriteStackPointerToRegister(int reg);
101
102 static Result Match(Handle<Code> regexp,
103 Handle<String> subject,
104 int* offsets_vector,
105 int offsets_vector_length,
106 int previous_index);
107
108 static Result Execute(Code* code,
109 String* input,
110 int start_offset,
111 const byte* input_start,
112 const byte* input_end,
113 int* output,
114 bool at_start);
115
116 private:
117 // Offsets from rbp of function parameters and stored registers.
118 static const int kFramePointer = 0;
119 // Above the frame pointer - function parameters and return address.
120 static const int kReturn_eip = kFramePointer + kPointerSize;
121 static const int kFrameAlign = kReturn_eip + kPointerSize;
122
123 #ifdef __MSVC__
124 // Parameters (first four passed as registers, but with room on stack).
125 // In Microsoft 64-bit Calling Convention, there is room on the callers
126 // stack (before the return address) to spill parameter registers. We
127 // use this space to store the register passed parameters.
128 static const int kInputString = kFrameAlign;
129 static const int kStartIndex = kInputString + kPointerSize;
130 static const int kInputStart = kStartIndex + kPointerSize;
131 static const int kInputEnd = kInputStart + kPointerSize;
132 static const int kRegisterOutput = kInputEnd + kPointerSize;
133 static const int kAtStart = kRegisterOutput + kPointerSize;
134 static const int kStackHighEnd = kAtStart + kPointerSize;
135 #else
136 // In AMD64 ABI Calling Convention, the first six integer parameters
137 // are passed as registers, and caller must allocate space on the stack
138 // if it wants them stored. We push the parameters after the frame pointer.
139 static const int kInputString = kFramePointer - kPointerSize;
140 static const int kStartIndex = kInputString - kPointerSize;
141 static const int kInputStart = kStartIndex - kPointerSize;
142 static const int kInputEnd = kInputStart - kPointerSize;
143 static const int kRegisterOutput = kInputEnd - kPointerSize;
144 static const int kAtStart = kRegisterOutput - kPointerSize;
145 static const int kStackHighEnd = kFrameAlign;
146 #endif
147
148 #ifdef __MSVC__
149 // Microsoft calling convention has three callee-saved registers
150 // (that we are using). We push these after the frame pointer.
151 static const int kBackup_rsi = kFramePointer - kPointerSize;
152 static const int kBackup_rdi = kBackup_rsi - kPointerSize;
153 static const int kBackup_rbx = kBackup_rdi - kPointerSize;
154 static const int kLastCalleeSaveRegister = kBackup_rbx;
155 #else
156 // AMD64 Calling Convention has only one callee-save register that
157 // we use. We push this after the frame pointer (and after the
158 // parameters).
159 static const int kBackup_rbx = kAtStart - kPointerSize;
160 static const int kLastCalleeSaveRegister = kBackup_rbx;
161 #endif
162
163 // When adding local variables remember to push space for them in
164 // the frame in GetCode.
165 static const int kInputStartMinusOne =
166 kLastCalleeSaveRegister - kPointerSize;
167
168 // First register address. Following registers are below it on the stack.
169 static const int kRegisterZero = kInputStartMinusOne - kPointerSize;
170
171 // Initial size of code buffer.
172 static const size_t kRegExpCodeSize = 1024;
173
174 // Load a number of characters at the given offset from the
175 // current position, into the current-character register.
176 void LoadCurrentCharacterUnchecked(int cp_offset, int character_count);
177
178 // Check whether preemption has been requested.
179 void CheckPreemption();
180
181 // Check whether we are exceeding the stack limit on the backtrack stack.
182 void CheckStackLimit();
183
184 // Called from RegExp if the stack-guard is triggered.
185 // If the code object is relocated, the return address is fixed before
186 // returning.
187 static int CheckStackGuardState(Address* return_address,
188 Code* re_code,
189 Address re_frame);
190
191 // Generate a call to CheckStackGuardState.
192 void CallCheckStackGuardState();
193
194 // Called from RegExp if the backtrack stack limit is hit.
195 // Tries to expand the stack. Returns the new stack-pointer if
196 // successful, and updates the stack_top address, or returns 0 if unable
197 // to grow the stack.
198 // This function must not trigger a garbage collection.
199 static Address GrowStack(Address stack_pointer, Address* stack_top);
200
201 // The rbp-relative location of a regexp register.
202 Operand register_location(int register_index);
203
204 // The register containing the current character after LoadCurrentCharacter.
205 inline Register current_character() { return rdx; }
206
207 // The register containing the backtrack stack top. Provides a meaningful
208 // name to the register.
209 inline Register backtrack_stackpointer() { return rcx; }
210
211 // The registers containing a self pointer to this code's Code object.
212 inline Register code_object_pointer() { return r8; }
213
214 // Byte size of chars in the string to match (decided by the Mode argument)
215 inline int char_size() { return static_cast<int>(mode_); }
216
217 // Equivalent to a conditional branch to the label, unless the label
218 // is NULL, in which case it is a conditional Backtrack.
219 void BranchOrBacktrack(Condition condition, Label* to);
220
221 void MarkPositionForCodeRelativeFixup() {
222 code_relative_fixup_positions_.Add(masm_->pc_offset());
223 }
224
225 void FixupCodeRelativePositions();
226
227 // Call and return internally in the generated code in a way that
228 // is GC-safe (i.e., doesn't leave absolute code addresses on the stack)
229 inline void SafeCall(Label* to);
230 inline void SafeCallTarget(Label* label);
231 inline void SafeReturn();
232
233 // Pushes the value of a register on the backtrack stack. Decrements the
234 // stack pointer (rcx) by a word size and stores the register's value there.
235 inline void Push(Register source);
236
237 // Pushes a value on the backtrack stack. Decrements the stack pointer (rcx)
238 // by a word size and stores the value there.
239 inline void Push(Immediate value);
240
241 // Pushes the Code object relative offset of a label on the backtrack stack
242 // (i.e., a backtrack target). Decrements the stack pointer (rcx)
243 // by a word size and stores the value there.
244 inline void Push(Label* label);
245
246 // Pops a value from the backtrack stack. Reads the word at the stack pointer
247 // (rcx) and increments it by a word size.
248 inline void Pop(Register target);
249
250 // Drops the top value from the backtrack stack without reading it.
251 // Increments the stack pointer (rcx) by a word size.
252 inline void Drop();
253
254 // Before calling a C-function from generated code, align arguments on stack.
255 // After aligning the frame, arguments must be stored in esp[0], esp[4],
256 // etc., not pushed. The argument count assumes all arguments are word sized.
257 // Some compilers/platforms require the stack to be aligned when calling
258 // C++ code.
259 // Needs a scratch register to do some arithmetic. This register will be
260 // trashed.
261 inline void FrameAlign(int num_arguments);
262
263 // Calls a C function and cleans up the space for arguments allocated
264 // by FrameAlign. The called function is not allowed to trigger a garbage
265 // collection, since that might move the code and invalidate the return
266 // address (unless this is somehow accounted for by the called function).
267 inline void CallCFunction(Address function_address, int num_arguments);
268
269 MacroAssembler* masm_;
270
271 ZoneList<int> code_relative_fixup_positions_;
272
273 // Which mode to generate code for (ASCII or UC16).
274 Mode mode_;
275
276 // One greater than maximal register index actually used.
277 int num_registers_;
278
279 // Number of registers to output at the end (the saved registers
280 // are always 0..num_saved_registers_-1)
281 int num_saved_registers_;
282
283 // Labels used internally.
284 Label entry_label_;
285 Label start_label_;
286 Label success_label_;
287 Label backtrack_label_;
288 Label exit_label_;
289 Label check_preempt_label_;
290 Label stack_overflow_label_;
291 };
292
293 }} // namespace v8::internal
294
295 #endif // V8_X64_REGEXP_MACRO_ASSEMBLER_X64_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698