• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2009 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 #include "v8.h"
29 #include "serialize.h"
30 #include "unicode.h"
31 #include "log.h"
32 #include "ast.h"
33 #include "regexp-stack.h"
34 #include "macro-assembler.h"
35 #include "regexp-macro-assembler.h"
36 #include "x64/macro-assembler-x64.h"
37 #include "x64/regexp-macro-assembler-x64.h"
38 
39 namespace v8 {
40 namespace internal {
41 
42 #ifdef V8_NATIVE_REGEXP
43 
44 /*
45  * This assembler uses the following register assignment convention
46  * - rdx : currently loaded character(s) as ASCII or UC16. Must be loaded using
47  *         LoadCurrentCharacter before using any of the dispatch methods.
48  * - rdi : current position in input, as negative offset from end of string.
49  *         Please notice that this is the byte offset, not the character
50  *         offset! Is always a 32-bit signed (negative) offset, but must be
51  *         maintained sign-extended to 64 bits, since it is used as index.
52  * - rsi : end of input (points to byte after last character in input),
53  *         so that rsi+rdi points to the current character.
54  * - rbp : frame pointer. Used to access arguments, local variables and
55  *         RegExp registers.
56  * - rsp : points to tip of C stack.
57  * - rcx : points to tip of backtrack stack. The backtrack stack contains
58  *         only 32-bit values. Most are offsets from some base (e.g., character
59  *         positions from end of string or code location from Code* pointer).
60  * - r8  : code object pointer. Used to convert between absolute and
61  *         code-object-relative addresses.
62  *
63  * The registers rax, rbx, rcx, r9 and r11 are free to use for computations.
64  * If changed to use r12+, they should be saved as callee-save registers.
65  *
66  * Each call to a C++ method should retain these registers.
67  *
68  * The stack will have the following content, in some order, indexable from the
69  * frame pointer (see, e.g., kStackHighEnd):
70  *    - stack_area_base       (High end of the memory area to use as
71  *                             backtracking stack)
72  *    - at_start              (if 1, start at start of string, if 0, don't)
73  *    - int* capture_array    (int[num_saved_registers_], for output).
74  *    - end of input          (Address of end of string)
75  *    - start of input        (Address of first character in string)
76  *    - String** input_string (location of a handle containing the string)
77  *    - return address
78  *    - backup of callee save registers (rbx, possibly rsi and rdi).
79  *    - Offset of location before start of input (effectively character
80  *      position -1). Used to initialize capture registers to a non-position.
81  *    - register 0  rbp[-n]   (Only positions must be stored in the first
82  *    - register 1  rbp[-n-8]  num_saved_registers_ registers)
83  *    - ...
84  *
85  * The first num_saved_registers_ registers are initialized to point to
86  * "character -1" in the string (i.e., char_size() bytes before the first
87  * character of the string). The remaining registers starts out uninitialized.
88  *
89  * The first seven values must be provided by the calling code by
90  * calling the code's entry address cast to a function pointer with the
91  * following signature:
92  * int (*match)(String* input_string,
93  *              Address start,
94  *              Address end,
95  *              int* capture_output_array,
96  *              bool at_start,
97  *              byte* stack_area_base)
98  */
99 
100 #define __ ACCESS_MASM(masm_)
101 
RegExpMacroAssemblerX64(Mode mode,int registers_to_save)102 RegExpMacroAssemblerX64::RegExpMacroAssemblerX64(
103     Mode mode,
104     int registers_to_save)
105     : masm_(new MacroAssembler(NULL, kRegExpCodeSize)),
106       code_relative_fixup_positions_(4),
107       mode_(mode),
108       num_registers_(registers_to_save),
109       num_saved_registers_(registers_to_save),
110       entry_label_(),
111       start_label_(),
112       success_label_(),
113       backtrack_label_(),
114       exit_label_() {
115   ASSERT_EQ(0, registers_to_save % 2);
116   __ jmp(&entry_label_);   // We'll write the entry code when we know more.
117   __ bind(&start_label_);  // And then continue from here.
118 }
119 
120 
~RegExpMacroAssemblerX64()121 RegExpMacroAssemblerX64::~RegExpMacroAssemblerX64() {
122   delete masm_;
123   // Unuse labels in case we throw away the assembler without calling GetCode.
124   entry_label_.Unuse();
125   start_label_.Unuse();
126   success_label_.Unuse();
127   backtrack_label_.Unuse();
128   exit_label_.Unuse();
129   check_preempt_label_.Unuse();
130   stack_overflow_label_.Unuse();
131 }
132 
133 
stack_limit_slack()134 int RegExpMacroAssemblerX64::stack_limit_slack()  {
135   return RegExpStack::kStackLimitSlack;
136 }
137 
138 
AdvanceCurrentPosition(int by)139 void RegExpMacroAssemblerX64::AdvanceCurrentPosition(int by) {
140   if (by != 0) {
141     Label inside_string;
142     __ addq(rdi, Immediate(by * char_size()));
143   }
144 }
145 
146 
AdvanceRegister(int reg,int by)147 void RegExpMacroAssemblerX64::AdvanceRegister(int reg, int by) {
148   ASSERT(reg >= 0);
149   ASSERT(reg < num_registers_);
150   if (by != 0) {
151     __ addq(register_location(reg), Immediate(by));
152   }
153 }
154 
155 
Backtrack()156 void RegExpMacroAssemblerX64::Backtrack() {
157   CheckPreemption();
158   // Pop Code* offset from backtrack stack, add Code* and jump to location.
159   Pop(rbx);
160   __ addq(rbx, code_object_pointer());
161   __ jmp(rbx);
162 }
163 
164 
Bind(Label * label)165 void RegExpMacroAssemblerX64::Bind(Label* label) {
166   __ bind(label);
167 }
168 
169 
CheckCharacter(uint32_t c,Label * on_equal)170 void RegExpMacroAssemblerX64::CheckCharacter(uint32_t c, Label* on_equal) {
171   __ cmpl(current_character(), Immediate(c));
172   BranchOrBacktrack(equal, on_equal);
173 }
174 
175 
CheckCharacterGT(uc16 limit,Label * on_greater)176 void RegExpMacroAssemblerX64::CheckCharacterGT(uc16 limit, Label* on_greater) {
177   __ cmpl(current_character(), Immediate(limit));
178   BranchOrBacktrack(greater, on_greater);
179 }
180 
181 
CheckAtStart(Label * on_at_start)182 void RegExpMacroAssemblerX64::CheckAtStart(Label* on_at_start) {
183   Label not_at_start;
184   // Did we start the match at the start of the string at all?
185   __ cmpb(Operand(rbp, kAtStart), Immediate(0));
186   BranchOrBacktrack(equal, &not_at_start);
187   // If we did, are we still at the start of the input?
188   __ lea(rax, Operand(rsi, rdi, times_1, 0));
189   __ cmpq(rax, Operand(rbp, kInputStart));
190   BranchOrBacktrack(equal, on_at_start);
191   __ bind(&not_at_start);
192 }
193 
194 
CheckNotAtStart(Label * on_not_at_start)195 void RegExpMacroAssemblerX64::CheckNotAtStart(Label* on_not_at_start) {
196   // Did we start the match at the start of the string at all?
197   __ cmpb(Operand(rbp, kAtStart), Immediate(0));
198   BranchOrBacktrack(equal, on_not_at_start);
199   // If we did, are we still at the start of the input?
200   __ lea(rax, Operand(rsi, rdi, times_1, 0));
201   __ cmpq(rax, Operand(rbp, kInputStart));
202   BranchOrBacktrack(not_equal, on_not_at_start);
203 }
204 
205 
CheckCharacterLT(uc16 limit,Label * on_less)206 void RegExpMacroAssemblerX64::CheckCharacterLT(uc16 limit, Label* on_less) {
207   __ cmpl(current_character(), Immediate(limit));
208   BranchOrBacktrack(less, on_less);
209 }
210 
211 
CheckCharacters(Vector<const uc16> str,int cp_offset,Label * on_failure,bool check_end_of_string)212 void RegExpMacroAssemblerX64::CheckCharacters(Vector<const uc16> str,
213                                               int cp_offset,
214                                               Label* on_failure,
215                                               bool check_end_of_string) {
216   int byte_length = str.length() * char_size();
217   int byte_offset = cp_offset * char_size();
218   if (check_end_of_string) {
219     // Check that there are at least str.length() characters left in the input.
220     __ cmpl(rdi, Immediate(-(byte_offset + byte_length)));
221     BranchOrBacktrack(greater, on_failure);
222   }
223 
224   if (on_failure == NULL) {
225     // Instead of inlining a backtrack, (re)use the global backtrack target.
226     on_failure = &backtrack_label_;
227   }
228 
229   // TODO(lrn): Test multiple characters at a time by loading 4 or 8 bytes
230   // at a time.
231   for (int i = 0; i < str.length(); i++) {
232     if (mode_ == ASCII) {
233       __ cmpb(Operand(rsi, rdi, times_1, byte_offset + i),
234               Immediate(static_cast<int8_t>(str[i])));
235     } else {
236       ASSERT(mode_ == UC16);
237       __ cmpw(Operand(rsi, rdi, times_1, byte_offset + i * sizeof(uc16)),
238               Immediate(str[i]));
239     }
240     BranchOrBacktrack(not_equal, on_failure);
241   }
242 }
243 
244 
CheckGreedyLoop(Label * on_equal)245 void RegExpMacroAssemblerX64::CheckGreedyLoop(Label* on_equal) {
246   Label fallthrough;
247   __ cmpl(rdi, Operand(backtrack_stackpointer(), 0));
248   __ j(not_equal, &fallthrough);
249   Drop();
250   BranchOrBacktrack(no_condition, on_equal);
251   __ bind(&fallthrough);
252 }
253 
254 
CheckNotBackReferenceIgnoreCase(int start_reg,Label * on_no_match)255 void RegExpMacroAssemblerX64::CheckNotBackReferenceIgnoreCase(
256     int start_reg,
257     Label* on_no_match) {
258   Label fallthrough;
259   __ movq(rdx, register_location(start_reg));  // Offset of start of capture
260   __ movq(rbx, register_location(start_reg + 1));  // Offset of end of capture
261   __ subq(rbx, rdx);  // Length of capture.
262 
263   // -----------------------
264   // rdx  = Start offset of capture.
265   // rbx = Length of capture
266 
267   // If length is negative, this code will fail (it's a symptom of a partial or
268   // illegal capture where start of capture after end of capture).
269   // This must not happen (no back-reference can reference a capture that wasn't
270   // closed before in the reg-exp, and we must not generate code that can cause
271   // this condition).
272 
273   // If length is zero, either the capture is empty or it is nonparticipating.
274   // In either case succeed immediately.
275   __ j(equal, &fallthrough);
276 
277   if (mode_ == ASCII) {
278     Label loop_increment;
279     if (on_no_match == NULL) {
280       on_no_match = &backtrack_label_;
281     }
282 
283     __ lea(r9, Operand(rsi, rdx, times_1, 0));
284     __ lea(r11, Operand(rsi, rdi, times_1, 0));
285     __ addq(rbx, r9);  // End of capture
286     // ---------------------
287     // r11 - current input character address
288     // r9 - current capture character address
289     // rbx - end of capture
290 
291     Label loop;
292     __ bind(&loop);
293     __ movzxbl(rdx, Operand(r9, 0));
294     __ movzxbl(rax, Operand(r11, 0));
295     // al - input character
296     // dl - capture character
297     __ cmpb(rax, rdx);
298     __ j(equal, &loop_increment);
299 
300     // Mismatch, try case-insensitive match (converting letters to lower-case).
301     // I.e., if or-ing with 0x20 makes values equal and in range 'a'-'z', it's
302     // a match.
303     __ or_(rax, Immediate(0x20));  // Convert match character to lower-case.
304     __ or_(rdx, Immediate(0x20));  // Convert capture character to lower-case.
305     __ cmpb(rax, rdx);
306     __ j(not_equal, on_no_match);  // Definitely not equal.
307     __ subb(rax, Immediate('a'));
308     __ cmpb(rax, Immediate('z' - 'a'));
309     __ j(above, on_no_match);  // Weren't letters anyway.
310 
311     __ bind(&loop_increment);
312     // Increment pointers into match and capture strings.
313     __ addq(r11, Immediate(1));
314     __ addq(r9, Immediate(1));
315     // Compare to end of capture, and loop if not done.
316     __ cmpq(r9, rbx);
317     __ j(below, &loop);
318 
319     // Compute new value of character position after the matched part.
320     __ movq(rdi, r11);
321     __ subq(rdi, rsi);
322   } else {
323     ASSERT(mode_ == UC16);
324     // Save important/volatile registers before calling C function.
325 #ifndef _WIN64
326     // Callee save on Win64
327     __ push(rsi);
328     __ push(rdi);
329 #endif
330     __ push(backtrack_stackpointer());
331 
332     int num_arguments = 3;
333     FrameAlign(num_arguments);
334 
335     // Put arguments into parameter registers. Parameters are
336     //   Address byte_offset1 - Address captured substring's start.
337     //   Address byte_offset2 - Address of current character position.
338     //   size_t byte_length - length of capture in bytes(!)
339 #ifdef _WIN64
340     // Compute and set byte_offset1 (start of capture).
341     __ lea(rcx, Operand(rsi, rdx, times_1, 0));
342     // Set byte_offset2.
343     __ lea(rdx, Operand(rsi, rdi, times_1, 0));
344     // Set byte_length.
345     __ movq(r8, rbx);
346 #else  // AMD64 calling convention
347     // Compute byte_offset2 (current position = rsi+rdi).
348     __ lea(rax, Operand(rsi, rdi, times_1, 0));
349     // Compute and set byte_offset1 (start of capture).
350     __ lea(rdi, Operand(rsi, rdx, times_1, 0));
351     // Set byte_offset2.
352     __ movq(rsi, rax);
353     // Set byte_length.
354     __ movq(rdx, rbx);
355 #endif
356     ExternalReference compare =
357         ExternalReference::re_case_insensitive_compare_uc16();
358     CallCFunction(compare, num_arguments);
359 
360     // Restore original values before reacting on result value.
361     __ Move(code_object_pointer(), masm_->CodeObject());
362     __ pop(backtrack_stackpointer());
363 #ifndef _WIN64
364     __ pop(rdi);
365     __ pop(rsi);
366 #endif
367 
368     // Check if function returned non-zero for success or zero for failure.
369     __ testq(rax, rax);
370     BranchOrBacktrack(zero, on_no_match);
371     // On success, increment position by length of capture.
372     // Requires that rbx is callee save (true for both Win64 and AMD64 ABIs).
373     __ addq(rdi, rbx);
374   }
375   __ bind(&fallthrough);
376 }
377 
378 
CheckNotBackReference(int start_reg,Label * on_no_match)379 void RegExpMacroAssemblerX64::CheckNotBackReference(
380     int start_reg,
381     Label* on_no_match) {
382   Label fallthrough;
383 
384   // Find length of back-referenced capture.
385   __ movq(rdx, register_location(start_reg));
386   __ movq(rax, register_location(start_reg + 1));
387   __ subq(rax, rdx);  // Length to check.
388 
389   // Fail on partial or illegal capture (start of capture after end of capture).
390   // This must not happen (no back-reference can reference a capture that wasn't
391   // closed before in the reg-exp).
392   __ Check(greater_equal, "Invalid capture referenced");
393 
394   // Succeed on empty capture (including non-participating capture)
395   __ j(equal, &fallthrough);
396 
397   // -----------------------
398   // rdx - Start of capture
399   // rax - length of capture
400 
401   // Check that there are sufficient characters left in the input.
402   __ movl(rbx, rdi);
403   __ addl(rbx, rax);
404   BranchOrBacktrack(greater, on_no_match);
405 
406   // Compute pointers to match string and capture string
407   __ lea(rbx, Operand(rsi, rdi, times_1, 0));  // Start of match.
408   __ addq(rdx, rsi);  // Start of capture.
409   __ lea(r9, Operand(rdx, rax, times_1, 0));  // End of capture
410 
411   // -----------------------
412   // rbx - current capture character address.
413   // rbx - current input character address .
414   // r9 - end of input to match (capture length after rbx).
415 
416   Label loop;
417   __ bind(&loop);
418   if (mode_ == ASCII) {
419     __ movzxbl(rax, Operand(rdx, 0));
420     __ cmpb(rax, Operand(rbx, 0));
421   } else {
422     ASSERT(mode_ == UC16);
423     __ movzxwl(rax, Operand(rdx, 0));
424     __ cmpw(rax, Operand(rbx, 0));
425   }
426   BranchOrBacktrack(not_equal, on_no_match);
427   // Increment pointers into capture and match string.
428   __ addq(rbx, Immediate(char_size()));
429   __ addq(rdx, Immediate(char_size()));
430   // Check if we have reached end of match area.
431   __ cmpq(rdx, r9);
432   __ j(below, &loop);
433 
434   // Success.
435   // Set current character position to position after match.
436   __ movq(rdi, rbx);
437   __ subq(rdi, rsi);
438 
439   __ bind(&fallthrough);
440 }
441 
442 
CheckNotRegistersEqual(int reg1,int reg2,Label * on_not_equal)443 void RegExpMacroAssemblerX64::CheckNotRegistersEqual(int reg1,
444                                                      int reg2,
445                                                      Label* on_not_equal) {
446   __ movq(rax, register_location(reg1));
447   __ cmpq(rax, register_location(reg2));
448   BranchOrBacktrack(not_equal, on_not_equal);
449 }
450 
451 
CheckNotCharacter(uint32_t c,Label * on_not_equal)452 void RegExpMacroAssemblerX64::CheckNotCharacter(uint32_t c,
453                                                 Label* on_not_equal) {
454   __ cmpl(current_character(), Immediate(c));
455   BranchOrBacktrack(not_equal, on_not_equal);
456 }
457 
458 
CheckCharacterAfterAnd(uint32_t c,uint32_t mask,Label * on_equal)459 void RegExpMacroAssemblerX64::CheckCharacterAfterAnd(uint32_t c,
460                                                      uint32_t mask,
461                                                      Label* on_equal) {
462   __ movl(rax, current_character());
463   __ and_(rax, Immediate(mask));
464   __ cmpl(rax, Immediate(c));
465   BranchOrBacktrack(equal, on_equal);
466 }
467 
468 
CheckNotCharacterAfterAnd(uint32_t c,uint32_t mask,Label * on_not_equal)469 void RegExpMacroAssemblerX64::CheckNotCharacterAfterAnd(uint32_t c,
470                                                         uint32_t mask,
471                                                         Label* on_not_equal) {
472   __ movl(rax, current_character());
473   __ and_(rax, Immediate(mask));
474   __ cmpl(rax, Immediate(c));
475   BranchOrBacktrack(not_equal, on_not_equal);
476 }
477 
478 
CheckNotCharacterAfterMinusAnd(uc16 c,uc16 minus,uc16 mask,Label * on_not_equal)479 void RegExpMacroAssemblerX64::CheckNotCharacterAfterMinusAnd(
480     uc16 c,
481     uc16 minus,
482     uc16 mask,
483     Label* on_not_equal) {
484   ASSERT(minus < String::kMaxUC16CharCode);
485   __ lea(rax, Operand(current_character(), -minus));
486   __ and_(rax, Immediate(mask));
487   __ cmpl(rax, Immediate(c));
488   BranchOrBacktrack(not_equal, on_not_equal);
489 }
490 
491 
CheckSpecialCharacterClass(uc16 type,int cp_offset,bool check_offset,Label * on_no_match)492 bool RegExpMacroAssemblerX64::CheckSpecialCharacterClass(uc16 type,
493                                                          int cp_offset,
494                                                          bool check_offset,
495                                                          Label* on_no_match) {
496   // Range checks (c in min..max) are generally implemented by an unsigned
497   // (c - min) <= (max - min) check
498   switch (type) {
499   case 's':
500     // Match space-characters
501     if (mode_ == ASCII) {
502       // ASCII space characters are '\t'..'\r' and ' '.
503       if (check_offset) {
504         LoadCurrentCharacter(cp_offset, on_no_match);
505       } else {
506         LoadCurrentCharacterUnchecked(cp_offset, 1);
507       }
508       Label success;
509       __ cmpl(current_character(), Immediate(' '));
510       __ j(equal, &success);
511       // Check range 0x09..0x0d
512       __ subl(current_character(), Immediate('\t'));
513       __ cmpl(current_character(), Immediate('\r' - '\t'));
514       BranchOrBacktrack(above, on_no_match);
515       __ bind(&success);
516       return true;
517     }
518     return false;
519   case 'S':
520     // Match non-space characters.
521     if (check_offset) {
522       LoadCurrentCharacter(cp_offset, on_no_match, 1);
523     } else {
524       LoadCurrentCharacterUnchecked(cp_offset, 1);
525     }
526     if (mode_ == ASCII) {
527       // ASCII space characters are '\t'..'\r' and ' '.
528       __ cmpl(current_character(), Immediate(' '));
529       BranchOrBacktrack(equal, on_no_match);
530       __ subl(current_character(), Immediate('\t'));
531       __ cmpl(current_character(), Immediate('\r' - '\t'));
532       BranchOrBacktrack(below_equal, on_no_match);
533       return true;
534     }
535     return false;
536   case 'd':
537     // Match ASCII digits ('0'..'9')
538     if (check_offset) {
539       LoadCurrentCharacter(cp_offset, on_no_match, 1);
540     } else {
541       LoadCurrentCharacterUnchecked(cp_offset, 1);
542     }
543     __ subl(current_character(), Immediate('0'));
544     __ cmpl(current_character(), Immediate('9' - '0'));
545     BranchOrBacktrack(above, on_no_match);
546     return true;
547   case 'D':
548     // Match non ASCII-digits
549     if (check_offset) {
550       LoadCurrentCharacter(cp_offset, on_no_match, 1);
551     } else {
552       LoadCurrentCharacterUnchecked(cp_offset, 1);
553     }
554     __ subl(current_character(), Immediate('0'));
555     __ cmpl(current_character(), Immediate('9' - '0'));
556     BranchOrBacktrack(below_equal, on_no_match);
557     return true;
558   case '.': {
559     // Match non-newlines (not 0x0a('\n'), 0x0d('\r'), 0x2028 and 0x2029)
560     if (check_offset) {
561       LoadCurrentCharacter(cp_offset, on_no_match, 1);
562     } else {
563       LoadCurrentCharacterUnchecked(cp_offset, 1);
564     }
565     __ xor_(current_character(), Immediate(0x01));
566     // See if current character is '\n'^1 or '\r'^1, i.e., 0x0b or 0x0c
567     __ subl(current_character(), Immediate(0x0b));
568     __ cmpl(current_character(), Immediate(0x0c - 0x0b));
569     BranchOrBacktrack(below_equal, on_no_match);
570     if (mode_ == UC16) {
571       // Compare original value to 0x2028 and 0x2029, using the already
572       // computed (current_char ^ 0x01 - 0x0b). I.e., check for
573       // 0x201d (0x2028 - 0x0b) or 0x201e.
574       __ subl(current_character(), Immediate(0x2028 - 0x0b));
575       __ cmpl(current_character(), Immediate(1));
576       BranchOrBacktrack(below_equal, on_no_match);
577     }
578     return true;
579   }
580   case '*':
581     // Match any character.
582     if (check_offset) {
583       CheckPosition(cp_offset, on_no_match);
584     }
585     return true;
586   // No custom implementation (yet): w, W, s(UC16), S(UC16).
587   default:
588     return false;
589   }
590 }
591 
592 
Fail()593 void RegExpMacroAssemblerX64::Fail() {
594   ASSERT(FAILURE == 0);  // Return value for failure is zero.
595   __ xor_(rax, rax);  // zero rax.
596   __ jmp(&exit_label_);
597 }
598 
599 
GetCode(Handle<String> source)600 Handle<Object> RegExpMacroAssemblerX64::GetCode(Handle<String> source) {
601   // Finalize code - write the entry point code now we know how many
602   // registers we need.
603 
604   // Entry code:
605   __ bind(&entry_label_);
606   // Start new stack frame.
607   __ push(rbp);
608   __ movq(rbp, rsp);
609   // Save parameters and callee-save registers. Order here should correspond
610   //  to order of kBackup_ebx etc.
611 #ifdef _WIN64
612   // MSVC passes arguments in rcx, rdx, r8, r9, with backing stack slots.
613   // Store register parameters in pre-allocated stack slots,
614   __ movq(Operand(rbp, kInputString), rcx);
615   __ movzxlq(Operand(rbp, kStartIndex), rdx);  // Passed as int in eax.
616   __ movq(Operand(rbp, kInputStart), r8);
617   __ movq(Operand(rbp, kInputEnd), r9);
618   // Callee-save on Win64.
619   __ push(rsi);
620   __ push(rdi);
621   __ push(rbx);
622 #else
623   // GCC passes arguments in rdi, rsi, rdx, rcx, r8, r9 (and then on stack).
624   // Push register parameters on stack for reference.
625   ASSERT_EQ(kInputString, -1 * kPointerSize);
626   ASSERT_EQ(kStartIndex, -2 * kPointerSize);
627   ASSERT_EQ(kInputStart, -3 * kPointerSize);
628   ASSERT_EQ(kInputEnd, -4 * kPointerSize);
629   ASSERT_EQ(kRegisterOutput, -5 * kPointerSize);
630   ASSERT_EQ(kAtStart, -6 * kPointerSize);
631   __ push(rdi);
632   __ push(rsi);
633   __ push(rdx);
634   __ push(rcx);
635   __ push(r8);
636   __ push(r9);
637 
638   __ push(rbx);  // Callee-save
639 #endif
640   __ push(Immediate(0));  // Make room for "input start - 1" constant.
641 
642   // Check if we have space on the stack for registers.
643   Label stack_limit_hit;
644   Label stack_ok;
645 
646   ExternalReference stack_guard_limit =
647       ExternalReference::address_of_stack_guard_limit();
648   __ movq(rcx, rsp);
649   __ movq(kScratchRegister, stack_guard_limit);
650   __ subq(rcx, Operand(kScratchRegister, 0));
651   // Handle it if the stack pointer is already below the stack limit.
652   __ j(below_equal, &stack_limit_hit);
653   // Check if there is room for the variable number of registers above
654   // the stack limit.
655   __ cmpq(rcx, Immediate(num_registers_ * kPointerSize));
656   __ j(above_equal, &stack_ok);
657   // Exit with OutOfMemory exception. There is not enough space on the stack
658   // for our working registers.
659   __ movq(rax, Immediate(EXCEPTION));
660   __ jmp(&exit_label_);
661 
662   __ bind(&stack_limit_hit);
663   __ Move(code_object_pointer(), masm_->CodeObject());
664   CallCheckStackGuardState();  // Preserves no registers beside rbp and rsp.
665   __ testq(rax, rax);
666   // If returned value is non-zero, we exit with the returned value as result.
667   __ j(not_zero, &exit_label_);
668 
669   __ bind(&stack_ok);
670 
671   // Allocate space on stack for registers.
672   __ subq(rsp, Immediate(num_registers_ * kPointerSize));
673   // Load string length.
674   __ movq(rsi, Operand(rbp, kInputEnd));
675   // Load input position.
676   __ movq(rdi, Operand(rbp, kInputStart));
677   // Set up rdi to be negative offset from string end.
678   __ subq(rdi, rsi);
679   // Set rax to address of char before start of input
680   // (effectively string position -1).
681   __ lea(rax, Operand(rdi, -char_size()));
682   // Store this value in a local variable, for use when clearing
683   // position registers.
684   __ movq(Operand(rbp, kInputStartMinusOne), rax);
685   if (num_saved_registers_ > 0) {
686     // Fill saved registers with initial value = start offset - 1
687     // Fill in stack push order, to avoid accessing across an unwritten
688     // page (a problem on Windows).
689     __ movq(rcx, Immediate(kRegisterZero));
690     Label init_loop;
691     __ bind(&init_loop);
692     __ movq(Operand(rbp, rcx, times_1, 0), rax);
693     __ subq(rcx, Immediate(kPointerSize));
694     __ cmpq(rcx,
695             Immediate(kRegisterZero - num_saved_registers_ * kPointerSize));
696     __ j(greater, &init_loop);
697   }
698   // Ensure that we have written to each stack page, in order. Skipping a page
699   // on Windows can cause segmentation faults. Assuming page size is 4k.
700   const int kPageSize = 4096;
701   const int kRegistersPerPage = kPageSize / kPointerSize;
702   for (int i = num_saved_registers_ + kRegistersPerPage - 1;
703       i < num_registers_;
704       i += kRegistersPerPage) {
705     __ movq(register_location(i), rax);  // One write every page.
706   }
707 
708   // Initialize backtrack stack pointer.
709   __ movq(backtrack_stackpointer(), Operand(rbp, kStackHighEnd));
710   // Initialize code object pointer.
711   __ Move(code_object_pointer(), masm_->CodeObject());
712   // Load previous char as initial value of current-character.
713   Label at_start;
714   __ cmpb(Operand(rbp, kAtStart), Immediate(0));
715   __ j(not_equal, &at_start);
716   LoadCurrentCharacterUnchecked(-1, 1);  // Load previous char.
717   __ jmp(&start_label_);
718   __ bind(&at_start);
719   __ movq(current_character(), Immediate('\n'));
720   __ jmp(&start_label_);
721 
722 
723   // Exit code:
724   if (success_label_.is_linked()) {
725     // Save captures when successful.
726     __ bind(&success_label_);
727     if (num_saved_registers_ > 0) {
728       // copy captures to output
729       __ movq(rbx, Operand(rbp, kRegisterOutput));
730       __ movq(rcx, Operand(rbp, kInputEnd));
731       __ subq(rcx, Operand(rbp, kInputStart));
732       for (int i = 0; i < num_saved_registers_; i++) {
733         __ movq(rax, register_location(i));
734         __ addq(rax, rcx);  // Convert to index from start, not end.
735         if (mode_ == UC16) {
736           __ sar(rax, Immediate(1));  // Convert byte index to character index.
737         }
738         __ movl(Operand(rbx, i * kIntSize), rax);
739       }
740     }
741     __ movq(rax, Immediate(SUCCESS));
742   }
743 
744   // Exit and return rax
745   __ bind(&exit_label_);
746 
747 #ifdef _WIN64
748   // Restore callee save registers.
749   __ lea(rsp, Operand(rbp, kLastCalleeSaveRegister));
750   __ pop(rbx);
751   __ pop(rdi);
752   __ pop(rsi);
753   // Stack now at rbp.
754 #else
755   // Restore callee save register.
756   __ movq(rbx, Operand(rbp, kBackup_rbx));
757   // Skip rsp to rbp.
758   __ movq(rsp, rbp);
759 #endif
760   // Exit function frame, restore previous one.
761   __ pop(rbp);
762   __ ret(0);
763 
764   // Backtrack code (branch target for conditional backtracks).
765   if (backtrack_label_.is_linked()) {
766     __ bind(&backtrack_label_);
767     Backtrack();
768   }
769 
770   Label exit_with_exception;
771 
772   // Preempt-code
773   if (check_preempt_label_.is_linked()) {
774     SafeCallTarget(&check_preempt_label_);
775 
776     __ push(backtrack_stackpointer());
777     __ push(rdi);
778 
779     CallCheckStackGuardState();
780     __ testq(rax, rax);
781     // If returning non-zero, we should end execution with the given
782     // result as return value.
783     __ j(not_zero, &exit_label_);
784 
785     // Restore registers.
786     __ Move(code_object_pointer(), masm_->CodeObject());
787     __ pop(rdi);
788     __ pop(backtrack_stackpointer());
789     // String might have moved: Reload esi from frame.
790     __ movq(rsi, Operand(rbp, kInputEnd));
791     SafeReturn();
792   }
793 
794   // Backtrack stack overflow code.
795   if (stack_overflow_label_.is_linked()) {
796     SafeCallTarget(&stack_overflow_label_);
797     // Reached if the backtrack-stack limit has been hit.
798 
799     Label grow_failed;
800     // Save registers before calling C function
801 #ifndef _WIN64
802     // Callee-save in Microsoft 64-bit ABI, but not in AMD64 ABI.
803     __ push(rsi);
804     __ push(rdi);
805 #endif
806 
807     // Call GrowStack(backtrack_stackpointer())
808     int num_arguments = 2;
809     FrameAlign(num_arguments);
810 #ifdef _WIN64
811     // Microsoft passes parameters in rcx, rdx.
812     // First argument, backtrack stackpointer, is already in rcx.
813     __ lea(rdx, Operand(rbp, kStackHighEnd));  // Second argument
814 #else
815     // AMD64 ABI passes parameters in rdi, rsi.
816     __ movq(rdi, backtrack_stackpointer());   // First argument.
817     __ lea(rsi, Operand(rbp, kStackHighEnd));  // Second argument.
818 #endif
819     ExternalReference grow_stack = ExternalReference::re_grow_stack();
820     CallCFunction(grow_stack, num_arguments);
821     // If return NULL, we have failed to grow the stack, and
822     // must exit with a stack-overflow exception.
823     __ testq(rax, rax);
824     __ j(equal, &exit_with_exception);
825     // Otherwise use return value as new stack pointer.
826     __ movq(backtrack_stackpointer(), rax);
827     // Restore saved registers and continue.
828     __ Move(code_object_pointer(), masm_->CodeObject());
829 #ifndef _WIN64
830     __ pop(rdi);
831     __ pop(rsi);
832 #endif
833     SafeReturn();
834   }
835 
836   if (exit_with_exception.is_linked()) {
837     // If any of the code above needed to exit with an exception.
838     __ bind(&exit_with_exception);
839     // Exit with Result EXCEPTION(-1) to signal thrown exception.
840     __ movq(rax, Immediate(EXCEPTION));
841     __ jmp(&exit_label_);
842   }
843 
844   FixupCodeRelativePositions();
845 
846   CodeDesc code_desc;
847   masm_->GetCode(&code_desc);
848   Handle<Code> code = Factory::NewCode(code_desc,
849                                        NULL,
850                                        Code::ComputeFlags(Code::REGEXP),
851                                        masm_->CodeObject());
852   LOG(RegExpCodeCreateEvent(*code, *source));
853   return Handle<Object>::cast(code);
854 }
855 
856 
GoTo(Label * to)857 void RegExpMacroAssemblerX64::GoTo(Label* to) {
858   BranchOrBacktrack(no_condition, to);
859 }
860 
861 
IfRegisterGE(int reg,int comparand,Label * if_ge)862 void RegExpMacroAssemblerX64::IfRegisterGE(int reg,
863                                            int comparand,
864                                            Label* if_ge) {
865   __ cmpq(register_location(reg), Immediate(comparand));
866   BranchOrBacktrack(greater_equal, if_ge);
867 }
868 
869 
IfRegisterLT(int reg,int comparand,Label * if_lt)870 void RegExpMacroAssemblerX64::IfRegisterLT(int reg,
871                                            int comparand,
872                                            Label* if_lt) {
873   __ cmpq(register_location(reg), Immediate(comparand));
874   BranchOrBacktrack(less, if_lt);
875 }
876 
877 
IfRegisterEqPos(int reg,Label * if_eq)878 void RegExpMacroAssemblerX64::IfRegisterEqPos(int reg,
879                                               Label* if_eq) {
880   __ cmpq(rdi, register_location(reg));
881   BranchOrBacktrack(equal, if_eq);
882 }
883 
884 
885 RegExpMacroAssembler::IrregexpImplementation
Implementation()886     RegExpMacroAssemblerX64::Implementation() {
887   return kX64Implementation;
888 }
889 
890 
LoadCurrentCharacter(int cp_offset,Label * on_end_of_input,bool check_bounds,int characters)891 void RegExpMacroAssemblerX64::LoadCurrentCharacter(int cp_offset,
892                                                    Label* on_end_of_input,
893                                                    bool check_bounds,
894                                                    int characters) {
895   ASSERT(cp_offset >= -1);      // ^ and \b can look behind one character.
896   ASSERT(cp_offset < (1<<30));  // Be sane! (And ensure negation works)
897   if (check_bounds) {
898     CheckPosition(cp_offset + characters - 1, on_end_of_input);
899   }
900   LoadCurrentCharacterUnchecked(cp_offset, characters);
901 }
902 
903 
PopCurrentPosition()904 void RegExpMacroAssemblerX64::PopCurrentPosition() {
905   Pop(rdi);
906 }
907 
908 
PopRegister(int register_index)909 void RegExpMacroAssemblerX64::PopRegister(int register_index) {
910   Pop(rax);
911   __ movq(register_location(register_index), rax);
912 }
913 
914 
PushBacktrack(Label * label)915 void RegExpMacroAssemblerX64::PushBacktrack(Label* label) {
916   Push(label);
917   CheckStackLimit();
918 }
919 
920 
PushCurrentPosition()921 void RegExpMacroAssemblerX64::PushCurrentPosition() {
922   Push(rdi);
923 }
924 
925 
PushRegister(int register_index,StackCheckFlag check_stack_limit)926 void RegExpMacroAssemblerX64::PushRegister(int register_index,
927                                            StackCheckFlag check_stack_limit) {
928   __ movq(rax, register_location(register_index));
929   Push(rax);
930   if (check_stack_limit) CheckStackLimit();
931 }
932 
933 
ReadCurrentPositionFromRegister(int reg)934 void RegExpMacroAssemblerX64::ReadCurrentPositionFromRegister(int reg) {
935   __ movq(rdi, register_location(reg));
936 }
937 
938 
ReadStackPointerFromRegister(int reg)939 void RegExpMacroAssemblerX64::ReadStackPointerFromRegister(int reg) {
940   __ movq(backtrack_stackpointer(), register_location(reg));
941   __ addq(backtrack_stackpointer(), Operand(rbp, kStackHighEnd));
942 }
943 
944 
SetRegister(int register_index,int to)945 void RegExpMacroAssemblerX64::SetRegister(int register_index, int to) {
946   ASSERT(register_index >= num_saved_registers_);  // Reserved for positions!
947   __ movq(register_location(register_index), Immediate(to));
948 }
949 
950 
Succeed()951 void RegExpMacroAssemblerX64::Succeed() {
952   __ jmp(&success_label_);
953 }
954 
955 
WriteCurrentPositionToRegister(int reg,int cp_offset)956 void RegExpMacroAssemblerX64::WriteCurrentPositionToRegister(int reg,
957                                                              int cp_offset) {
958   if (cp_offset == 0) {
959     __ movq(register_location(reg), rdi);
960   } else {
961     __ lea(rax, Operand(rdi, cp_offset * char_size()));
962     __ movq(register_location(reg), rax);
963   }
964 }
965 
966 
ClearRegisters(int reg_from,int reg_to)967 void RegExpMacroAssemblerX64::ClearRegisters(int reg_from, int reg_to) {
968   ASSERT(reg_from <= reg_to);
969   __ movq(rax, Operand(rbp, kInputStartMinusOne));
970   for (int reg = reg_from; reg <= reg_to; reg++) {
971     __ movq(register_location(reg), rax);
972   }
973 }
974 
975 
WriteStackPointerToRegister(int reg)976 void RegExpMacroAssemblerX64::WriteStackPointerToRegister(int reg) {
977   __ movq(rax, backtrack_stackpointer());
978   __ subq(rax, Operand(rbp, kStackHighEnd));
979   __ movq(register_location(reg), rax);
980 }
981 
982 
983 // Private methods:
984 
CallCheckStackGuardState()985 void RegExpMacroAssemblerX64::CallCheckStackGuardState() {
986   // This function call preserves no register values. Caller should
987   // store anything volatile in a C call or overwritten by this function.
988   int num_arguments = 3;
989   FrameAlign(num_arguments);
990 #ifdef _WIN64
991   // Second argument: Code* of self. (Do this before overwriting r8).
992   __ movq(rdx, code_object_pointer());
993   // Third argument: RegExp code frame pointer.
994   __ movq(r8, rbp);
995   // First argument: Next address on the stack (will be address of
996   // return address).
997   __ lea(rcx, Operand(rsp, -kPointerSize));
998 #else
999   // Third argument: RegExp code frame pointer.
1000   __ movq(rdx, rbp);
1001   // Second argument: Code* of self.
1002   __ movq(rsi, code_object_pointer());
1003   // First argument: Next address on the stack (will be address of
1004   // return address).
1005   __ lea(rdi, Operand(rsp, -kPointerSize));
1006 #endif
1007   ExternalReference stack_check =
1008       ExternalReference::re_check_stack_guard_state();
1009   CallCFunction(stack_check, num_arguments);
1010 }
1011 
1012 
1013 // Helper function for reading a value out of a stack frame.
1014 template <typename T>
frame_entry(Address re_frame,int frame_offset)1015 static T& frame_entry(Address re_frame, int frame_offset) {
1016   return reinterpret_cast<T&>(Memory::int32_at(re_frame + frame_offset));
1017 }
1018 
1019 
CheckStackGuardState(Address * return_address,Code * re_code,Address re_frame)1020 int RegExpMacroAssemblerX64::CheckStackGuardState(Address* return_address,
1021                                                   Code* re_code,
1022                                                   Address re_frame) {
1023   if (StackGuard::IsStackOverflow()) {
1024     Top::StackOverflow();
1025     return EXCEPTION;
1026   }
1027 
1028   // If not real stack overflow the stack guard was used to interrupt
1029   // execution for another purpose.
1030 
1031   // Prepare for possible GC.
1032   HandleScope handles;
1033   Handle<Code> code_handle(re_code);
1034 
1035   Handle<String> subject(frame_entry<String*>(re_frame, kInputString));
1036   // Current string.
1037   bool is_ascii = subject->IsAsciiRepresentation();
1038 
1039   ASSERT(re_code->instruction_start() <= *return_address);
1040   ASSERT(*return_address <=
1041       re_code->instruction_start() + re_code->instruction_size());
1042 
1043   Object* result = Execution::HandleStackGuardInterrupt();
1044 
1045   if (*code_handle != re_code) {  // Return address no longer valid
1046     intptr_t delta = *code_handle - re_code;
1047     // Overwrite the return address on the stack.
1048     *return_address += delta;
1049   }
1050 
1051   if (result->IsException()) {
1052     return EXCEPTION;
1053   }
1054 
1055   // String might have changed.
1056   if (subject->IsAsciiRepresentation() != is_ascii) {
1057     // If we changed between an ASCII and an UC16 string, the specialized
1058     // code cannot be used, and we need to restart regexp matching from
1059     // scratch (including, potentially, compiling a new version of the code).
1060     return RETRY;
1061   }
1062 
1063   // Otherwise, the content of the string might have moved. It must still
1064   // be a sequential or external string with the same content.
1065   // Update the start and end pointers in the stack frame to the current
1066   // location (whether it has actually moved or not).
1067   ASSERT(StringShape(*subject).IsSequential() ||
1068       StringShape(*subject).IsExternal());
1069 
1070   // The original start address of the characters to match.
1071   const byte* start_address = frame_entry<const byte*>(re_frame, kInputStart);
1072 
1073   // Find the current start address of the same character at the current string
1074   // position.
1075   int start_index = frame_entry<int>(re_frame, kStartIndex);
1076   const byte* new_address = StringCharacterPosition(*subject, start_index);
1077 
1078   if (start_address != new_address) {
1079     // If there is a difference, update the object pointer and start and end
1080     // addresses in the RegExp stack frame to match the new value.
1081     const byte* end_address = frame_entry<const byte* >(re_frame, kInputEnd);
1082     int byte_length = end_address - start_address;
1083     frame_entry<const String*>(re_frame, kInputString) = *subject;
1084     frame_entry<const byte*>(re_frame, kInputStart) = new_address;
1085     frame_entry<const byte*>(re_frame, kInputEnd) = new_address + byte_length;
1086   }
1087 
1088   return 0;
1089 }
1090 
1091 
register_location(int register_index)1092 Operand RegExpMacroAssemblerX64::register_location(int register_index) {
1093   ASSERT(register_index < (1<<30));
1094   if (num_registers_ <= register_index) {
1095     num_registers_ = register_index + 1;
1096   }
1097   return Operand(rbp, kRegisterZero - register_index * kPointerSize);
1098 }
1099 
1100 
CheckPosition(int cp_offset,Label * on_outside_input)1101 void RegExpMacroAssemblerX64::CheckPosition(int cp_offset,
1102                                             Label* on_outside_input) {
1103   __ cmpl(rdi, Immediate(-cp_offset * char_size()));
1104   BranchOrBacktrack(greater_equal, on_outside_input);
1105 }
1106 
1107 
BranchOrBacktrack(Condition condition,Label * to)1108 void RegExpMacroAssemblerX64::BranchOrBacktrack(Condition condition,
1109                                                 Label* to) {
1110   if (condition < 0) {  // No condition
1111     if (to == NULL) {
1112       Backtrack();
1113       return;
1114     }
1115     __ jmp(to);
1116     return;
1117   }
1118   if (to == NULL) {
1119     __ j(condition, &backtrack_label_);
1120     return;
1121   }
1122   __ j(condition, to);
1123 }
1124 
1125 
SafeCall(Label * to)1126 void RegExpMacroAssemblerX64::SafeCall(Label* to) {
1127   __ call(to);
1128 }
1129 
1130 
SafeCallTarget(Label * label)1131 void RegExpMacroAssemblerX64::SafeCallTarget(Label* label) {
1132   __ bind(label);
1133   __ subq(Operand(rsp, 0), code_object_pointer());
1134 }
1135 
1136 
SafeReturn()1137 void RegExpMacroAssemblerX64::SafeReturn() {
1138   __ addq(Operand(rsp, 0), code_object_pointer());
1139   __ ret(0);
1140 }
1141 
1142 
Push(Register source)1143 void RegExpMacroAssemblerX64::Push(Register source) {
1144   ASSERT(!source.is(backtrack_stackpointer()));
1145   // Notice: This updates flags, unlike normal Push.
1146   __ subq(backtrack_stackpointer(), Immediate(kIntSize));
1147   __ movl(Operand(backtrack_stackpointer(), 0), source);
1148 }
1149 
1150 
Push(Immediate value)1151 void RegExpMacroAssemblerX64::Push(Immediate value) {
1152   // Notice: This updates flags, unlike normal Push.
1153   __ subq(backtrack_stackpointer(), Immediate(kIntSize));
1154   __ movl(Operand(backtrack_stackpointer(), 0), value);
1155 }
1156 
1157 
FixupCodeRelativePositions()1158 void RegExpMacroAssemblerX64::FixupCodeRelativePositions() {
1159   for (int i = 0, n = code_relative_fixup_positions_.length(); i < n; i++) {
1160     int position = code_relative_fixup_positions_[i];
1161     // The position succeeds a relative label offset from position.
1162     // Patch the relative offset to be relative to the Code object pointer
1163     // instead.
1164     int patch_position = position - kIntSize;
1165     int offset = masm_->long_at(patch_position);
1166     masm_->long_at_put(patch_position,
1167                        offset
1168                        + position
1169                        + Code::kHeaderSize
1170                        - kHeapObjectTag);
1171   }
1172   code_relative_fixup_positions_.Clear();
1173 }
1174 
1175 
Push(Label * backtrack_target)1176 void RegExpMacroAssemblerX64::Push(Label* backtrack_target) {
1177   __ subq(backtrack_stackpointer(), Immediate(kIntSize));
1178   __ movl(Operand(backtrack_stackpointer(), 0), backtrack_target);
1179   MarkPositionForCodeRelativeFixup();
1180 }
1181 
1182 
Pop(Register target)1183 void RegExpMacroAssemblerX64::Pop(Register target) {
1184   ASSERT(!target.is(backtrack_stackpointer()));
1185   __ movsxlq(target, Operand(backtrack_stackpointer(), 0));
1186   // Notice: This updates flags, unlike normal Pop.
1187   __ addq(backtrack_stackpointer(), Immediate(kIntSize));
1188 }
1189 
1190 
Drop()1191 void RegExpMacroAssemblerX64::Drop() {
1192   __ addq(backtrack_stackpointer(), Immediate(kIntSize));
1193 }
1194 
1195 
CheckPreemption()1196 void RegExpMacroAssemblerX64::CheckPreemption() {
1197   // Check for preemption.
1198   Label no_preempt;
1199   ExternalReference stack_guard_limit =
1200       ExternalReference::address_of_stack_guard_limit();
1201   __ load_rax(stack_guard_limit);
1202   __ cmpq(rsp, rax);
1203   __ j(above, &no_preempt);
1204 
1205   SafeCall(&check_preempt_label_);
1206 
1207   __ bind(&no_preempt);
1208 }
1209 
1210 
CheckStackLimit()1211 void RegExpMacroAssemblerX64::CheckStackLimit() {
1212   if (FLAG_check_stack) {
1213     Label no_stack_overflow;
1214     ExternalReference stack_limit =
1215         ExternalReference::address_of_regexp_stack_limit();
1216     __ load_rax(stack_limit);
1217     __ cmpq(backtrack_stackpointer(), rax);
1218     __ j(above, &no_stack_overflow);
1219 
1220     SafeCall(&stack_overflow_label_);
1221 
1222     __ bind(&no_stack_overflow);
1223   }
1224 }
1225 
1226 
FrameAlign(int num_arguments)1227 void RegExpMacroAssemblerX64::FrameAlign(int num_arguments) {
1228   // TODO(lrn): Since we no longer use the system stack arbitrarily (but we do
1229   // use it, e.g., for SafeCall), we know the number of elements on the stack
1230   // since the last frame alignment. We might be able to do this simpler then.
1231   int frameAlignment = OS::ActivationFrameAlignment();
1232   ASSERT(frameAlignment != 0);
1233   // Make stack end at alignment and make room for num_arguments pointers
1234   // (on Win64 only) and the original value of rsp.
1235   __ movq(kScratchRegister, rsp);
1236   ASSERT(IsPowerOf2(frameAlignment));
1237 #ifdef _WIN64
1238   // Allocate space for parameters and old rsp.
1239   __ subq(rsp, Immediate((num_arguments + 1) * kPointerSize));
1240   __ and_(rsp, Immediate(-frameAlignment));
1241   __ movq(Operand(rsp, num_arguments * kPointerSize), kScratchRegister);
1242 #else
1243   // Allocate space for old rsp.
1244   __ subq(rsp, Immediate(kPointerSize));
1245   __ and_(rsp, Immediate(-frameAlignment));
1246   __ movq(Operand(rsp, 0), kScratchRegister);
1247 #endif
1248 }
1249 
1250 
CallCFunction(ExternalReference function,int num_arguments)1251 void RegExpMacroAssemblerX64::CallCFunction(ExternalReference function,
1252                                             int num_arguments) {
1253   __ movq(rax, function);
1254   __ call(rax);
1255   ASSERT(OS::ActivationFrameAlignment() != 0);
1256 #ifdef _WIN64
1257   __ movq(rsp, Operand(rsp, num_arguments * kPointerSize));
1258 #else
1259   // All arguments passed in registers.
1260   ASSERT(num_arguments <= 6);
1261   __ pop(rsp);
1262 #endif
1263 }
1264 
1265 
LoadCurrentCharacterUnchecked(int cp_offset,int characters)1266 void RegExpMacroAssemblerX64::LoadCurrentCharacterUnchecked(int cp_offset,
1267                                                             int characters) {
1268   if (mode_ == ASCII) {
1269     if (characters == 4) {
1270       __ movl(current_character(), Operand(rsi, rdi, times_1, cp_offset));
1271     } else if (characters == 2) {
1272       __ movzxwl(current_character(), Operand(rsi, rdi, times_1, cp_offset));
1273     } else {
1274       ASSERT(characters == 1);
1275       __ movzxbl(current_character(), Operand(rsi, rdi, times_1, cp_offset));
1276     }
1277   } else {
1278     ASSERT(mode_ == UC16);
1279     if (characters == 2) {
1280       __ movl(current_character(),
1281               Operand(rsi, rdi, times_1, cp_offset * sizeof(uc16)));
1282     } else {
1283       ASSERT(characters == 1);
1284       __ movzxwl(current_character(),
1285                  Operand(rsi, rdi, times_1, cp_offset * sizeof(uc16)));
1286     }
1287   }
1288 }
1289 
1290 
Generate(MacroAssembler * masm_)1291 void RegExpCEntryStub::Generate(MacroAssembler* masm_) {
1292   __ int3();  // Unused on x64.
1293 }
1294 
1295 #undef __
1296 
1297 #endif  // V8_NATIVE_REGEXP
1298 
1299 }}  // namespace v8::internal
1300