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