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