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