regexp-macro-assembler-ia32.cc 40.8 KB
Newer Older
1
// Copyright 2012 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5
#if V8_TARGET_ARCH_IA32
6

7 8
#include "src/regexp/ia32/regexp-macro-assembler-ia32.h"

9 10
#include "src/codegen/assembler-inl.h"
#include "src/codegen/macro-assembler.h"
11
#include "src/logging/log.h"
12
#include "src/objects/objects-inl.h"
13 14
#include "src/regexp/regexp-macro-assembler.h"
#include "src/regexp/regexp-stack.h"
15
#include "src/strings/unicode.h"
16

17 18
namespace v8 {
namespace internal {
19

20 21
/*
 * This assembler uses the following register assignment convention
22 23 24 25
 * - edx : Current character.  Must be loaded using LoadCurrentCharacter
 *         before using any of the dispatch methods.  Temporarily stores the
 *         index of capture start after a matching pass for a global regexp.
 * - edi : Current position in input, as negative offset from end of string.
26
 *         Please notice that this is the byte offset, not the character offset!
27
 * - esi : end of input (points to byte after last character in input).
28
 * - ebp : Frame pointer.  Used to access arguments, local variables and
29
 *         RegExp registers.
30 31
 * - esp : Points to tip of C stack.
 * - ecx : Points to tip of backtrack stack
32
 *
33
 * The registers eax and ebx are free to use for computations.
34 35 36
 *
 * Each call to a public method should retain this convention.
 * The stack will have the following structure:
37 38 39
 *       - Address regexp       (address of the JSRegExp object; unused in
 *                               native code, passed to match signature of
 *                               the interpreter)
40
 *       - Isolate* isolate     (address of the current isolate)
41 42
 *       - direct_call          (if 1, direct call from JavaScript code, if 0
 *                               call through the runtime system)
43
 *       - stack_area_base      (high end of the memory area to use as
44
 *                               backtracking stack)
45
 *       - capture array size   (may fit multiple sets of matches)
46
 *       - int* capture_array   (int[num_saved_registers_], for output).
47 48
 *       - end of input         (address of end of string)
 *       - start of input       (address of first character in string)
49
 *       - start index          (character index of start)
50
 *       - String input_string  (location of a handle containing the string)
51
 *       --- frame alignment (if applicable) ---
52
 *       - return address
53
 * ebp-> - old ebp
54 55 56
 *       - backup of caller esi
 *       - backup of caller edi
 *       - backup of caller ebx
57
 *       - success counter      (only for global regexps to count matches).
58
 *       - Offset of location before start of input (effectively character
59 60
 *         string start - 1). Used to initialize capture registers to a
 *         non-position.
61
 *       - register 0  ebp[-4]  (only positions must be stored in the first
62
 *       - register 1  ebp[-8]   num_saved_registers_ registers)
63 64
 *       - ...
 *
65 66 67 68 69
 * The first num_saved_registers_ registers are initialized to point to
 * "character -1" in the string (i.e., char_size() bytes before the first
 * character of the string). The remaining registers starts out as garbage.
 *
 * The data up to the return address must be placed there by the calling
70
 * code, by calling the code entry as cast to a function with the signature:
71
 * int (*match)(String input_string,
72
 *              int start_index,
73 74 75
 *              Address start,
 *              Address end,
 *              int* capture_output_array,
76
 *              int num_capture_registers,
77
 *              byte* stack_area_base,
78
 *              bool direct_call = false,
79 80
 *              Isolate* isolate
 *              Address regexp);
81 82
 */

83
#define __ ACCESS_MASM(masm_)
84

85 86
const int RegExpMacroAssemblerIA32::kRegExpCodeSize;

87 88 89 90
RegExpMacroAssemblerIA32::RegExpMacroAssemblerIA32(Isolate* isolate, Zone* zone,
                                                   Mode mode,
                                                   int registers_to_save)
    : NativeRegExpMacroAssembler(isolate, zone),
91 92
      masm_(new MacroAssembler(isolate, CodeObjectRequired::kYes,
                               NewAssemblerBuffer(kRegExpCodeSize))),
93 94 95 96 97 98
      mode_(mode),
      num_registers_(registers_to_save),
      num_saved_registers_(registers_to_save),
      entry_label_(),
      start_label_(),
      success_label_(),
99
      backtrack_label_(),
100
      exit_label_() {
101 102 103
  // Irregexp code clobbers ebx and spills/restores it at all boundaries.
  masm_->set_root_array_available(false);

104
  DCHECK_EQ(0, registers_to_save % 2);
105 106 107 108 109 110 111 112 113 114
  __ jmp(&entry_label_);   // We'll write the entry code later.
  __ bind(&start_label_);  // And then continue from here.
}

RegExpMacroAssemblerIA32::~RegExpMacroAssemblerIA32() {
  delete masm_;
  // Unuse labels in case we throw away the assembler without calling GetCode.
  entry_label_.Unuse();
  start_label_.Unuse();
  success_label_.Unuse();
115
  backtrack_label_.Unuse();
116
  exit_label_.Unuse();
117
  check_preempt_label_.Unuse();
118 119 120 121 122 123
  stack_overflow_label_.Unuse();
}


int RegExpMacroAssemblerIA32::stack_limit_slack()  {
  return RegExpStack::kStackLimitSlack;
124 125 126 127
}


void RegExpMacroAssemblerIA32::AdvanceCurrentPosition(int by) {
128
  if (by != 0) {
129
    __ add(edi, Immediate(by * char_size()));
130
  }
131 132 133 134
}


void RegExpMacroAssemblerIA32::AdvanceRegister(int reg, int by) {
135 136
  DCHECK_LE(0, reg);
  DCHECK_GT(num_registers_, reg);
137 138 139
  if (by != 0) {
    __ add(register_location(reg), Immediate(by));
  }
140 141 142 143
}


void RegExpMacroAssemblerIA32::Backtrack() {
144
  CheckPreemption();
145 146 147 148 149 150 151 152 153 154 155
  if (has_backtrack_limit()) {
    Label next;
    __ inc(Operand(ebp, kBacktrackCount));
    __ cmp(Operand(ebp, kBacktrackCount), Immediate(backtrack_limit()));
    __ j(not_equal, &next);

    // Exceeded limits are treated as a failed match.
    Fail();

    __ bind(&next);
  }
156
  // Pop Code offset from backtrack stack, add Code and jump to location.
157
  Pop(ebx);
158 159
  __ add(ebx, Immediate(masm_->CodeObject()));
  __ jmp(ebx);
160 161 162 163 164 165 166
}


void RegExpMacroAssemblerIA32::Bind(Label* label) {
  __ bind(label);
}

167

168
void RegExpMacroAssemblerIA32::CheckCharacter(uint32_t c, Label* on_equal) {
169
  __ cmp(current_character(), c);
170 171 172 173 174
  BranchOrBacktrack(equal, on_equal);
}


void RegExpMacroAssemblerIA32::CheckCharacterGT(uc16 limit, Label* on_greater) {
175
  __ cmp(current_character(), limit);
176 177 178
  BranchOrBacktrack(greater, on_greater);
}

179 180
void RegExpMacroAssemblerIA32::CheckAtStart(int cp_offset, Label* on_at_start) {
  __ lea(eax, Operand(edi, -char_size() + cp_offset * char_size()));
181
  __ cmp(eax, Operand(ebp, kStringStartMinusOne));
182 183 184
  BranchOrBacktrack(equal, on_at_start);
}

185 186 187 188
void RegExpMacroAssemblerIA32::CheckNotAtStart(int cp_offset,
                                               Label* on_not_at_start) {
  __ lea(eax, Operand(edi, -char_size() + cp_offset * char_size()));
  __ cmp(eax, Operand(ebp, kStringStartMinusOne));
189
  BranchOrBacktrack(not_equal, on_not_at_start);
190 191 192
}


193
void RegExpMacroAssemblerIA32::CheckCharacterLT(uc16 limit, Label* on_less) {
194
  __ cmp(current_character(), limit);
195 196 197 198
  BranchOrBacktrack(less, on_less);
}


erik.corry@gmail.com's avatar
erik.corry@gmail.com committed
199 200
void RegExpMacroAssemblerIA32::CheckGreedyLoop(Label* on_equal) {
  Label fallthrough;
201
  __ cmp(edi, Operand(backtrack_stackpointer(), 0));
202
  __ j(not_equal, &fallthrough);
203
  __ add(backtrack_stackpointer(), Immediate(kSystemPointerSize));  // Pop.
erik.corry@gmail.com's avatar
erik.corry@gmail.com committed
204 205
  BranchOrBacktrack(no_condition, on_equal);
  __ bind(&fallthrough);
206 207
}

208
void RegExpMacroAssemblerIA32::CheckNotBackReferenceIgnoreCase(
209
    int start_reg, bool read_backward, bool unicode, Label* on_no_match) {
210
  Label fallthrough;
211 212
  __ mov(edx, register_location(start_reg));  // Index of start of capture
  __ mov(ebx, register_location(start_reg + 1));  // Index of end of capture
213
  __ sub(ebx, edx);  // Length of capture.
214

215 216 217
  // At this point, the capture registers are either both set or both cleared.
  // If the capture length is zero, then the capture is either empty or cleared.
  // Fall through in both cases.
218 219
  __ j(equal, &fallthrough);

220
  // Check that there are sufficient characters left in the input.
221 222 223 224 225 226 227 228 229 230
  if (read_backward) {
    __ mov(eax, Operand(ebp, kStringStartMinusOne));
    __ add(eax, ebx);
    __ cmp(edi, eax);
    BranchOrBacktrack(less_equal, on_no_match);
  } else {
    __ mov(eax, edi);
    __ add(eax, ebx);
    BranchOrBacktrack(greater, on_no_match);
  }
231

232
  if (mode_ == LATIN1) {
233 234
    Label success;
    Label fail;
235
    Label loop_increment;
236
    // Save register contents to make the registers available below.
237
    __ push(edi);
238
    __ push(backtrack_stackpointer());
239
    // After this, the eax, ecx, and edi registers are available.
240

241 242
    __ add(edx, esi);  // Start of capture
    __ add(edi, esi);  // Start of text to match against capture.
243 244 245
    if (read_backward) {
      __ sub(edi, ebx);  // Offset by length when matching backwards.
    }
246
    __ add(ebx, edi);  // End of text to match against capture.
247

248 249
    Label loop;
    __ bind(&loop);
250 251
    __ movzx_b(eax, Operand(edi, 0));
    __ cmpb_al(Operand(edx, 0));
252
    __ j(equal, &loop_increment);
253

254 255 256 257
    // Mismatch, try case-insensitive match (converting letters to lower-case).
    __ or_(eax, 0x20);  // Convert match character to lower-case.
    __ lea(ecx, Operand(eax, -'a'));
    __ cmp(ecx, static_cast<int32_t>('z' - 'a'));  // Is eax a lowercase letter?
258 259 260 261 262 263 264 265 266
    Label convert_capture;
    __ j(below_equal, &convert_capture);  // In range 'a'-'z'.
    // Latin-1: Check for values in range [224,254] but not 247.
    __ sub(ecx, Immediate(224 - 'a'));
    __ cmp(ecx, Immediate(254 - 224));
    __ j(above, &fail);  // Weren't Latin-1 letters.
    __ cmp(ecx, Immediate(247 - 224));  // Check for 247.
    __ j(equal, &fail);
    __ bind(&convert_capture);
267 268 269 270
    // Also convert capture character.
    __ movzx_b(ecx, Operand(edx, 0));
    __ or_(ecx, 0x20);

271
    __ cmp(eax, ecx);
272
    __ j(not_equal, &fail);
273 274

    __ bind(&loop_increment);
275
    // Increment pointers into match and capture strings.
276 277
    __ add(edx, Immediate(1));
    __ add(edi, Immediate(1));
278
    // Compare to end of match, and loop if not done.
279
    __ cmp(edi, ebx);
280
    __ j(below, &loop);
281
    __ jmp(&success);
282 283

    __ bind(&fail);
284 285
    // Restore original values before failing.
    __ pop(backtrack_stackpointer());
286 287 288 289
    __ pop(edi);
    BranchOrBacktrack(no_condition, on_no_match);

    __ bind(&success);
290 291 292
    // Restore original value before continuing.
    __ pop(backtrack_stackpointer());
    // Drop original value of character position.
293
    __ add(esp, Immediate(kSystemPointerSize));
294
    // Compute new value of character position after the matched part.
295
    __ sub(edi, esi);
296 297 298 299 300
    if (read_backward) {
      // Subtract match length if we matched backward.
      __ add(edi, register_location(start_reg));
      __ sub(edi, register_location(start_reg + 1));
    }
301
  } else {
302
    DCHECK(mode_ == UC16);
303
    // Save registers before calling C function.
304 305
    __ push(esi);
    __ push(edi);
306 307
    __ push(backtrack_stackpointer());
    __ push(ebx);
308

309
    static const int argument_count = 4;
310
    __ PrepareCallCFunction(argument_count, ecx);
311 312
    // Put arguments into allocated stack area, last argument highest on stack.
    // Parameters are
313 314
    //   Address byte_offset1 - Address captured substring's start.
    //   Address byte_offset2 - Address of current character position.
315
    //   size_t byte_length - length of capture in bytes(!)
316
    //   Isolate* isolate.
317

318
    // Set isolate.
319 320
    __ mov(Operand(esp, 3 * kSystemPointerSize),
           Immediate(ExternalReference::isolate_address(isolate())));
321
    // Set byte_length.
322
    __ mov(Operand(esp, 2 * kSystemPointerSize), ebx);
323 324
    // Set byte_offset2.
    // Found by adding negative string-end offset of current position (edi)
325
    // to end of string.
326
    __ add(edi, esi);
327 328 329
    if (read_backward) {
      __ sub(edi, ebx);  // Offset by length when matching backwards.
    }
330
    __ mov(Operand(esp, 1 * kSystemPointerSize), edi);
331
    // Set byte_offset1.
332
    // Start of capture, where edx already holds string-end negative offset.
333
    __ add(edx, esi);
334
    __ mov(Operand(esp, 0 * kSystemPointerSize), edx);
335

336 337 338
    {
      AllowExternalCallThatCantCauseGC scope(masm_);
      ExternalReference compare =
339 340 341 342
          unicode ? ExternalReference::re_case_insensitive_compare_unicode(
                        isolate())
                  : ExternalReference::re_case_insensitive_compare_non_unicode(
                        isolate());
343 344
      __ CallCFunction(compare, argument_count);
    }
345 346 347
    // Pop original values before reacting on result value.
    __ pop(ebx);
    __ pop(backtrack_stackpointer());
348 349
    __ pop(edi);
    __ pop(esi);
350

351
    // Check if function returned non-zero for success or zero for failure.
352
    __ or_(eax, eax);
353
    BranchOrBacktrack(zero, on_no_match);
354 355 356 357 358 359
    // On success, advance position by length of capture.
    if (read_backward) {
      __ sub(edi, ebx);
    } else {
      __ add(edi, ebx);
    }
360
  }
361
  __ bind(&fallthrough);
362 363
}

364 365 366
void RegExpMacroAssemblerIA32::CheckNotBackReference(int start_reg,
                                                     bool read_backward,
                                                     Label* on_no_match) {
367
  Label fallthrough;
368 369
  Label success;
  Label fail;
370 371

  // Find length of back-referenced capture.
372
  __ mov(edx, register_location(start_reg));
373
  __ mov(eax, register_location(start_reg + 1));
374
  __ sub(eax, edx);  // Length to check.
375 376 377 378

  // At this point, the capture registers are either both set or both cleared.
  // If the capture length is zero, then the capture is either empty or cleared.
  // Fall through in both cases.
379
  __ j(equal, &fallthrough);
380

381
  // Check that there are sufficient characters left in the input.
382 383 384 385 386 387 388 389 390 391
  if (read_backward) {
    __ mov(ebx, Operand(ebp, kStringStartMinusOne));
    __ add(ebx, eax);
    __ cmp(edi, ebx);
    BranchOrBacktrack(less_equal, on_no_match);
  } else {
    __ mov(ebx, edi);
    __ add(ebx, eax);
    BranchOrBacktrack(greater, on_no_match);
  }
392

393 394 395 396
  // Save register to make it available below.
  __ push(backtrack_stackpointer());

  // Compute pointers to match string and capture string
397
  __ add(edx, esi);  // Start of capture.
398 399 400 401
  __ lea(ebx, Operand(esi, edi, times_1, 0));  // Start of match.
  if (read_backward) {
    __ sub(ebx, eax);  // Offset by length when matching backwards.
  }
402
  __ lea(ecx, Operand(eax, ebx, times_1, 0));  // End of match
403 404 405

  Label loop;
  __ bind(&loop);
406
  if (mode_ == LATIN1) {
407
    __ movzx_b(eax, Operand(edx, 0));
408
    __ cmpb_al(Operand(ebx, 0));
409
  } else {
410
    DCHECK(mode_ == UC16);
411
    __ movzx_w(eax, Operand(edx, 0));
412
    __ cmpw_ax(Operand(ebx, 0));
413
  }
414
  __ j(not_equal, &fail);
415
  // Increment pointers into capture and match string.
416 417
  __ add(edx, Immediate(char_size()));
  __ add(ebx, Immediate(char_size()));
418
  // Check if we have reached end of match area.
419
  __ cmp(ebx, ecx);
420
  __ j(below, &loop);
421
  __ jmp(&success);
422 423

  __ bind(&fail);
424 425
  // Restore backtrack stackpointer.
  __ pop(backtrack_stackpointer());
426 427 428
  BranchOrBacktrack(no_condition, on_no_match);

  __ bind(&success);
429 430
  // Move current character position to position after match.
  __ mov(edi, ecx);
431
  __ sub(edi, esi);
432 433 434 435 436
  if (read_backward) {
    // Subtract match length if we matched backward.
    __ add(edi, register_location(start_reg));
    __ sub(edi, register_location(start_reg + 1));
  }
437 438 439
  // Restore backtrack stackpointer.
  __ pop(backtrack_stackpointer());

440 441 442 443
  __ bind(&fallthrough);
}


444 445
void RegExpMacroAssemblerIA32::CheckNotCharacter(uint32_t c,
                                                 Label* on_not_equal) {
446
  __ cmp(current_character(), c);
447 448 449 450
  BranchOrBacktrack(not_equal, on_not_equal);
}


451 452 453
void RegExpMacroAssemblerIA32::CheckCharacterAfterAnd(uint32_t c,
                                                      uint32_t mask,
                                                      Label* on_equal) {
454 455 456
  if (c == 0) {
    __ test(current_character(), Immediate(mask));
  } else {
457 458
    __ mov(eax, mask);
    __ and_(eax, current_character());
459 460
    __ cmp(eax, c);
  }
461 462 463 464 465 466 467
  BranchOrBacktrack(equal, on_equal);
}


void RegExpMacroAssemblerIA32::CheckNotCharacterAfterAnd(uint32_t c,
                                                         uint32_t mask,
                                                         Label* on_not_equal) {
468 469 470
  if (c == 0) {
    __ test(current_character(), Immediate(mask));
  } else {
471 472
    __ mov(eax, mask);
    __ and_(eax, current_character());
473 474
    __ cmp(eax, c);
  }
475 476 477 478
  BranchOrBacktrack(not_equal, on_not_equal);
}


479
void RegExpMacroAssemblerIA32::CheckNotCharacterAfterMinusAnd(
480
    uc16 c,
481
    uc16 minus,
482 483
    uc16 mask,
    Label* on_not_equal) {
484
  DCHECK_GT(String::kMaxUtf16CodeUnit, minus);
485
  __ lea(eax, Operand(current_character(), -minus));
486 487 488 489 490 491
  if (c == 0) {
    __ test(eax, Immediate(mask));
  } else {
    __ and_(eax, mask);
    __ cmp(eax, c);
  }
492 493 494
  BranchOrBacktrack(not_equal, on_not_equal);
}

495

496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
void RegExpMacroAssemblerIA32::CheckCharacterInRange(
    uc16 from,
    uc16 to,
    Label* on_in_range) {
  __ lea(eax, Operand(current_character(), -from));
  __ cmp(eax, to - from);
  BranchOrBacktrack(below_equal, on_in_range);
}


void RegExpMacroAssemblerIA32::CheckCharacterNotInRange(
    uc16 from,
    uc16 to,
    Label* on_not_in_range) {
  __ lea(eax, Operand(current_character(), -from));
  __ cmp(eax, to - from);
  BranchOrBacktrack(above, on_not_in_range);
}


void RegExpMacroAssemblerIA32::CheckBitInTable(
    Handle<ByteArray> table,
    Label* on_bit_set) {
  __ mov(eax, Immediate(table));
  Register index = current_character();
521
  if (mode_ != LATIN1 || kTableMask != String::kMaxOneByteCharCode) {
522 523
    __ mov(ebx, kTableSize - 1);
    __ and_(ebx, current_character());
524 525
    index = ebx;
  }
526 527
  __ cmpb(FieldOperand(eax, index, times_1, ByteArray::kHeaderSize),
          Immediate(0));
528 529 530 531
  BranchOrBacktrack(not_equal, on_bit_set);
}


532 533 534 535 536 537 538
bool RegExpMacroAssemblerIA32::CheckSpecialCharacterClass(uc16 type,
                                                          Label* on_no_match) {
  // Range checks (c in min..max) are generally implemented by an unsigned
  // (c - min) <= (max - min) check
  switch (type) {
  case 's':
    // Match space-characters
539
    if (mode_ == LATIN1) {
540
      // One byte space characters are '\t'..'\r', ' ' and \u00a0.
541 542
      Label success;
      __ cmp(current_character(), ' ');
543
      __ j(equal, &success, Label::kNear);
544
      // Check range 0x09..0x0D
545 546
      __ lea(eax, Operand(current_character(), -'\t'));
      __ cmp(eax, '\r' - '\t');
547 548
      __ j(below_equal, &success, Label::kNear);
      // \u00a0 (NBSP).
549
      __ cmp(eax, 0x00A0 - '\t');
550
      BranchOrBacktrack(not_equal, on_no_match);
551 552 553 554 555
      __ bind(&success);
      return true;
    }
    return false;
  case 'S':
556
    // The emitted code for generic character classes is good enough.
557 558 559
    return false;
  case 'd':
    // Match ASCII digits ('0'..'9')
560 561
    __ lea(eax, Operand(current_character(), -'0'));
    __ cmp(eax, '9' - '0');
562
    BranchOrBacktrack(above, on_no_match);
563 564 565
    return true;
  case 'D':
    // Match non ASCII-digits
566 567
    __ lea(eax, Operand(current_character(), -'0'));
    __ cmp(eax, '9' - '0');
568
    BranchOrBacktrack(below_equal, on_no_match);
569 570
    return true;
  case '.': {
571
    // Match non-newlines (not 0x0A('\n'), 0x0D('\r'), 0x2028 and 0x2029)
572 573
    __ mov(eax, current_character());
    __ xor_(eax, Immediate(0x01));
574 575 576
    // See if current character is '\n'^1 or '\r'^1, i.e., 0x0B or 0x0C
    __ sub(eax, Immediate(0x0B));
    __ cmp(eax, 0x0C - 0x0B);
577
    BranchOrBacktrack(below_equal, on_no_match);
578 579
    if (mode_ == UC16) {
      // Compare original value to 0x2028 and 0x2029, using the already
580 581 582
      // computed (current_char ^ 0x01 - 0x0B). I.e., check for
      // 0x201D (0x2028 - 0x0B) or 0x201E.
      __ sub(eax, Immediate(0x2028 - 0x0B));
583
      __ cmp(eax, 0x2029 - 0x2028);
584
      BranchOrBacktrack(below_equal, on_no_match);
585 586 587
    }
    return true;
  }
588
  case 'w': {
589 590
    if (mode_ != LATIN1) {
      // Table is 256 entries, so all Latin1 characters can be tested.
591
      __ cmp(current_character(), Immediate('z'));
592 593
      BranchOrBacktrack(above, on_no_match);
    }
594
    DCHECK_EQ(0, word_character_map[0]);  // Character '\0' is not a word char.
595 596
    ExternalReference word_map =
        ExternalReference::re_word_character_map(isolate());
597
    __ test_b(current_character(),
598 599
              Operand(current_character(), times_1, word_map.address(),
                      RelocInfo::EXTERNAL_REFERENCE));
600
    BranchOrBacktrack(zero, on_no_match);
601 602 603
    return true;
  }
  case 'W': {
604
    Label done;
605 606
    if (mode_ != LATIN1) {
      // Table is 256 entries, so all Latin1 characters can be tested.
607
      __ cmp(current_character(), Immediate('z'));
608
      __ j(above, &done);
609
    }
610
    DCHECK_EQ(0, word_character_map[0]);  // Character '\0' is not a word char.
611 612
    ExternalReference word_map =
        ExternalReference::re_word_character_map(isolate());
613
    __ test_b(current_character(),
614 615
              Operand(current_character(), times_1, word_map.address(),
                      RelocInfo::EXTERNAL_REFERENCE));
616
    BranchOrBacktrack(not_zero, on_no_match);
617
    if (mode_ != LATIN1) {
618 619
      __ bind(&done);
    }
620 621 622
    return true;
  }
  // Non-standard classes (with no syntactic shorthand) used internally.
623 624
  case '*':
    // Match any character.
625 626
    return true;
  case 'n': {
627
    // Match newlines (0x0A('\n'), 0x0D('\r'), 0x2028 or 0x2029).
628
    // The opposite of '.'.
629 630
    __ mov(eax, current_character());
    __ xor_(eax, Immediate(0x01));
631 632 633
    // See if current character is '\n'^1 or '\r'^1, i.e., 0x0B or 0x0C
    __ sub(eax, Immediate(0x0B));
    __ cmp(eax, 0x0C - 0x0B);
634
    if (mode_ == LATIN1) {
635 636 637 638
      BranchOrBacktrack(above, on_no_match);
    } else {
      Label done;
      BranchOrBacktrack(below_equal, &done);
639
      DCHECK_EQ(UC16, mode_);
640
      // Compare original value to 0x2028 and 0x2029, using the already
641 642 643
      // computed (current_char ^ 0x01 - 0x0B). I.e., check for
      // 0x201D (0x2028 - 0x0B) or 0x201E.
      __ sub(eax, Immediate(0x2028 - 0x0B));
644 645 646
      __ cmp(eax, 1);
      BranchOrBacktrack(above, on_no_match);
      __ bind(&done);
647 648
    }
    return true;
649 650
  }
  // No custom implementation (yet): s(UC16), S(UC16).
651 652 653 654
  default:
    return false;
  }
}
655 656 657


void RegExpMacroAssemblerIA32::Fail() {
658 659
  STATIC_ASSERT(FAILURE == 0);  // Return value for failure is zero.
  if (!global()) {
660
    __ Move(eax, Immediate(FAILURE));
661
  }
662 663 664 665
  __ jmp(&exit_label_);
}


666
Handle<HeapObject> RegExpMacroAssemblerIA32::GetCode(Handle<String> source) {
667
  Label return_eax;
668 669 670 671 672
  // Finalize code - write the entry point code now we know how many
  // registers we need.

  // Entry code:
  __ bind(&entry_label_);
673 674 675 676 677 678

  // Tell the system that we have a stack frame.  Because the type is MANUAL, no
  // code is generated.
  FrameScope scope(masm_, StackFrame::MANUAL);

  // Actually emit code to start a new stack frame.
679 680
  __ push(ebp);
  __ mov(ebp, esp);
681
  // Save callee-save registers. Order here should correspond to order of
682
  // kBackup_ebx etc.
683 684
  __ push(esi);
  __ push(edi);
685
  __ push(ebx);  // Callee-save on MacOS.
686 687

  STATIC_ASSERT(kSuccessfulCaptures == kBackup_ebx - kSystemPointerSize);
688
  __ push(Immediate(0));  // Number of successful matches in a global regexp.
689 690
  STATIC_ASSERT(kStringStartMinusOne ==
                kSuccessfulCaptures - kSystemPointerSize);
691
  __ push(Immediate(0));  // Make room for "string start - 1" constant.
692 693
  STATIC_ASSERT(kBacktrackCount == kStringStartMinusOne - kSystemPointerSize);
  __ push(Immediate(0));  // The backtrack counter.
694 695 696 697 698

  // Check if we have space on the stack for registers.
  Label stack_limit_hit;
  Label stack_ok;

699
  ExternalReference stack_limit =
700
      ExternalReference::address_of_jslimit(isolate());
701
  __ mov(ecx, esp);
702
  __ sub(ecx, StaticVariable(stack_limit));
703
  // Handle it if the stack pointer is already below the stack limit.
704
  __ j(below_equal, &stack_limit_hit);
705 706
  // Check if there is room for the variable number of registers above
  // the stack limit.
707
  __ cmp(ecx, num_registers_ * kSystemPointerSize);
708
  __ j(above_equal, &stack_ok);
709 710
  // Exit with OutOfMemory exception. There is not enough space on the stack
  // for our working registers.
711
  __ mov(eax, EXCEPTION);
712
  __ jmp(&return_eax);
713 714

  __ bind(&stack_limit_hit);
715
  CallCheckStackGuardState(ebx);
716
  __ or_(eax, eax);
717
  // If returned value is non-zero, we exit with the returned value as result.
718
  __ j(not_zero, &return_eax);
719 720

  __ bind(&stack_ok);
721 722
  // Load start index for later use.
  __ mov(ebx, Operand(ebp, kStartIndex));
723 724

  // Allocate space on stack for registers.
725
  __ AllocateStackSpace(num_registers_ * kSystemPointerSize);
726
  // Load string length.
727
  __ mov(esi, Operand(ebp, kInputEnd));
728
  // Load input position.
729
  __ mov(edi, Operand(ebp, kInputStart));
730
  // Set up edi to be negative offset from string end.
731
  __ sub(edi, esi);
732 733

  // Set eax to address of char before start of the string.
734
  // (effectively string position -1).
735 736 737 738 739 740
  __ neg(ebx);
  if (mode_ == UC16) {
    __ lea(eax, Operand(edi, ebx, times_2, -char_size()));
  } else {
    __ lea(eax, Operand(edi, ebx, times_1, -char_size()));
  }
741 742
  // Store this value in a local variable, for use when clearing
  // position registers.
743
  __ mov(Operand(ebp, kStringStartMinusOne), eax);
744

745 746 747 748 749 750 751 752 753 754 755 756 757 758
  Label load_char_start_regexp, start_regexp;
  // Load newline if index is at start, previous character otherwise.
  __ cmp(Operand(ebp, kStartIndex), Immediate(0));
  __ j(not_equal, &load_char_start_regexp, Label::kNear);
  __ mov(current_character(), '\n');
  __ jmp(&start_regexp, Label::kNear);

  // Global regexp restarts matching here.
  __ bind(&load_char_start_regexp);
  // Load previous char as initial value of current character register.
  LoadCurrentCharacterUnchecked(-1, 1);
  __ bind(&start_regexp);

  // Initialize on-stack registers.
759
  if (num_saved_registers_ > 0) {  // Always is, if generated from a regexp.
760
    // Fill saved registers with initial value = start offset - 1
761 762
    // Fill in stack push order, to avoid accessing across an unwritten
    // page (a problem on Windows).
763 764 765 766 767
    if (num_saved_registers_ > 8) {
      __ mov(ecx, kRegisterZero);
      Label init_loop;
      __ bind(&init_loop);
      __ mov(Operand(ebp, ecx, times_1, 0), eax);
768 769
      __ sub(ecx, Immediate(kSystemPointerSize));
      __ cmp(ecx, kRegisterZero - num_saved_registers_ * kSystemPointerSize);
770 771 772 773 774 775
      __ j(greater, &init_loop);
    } else {  // Unroll the loop.
      for (int i = 0; i < num_saved_registers_; i++) {
        __ mov(register_location(i), eax);
      }
    }
776
  }
777 778 779

  // Initialize backtrack stack pointer.
  __ mov(backtrack_stackpointer(), Operand(ebp, kStackHighEnd));
780

781
  __ jmp(&start_label_);
782

783
  // Exit code:
784
  if (success_label_.is_linked()) {
785
    // Save captures when successful.
786 787 788 789
    __ bind(&success_label_);
    if (num_saved_registers_ > 0) {
      // copy captures to output
      __ mov(ebx, Operand(ebp, kRegisterOutput));
790
      __ mov(ecx, Operand(ebp, kInputEnd));
791
      __ mov(edx, Operand(ebp, kStartIndex));
792
      __ sub(ecx, Operand(ebp, kInputStart));
793 794 795
      if (mode_ == UC16) {
        __ lea(ecx, Operand(ecx, edx, times_2, 0));
      } else {
796
        __ add(ecx, edx);
797
      }
798 799
      for (int i = 0; i < num_saved_registers_; i++) {
        __ mov(eax, register_location(i));
800
        if (i == 0 && global_with_zero_length_check()) {
801 802 803
          // Keep capture start in edx for the zero-length check later.
          __ mov(edx, eax);
        }
804
        // Convert to index from start of string, not end.
805
        __ add(eax, ecx);
806 807 808
        if (mode_ == UC16) {
          __ sar(eax, 1);  // Convert byte index to character index.
        }
809
        __ mov(Operand(ebx, i * kSystemPointerSize), eax);
810 811
      }
    }
812 813

    if (global()) {
814
      // Restart matching if the regular expression is flagged as global.
815 816 817 818 819 820 821 822 823 824 825 826 827
      // Increment success counter.
      __ inc(Operand(ebp, kSuccessfulCaptures));
      // Capture results have been stored, so the number of remaining global
      // output registers is reduced by the number of stored captures.
      __ mov(ecx, Operand(ebp, kNumOutputRegisters));
      __ sub(ecx, Immediate(num_saved_registers_));
      // Check whether we have enough room for another set of capture results.
      __ cmp(ecx, Immediate(num_saved_registers_));
      __ j(less, &exit_label_);

      __ mov(Operand(ebp, kNumOutputRegisters), ecx);
      // Advance the location for output.
      __ add(Operand(ebp, kRegisterOutput),
828
             Immediate(num_saved_registers_ * kSystemPointerSize));
829 830

      // Prepare eax to initialize registers with its value in the next run.
831
      __ mov(eax, Operand(ebp, kStringStartMinusOne));
832

833 834 835 836 837 838 839 840 841 842
      if (global_with_zero_length_check()) {
        // Special case for zero-length matches.
        // edx: capture start index
        __ cmp(edi, edx);
        // Not a zero-length match, restart.
        __ j(not_equal, &load_char_start_regexp);
        // edi (offset from the end) is zero if we already reached the end.
        __ test(edi, edi);
        __ j(zero, &exit_label_, Label::kNear);
        // Advance current position after a zero-length match.
843 844
        Label advance;
        __ bind(&advance);
845 846 847 848 849
        if (mode_ == UC16) {
          __ add(edi, Immediate(2));
        } else {
          __ inc(edi);
        }
850
        if (global_unicode()) CheckNotInSurrogatePair(0, &advance);
851 852 853 854 855
      }
      __ jmp(&load_char_start_regexp);
    } else {
      __ mov(eax, Immediate(SUCCESS));
    }
856
  }
857

858
  __ bind(&exit_label_);
859 860 861 862 863 864
  if (global()) {
    // Return the number of successful captures.
    __ mov(eax, Operand(ebp, kSuccessfulCaptures));
  }

  __ bind(&return_eax);
865 866 867
  // Skip esp past regexp registers.
  __ lea(esp, Operand(ebp, kBackup_ebx));
  // Restore callee-save registers.
868
  __ pop(ebx);
869 870
  __ pop(edi);
  __ pop(esi);
871
  // Exit function frame, restore previous one.
872
  __ pop(ebp);
873 874
  __ ret(0);

875 876 877 878 879 880
  // Backtrack code (branch target for conditional backtracks).
  if (backtrack_label_.is_linked()) {
    __ bind(&backtrack_label_);
    Backtrack();
  }

881 882
  Label exit_with_exception;

883 884
  // Preempt-code
  if (check_preempt_label_.is_linked()) {
885
    SafeCallTarget(&check_preempt_label_);
886 887

    __ push(backtrack_stackpointer());
888 889
    __ push(edi);

890
    CallCheckStackGuardState(ebx);
891
    __ or_(eax, eax);
892 893
    // If returning non-zero, we should end execution with the given
    // result as return value.
894
    __ j(not_zero, &return_eax);
895 896

    __ pop(edi);
897
    __ pop(backtrack_stackpointer());
898 899
    // String might have moved: Reload esi from frame.
    __ mov(esi, Operand(ebp, kInputEnd));
900 901 902 903 904
    SafeReturn();
  }

  // Backtrack stack overflow code.
  if (stack_overflow_label_.is_linked()) {
905
    SafeCallTarget(&stack_overflow_label_);
906 907 908 909 910 911 912
    // Reached if the backtrack-stack limit has been hit.

    // Save registers before calling C function
    __ push(esi);
    __ push(edi);

    // Call GrowStack(backtrack_stackpointer())
913
    static const int num_arguments = 3;
914
    __ PrepareCallCFunction(num_arguments, ebx);
915
    __ mov(Operand(esp, 2 * kSystemPointerSize),
916
           Immediate(ExternalReference::isolate_address(isolate())));
917
    __ lea(eax, Operand(ebp, kStackHighEnd));
918 919
    __ mov(Operand(esp, 1 * kSystemPointerSize), eax);
    __ mov(Operand(esp, 0 * kSystemPointerSize), backtrack_stackpointer());
920
    ExternalReference grow_stack =
921
        ExternalReference::re_grow_stack(isolate());
922
    __ CallCFunction(grow_stack, num_arguments);
923
    // If return nullptr, we have failed to grow the stack, and
924
    // must exit with a stack-overflow exception.
925
    __ or_(eax, eax);
926 927 928 929 930 931
    __ j(equal, &exit_with_exception);
    // Otherwise use return value as new stack pointer.
    __ mov(backtrack_stackpointer(), eax);
    // Restore saved registers and continue.
    __ pop(edi);
    __ pop(esi);
932
    SafeReturn();
933
  }
934

935 936 937 938 939
  if (exit_with_exception.is_linked()) {
    // If any of the code above needed to exit with an exception.
    __ bind(&exit_with_exception);
    // Exit with Result EXCEPTION(-1) to signal thrown exception.
    __ mov(eax, EXCEPTION);
940
    __ jmp(&return_eax);
941 942
  }

943
  CodeDesc code_desc;
944
  masm_->GetCode(masm_->isolate(), &code_desc);
945 946 947
  Handle<Code> code = Factory::CodeBuilder(isolate(), code_desc, Code::REGEXP)
                          .set_self_reference(masm_->CodeObject())
                          .Build();
948
  PROFILE(masm_->isolate(),
949
          RegExpCodeCreateEvent(Handle<AbstractCode>::cast(code), source));
950
  return Handle<HeapObject>::cast(code);
951 952 953 954
}


void RegExpMacroAssemblerIA32::GoTo(Label* to) {
955
  BranchOrBacktrack(no_condition, to);
956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974
}


void RegExpMacroAssemblerIA32::IfRegisterGE(int reg,
                                            int comparand,
                                            Label* if_ge) {
  __ cmp(register_location(reg), Immediate(comparand));
  BranchOrBacktrack(greater_equal, if_ge);
}


void RegExpMacroAssemblerIA32::IfRegisterLT(int reg,
                                            int comparand,
                                            Label* if_lt) {
  __ cmp(register_location(reg), Immediate(comparand));
  BranchOrBacktrack(less, if_lt);
}


975 976 977 978 979 980 981
void RegExpMacroAssemblerIA32::IfRegisterEqPos(int reg,
                                               Label* if_eq) {
  __ cmp(edi, register_location(reg));
  BranchOrBacktrack(equal, if_eq);
}


982 983 984 985 986 987 988
RegExpMacroAssembler::IrregexpImplementation
    RegExpMacroAssemblerIA32::Implementation() {
  return kIA32Implementation;
}


void RegExpMacroAssemblerIA32::PopCurrentPosition() {
989
  Pop(edi);
990 991 992 993
}


void RegExpMacroAssemblerIA32::PopRegister(int register_index) {
994 995
  Pop(eax);
  __ mov(register_location(register_index), eax);
996 997 998 999
}


void RegExpMacroAssemblerIA32::PushBacktrack(Label* label) {
1000
  Push(Immediate::CodeRelativeOffset(label));
1001
  CheckStackLimit();
1002 1003 1004 1005
}


void RegExpMacroAssemblerIA32::PushCurrentPosition() {
1006
  Push(edi);
1007 1008 1009
}


1010 1011 1012 1013 1014
void RegExpMacroAssemblerIA32::PushRegister(int register_index,
                                            StackCheckFlag check_stack_limit) {
  __ mov(eax, register_location(register_index));
  Push(eax);
  if (check_stack_limit) CheckStackLimit();
1015 1016 1017 1018 1019 1020 1021 1022 1023
}


void RegExpMacroAssemblerIA32::ReadCurrentPositionFromRegister(int reg) {
  __ mov(edi, register_location(reg));
}


void RegExpMacroAssemblerIA32::ReadStackPointerFromRegister(int reg) {
1024
  __ mov(backtrack_stackpointer(), register_location(reg));
1025
  __ add(backtrack_stackpointer(), Operand(ebp, kStackHighEnd));
1026 1027
}

1028
void RegExpMacroAssemblerIA32::SetCurrentPositionFromEnd(int by)  {
1029
  Label after_position;
1030
  __ cmp(edi, -by * char_size());
1031
  __ j(greater_equal, &after_position, Label::kNear);
1032 1033 1034 1035 1036 1037 1038
  __ mov(edi, -by * char_size());
  // On RegExp code entry (where this operation is used), the character before
  // the current position is expected to be already loaded.
  // We have advanced the position, so it's safe to read backwards.
  LoadCurrentCharacterUnchecked(-1, 1);
  __ bind(&after_position);
}
1039

1040

1041
void RegExpMacroAssemblerIA32::SetRegister(int register_index, int to) {
1042
  DCHECK(register_index >= num_saved_registers_);  // Reserved for positions!
1043 1044 1045 1046
  __ mov(register_location(register_index), Immediate(to));
}


1047
bool RegExpMacroAssemblerIA32::Succeed() {
1048
  __ jmp(&success_label_);
1049
  return global();
1050 1051 1052
}


erik.corry@gmail.com's avatar
erik.corry@gmail.com committed
1053 1054 1055 1056 1057
void RegExpMacroAssemblerIA32::WriteCurrentPositionToRegister(int reg,
                                                              int cp_offset) {
  if (cp_offset == 0) {
    __ mov(register_location(reg), edi);
  } else {
1058
    __ lea(eax, Operand(edi, cp_offset * char_size()));
erik.corry@gmail.com's avatar
erik.corry@gmail.com committed
1059 1060
    __ mov(register_location(reg), eax);
  }
1061 1062
}

erik.corry@gmail.com's avatar
erik.corry@gmail.com committed
1063

1064
void RegExpMacroAssemblerIA32::ClearRegisters(int reg_from, int reg_to) {
1065
  DCHECK(reg_from <= reg_to);
1066
  __ mov(eax, Operand(ebp, kStringStartMinusOne));
1067 1068 1069
  for (int reg = reg_from; reg <= reg_to; reg++) {
    __ mov(register_location(reg), eax);
  }
1070 1071 1072
}


1073
void RegExpMacroAssemblerIA32::WriteStackPointerToRegister(int reg) {
1074 1075 1076
  __ mov(eax, backtrack_stackpointer());
  __ sub(eax, Operand(ebp, kStackHighEnd));
  __ mov(register_location(reg), eax);
1077 1078 1079 1080 1081
}


// Private methods:

1082
void RegExpMacroAssemblerIA32::CallCheckStackGuardState(Register scratch) {
1083
  static const int num_arguments = 3;
1084
  __ PrepareCallCFunction(num_arguments, scratch);
1085
  // RegExp code frame pointer.
1086
  __ mov(Operand(esp, 2 * kSystemPointerSize), ebp);
1087
  // Code of self.
1088
  __ mov(Operand(esp, 1 * kSystemPointerSize), Immediate(masm_->CodeObject()));
1089
  // Next address on the stack (will be address of return address).
1090 1091
  __ lea(eax, Operand(esp, -kSystemPointerSize));
  __ mov(Operand(esp, 0 * kSystemPointerSize), eax);
lrn@chromium.org's avatar
lrn@chromium.org committed
1092
  ExternalReference check_stack_guard =
1093
      ExternalReference::re_check_stack_guard_state(isolate());
1094
  __ CallCFunction(check_stack_guard, num_arguments);
1095 1096
}

1097 1098 1099
Operand RegExpMacroAssemblerIA32::StaticVariable(const ExternalReference& ext) {
  return Operand(ext.address(), RelocInfo::EXTERNAL_REFERENCE);
}
1100 1101 1102 1103

// Helper function for reading a value out of a stack frame.
template <typename T>
static T& frame_entry(Address re_frame, int frame_offset) {
1104
  return reinterpret_cast<T&>(Memory<int32_t>(re_frame + frame_offset));
1105 1106 1107
}


1108 1109 1110 1111 1112
template <typename T>
static T* frame_entry_address(Address re_frame, int frame_offset) {
  return reinterpret_cast<T*>(re_frame + frame_offset);
}

1113
int RegExpMacroAssemblerIA32::CheckStackGuardState(Address* return_address,
1114
                                                   Address raw_code,
1115
                                                   Address re_frame) {
1116
  Code re_code = Code::cast(Object(raw_code));
1117 1118 1119
  return NativeRegExpMacroAssembler::CheckStackGuardState(
      frame_entry<Isolate*>(re_frame, kIsolate),
      frame_entry<int>(re_frame, kStartIndex),
1120 1121
      static_cast<RegExp::CallOrigin>(frame_entry<int>(re_frame, kDirectCall)),
      return_address, re_code,
1122
      frame_entry_address<Address>(re_frame, kInputString),
1123 1124
      frame_entry_address<const byte*>(re_frame, kInputStart),
      frame_entry_address<const byte*>(re_frame, kInputEnd));
1125 1126 1127
}


1128
Operand RegExpMacroAssemblerIA32::register_location(int register_index) {
1129
  DCHECK(register_index < (1<<30));
1130 1131 1132
  if (num_registers_ <= register_index) {
    num_registers_ = register_index + 1;
  }
1133
  return Operand(ebp, kRegisterZero - register_index * kSystemPointerSize);
1134 1135 1136
}


1137 1138
void RegExpMacroAssemblerIA32::CheckPosition(int cp_offset,
                                             Label* on_outside_input) {
1139 1140 1141 1142 1143 1144 1145 1146
  if (cp_offset >= 0) {
    __ cmp(edi, -cp_offset * char_size());
    BranchOrBacktrack(greater_equal, on_outside_input);
  } else {
    __ lea(eax, Operand(edi, cp_offset * char_size()));
    __ cmp(eax, Operand(ebp, kStringStartMinusOne));
    BranchOrBacktrack(less_equal, on_outside_input);
  }
1147 1148 1149
}


1150
void RegExpMacroAssemblerIA32::BranchOrBacktrack(Condition condition,
1151
                                                 Label* to) {
1152
  if (condition < 0) {  // No condition
1153
    if (to == nullptr) {
1154 1155 1156 1157 1158
      Backtrack();
      return;
    }
    __ jmp(to);
    return;
1159
  }
1160
  if (to == nullptr) {
1161
    __ j(condition, &backtrack_label_);
1162 1163
    return;
  }
1164
  __ j(condition, to);
1165 1166 1167
}


1168
void RegExpMacroAssemblerIA32::SafeCall(Label* to) {
sandholm@chromium.org's avatar
sandholm@chromium.org committed
1169 1170 1171 1172
  Label return_to;
  __ push(Immediate::CodeRelativeOffset(&return_to));
  __ jmp(to);
  __ bind(&return_to);
1173 1174 1175 1176
}


void RegExpMacroAssemblerIA32::SafeReturn() {
sandholm@chromium.org's avatar
sandholm@chromium.org committed
1177
  __ pop(ebx);
1178 1179
  __ add(ebx, Immediate(masm_->CodeObject()));
  __ jmp(ebx);
1180 1181 1182 1183 1184
}


void RegExpMacroAssemblerIA32::SafeCallTarget(Label* name) {
  __ bind(name);
1185 1186 1187 1188
}


void RegExpMacroAssemblerIA32::Push(Register source) {
1189
  DCHECK(source != backtrack_stackpointer());
1190
  // Notice: This updates flags, unlike normal Push.
1191
  __ sub(backtrack_stackpointer(), Immediate(kSystemPointerSize));
1192 1193 1194 1195 1196 1197
  __ mov(Operand(backtrack_stackpointer(), 0), source);
}


void RegExpMacroAssemblerIA32::Push(Immediate value) {
  // Notice: This updates flags, unlike normal Push.
1198
  __ sub(backtrack_stackpointer(), Immediate(kSystemPointerSize));
1199 1200 1201 1202 1203
  __ mov(Operand(backtrack_stackpointer(), 0), value);
}


void RegExpMacroAssemblerIA32::Pop(Register target) {
1204
  DCHECK(target != backtrack_stackpointer());
1205 1206
  __ mov(target, Operand(backtrack_stackpointer(), 0));
  // Notice: This updates flags, unlike normal Pop.
1207
  __ add(backtrack_stackpointer(), Immediate(kSystemPointerSize));
1208 1209 1210 1211 1212 1213
}


void RegExpMacroAssemblerIA32::CheckPreemption() {
  // Check for preemption.
  Label no_preempt;
1214
  ExternalReference stack_limit =
1215
      ExternalReference::address_of_jslimit(isolate());
1216
  __ cmp(esp, StaticVariable(stack_limit));
1217
  __ j(above, &no_preempt);
1218 1219 1220 1221

  SafeCall(&check_preempt_label_);

  __ bind(&no_preempt);
1222 1223 1224
}


1225
void RegExpMacroAssemblerIA32::CheckStackLimit() {
1226 1227
  Label no_stack_overflow;
  ExternalReference stack_limit =
1228
      ExternalReference::address_of_regexp_stack_limit_address(isolate());
1229
  __ cmp(backtrack_stackpointer(), StaticVariable(stack_limit));
1230
  __ j(above, &no_stack_overflow);
1231

1232
  SafeCall(&stack_overflow_label_);
1233

1234
  __ bind(&no_stack_overflow);
1235 1236 1237
}


1238 1239
void RegExpMacroAssemblerIA32::LoadCurrentCharacterUnchecked(int cp_offset,
                                                             int characters) {
1240
  if (mode_ == LATIN1) {
1241 1242 1243 1244 1245
    if (characters == 4) {
      __ mov(current_character(), Operand(esi, edi, times_1, cp_offset));
    } else if (characters == 2) {
      __ movzx_w(current_character(), Operand(esi, edi, times_1, cp_offset));
    } else {
1246
      DCHECK_EQ(1, characters);
1247 1248
      __ movzx_b(current_character(), Operand(esi, edi, times_1, cp_offset));
    }
1249
  } else {
1250
    DCHECK(mode_ == UC16);
1251 1252 1253 1254
    if (characters == 2) {
      __ mov(current_character(),
             Operand(esi, edi, times_1, cp_offset * sizeof(uc16)));
    } else {
1255
      DCHECK_EQ(1, characters);
1256 1257 1258
      __ movzx_w(current_character(),
                 Operand(esi, edi, times_1, cp_offset * sizeof(uc16)));
    }
1259 1260 1261 1262 1263
  }
}


#undef __
1264

1265 1266
}  // namespace internal
}  // namespace v8
1267 1268

#endif  // V8_TARGET_ARCH_IA32