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