1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
9 #include "src/cpu-profiler.h"
11 #include "src/macro-assembler.h"
12 #include "src/regexp-macro-assembler.h"
13 #include "src/regexp-stack.h"
14 #include "src/unicode.h"
15 #include "src/x87/regexp-macro-assembler-x87.h"
20 #ifndef V8_INTERPRETED_REGEXP
22 * This assembler uses the following register assignment convention
23 * - edx : Current character. Must be loaded using LoadCurrentCharacter
24 * before using any of the dispatch methods. Temporarily stores the
25 * index of capture start after a matching pass for a global regexp.
26 * - edi : Current position in input, as negative offset from end of string.
27 * Please notice that this is the byte offset, not the character offset!
28 * - esi : end of input (points to byte after last character in input).
29 * - ebp : Frame pointer. Used to access arguments, local variables and
31 * - esp : Points to tip of C stack.
32 * - ecx : Points to tip of backtrack stack
34 * The registers eax and ebx are free to use for computations.
36 * Each call to a public method should retain this convention.
37 * The stack will have the following structure:
38 * - Isolate* isolate (address of the current isolate)
39 * - direct_call (if 1, direct call from JavaScript code, if 0
40 * call through the runtime system)
41 * - stack_area_base (high end of the memory area to use as
43 * - capture array size (may fit multiple sets of matches)
44 * - int* capture_array (int[num_saved_registers_], for output).
45 * - end of input (address of end of string)
46 * - start of input (address of first character in string)
47 * - start index (character index of start)
48 * - String* input_string (location of a handle containing the string)
49 * --- frame alignment (if applicable) ---
52 * - backup of caller esi
53 * - backup of caller edi
54 * - backup of caller ebx
55 * - success counter (only for global regexps to count matches).
56 * - Offset of location before start of input (effectively character
57 * position -1). Used to initialize capture registers to a non-position.
58 * - register 0 ebp[-4] (only positions must be stored in the first
59 * - register 1 ebp[-8] num_saved_registers_ registers)
62 * The first num_saved_registers_ registers are initialized to point to
63 * "character -1" in the string (i.e., char_size() bytes before the first
64 * character of the string). The remaining registers starts out as garbage.
66 * The data up to the return address must be placed there by the calling
67 * code, by calling the code entry as cast to a function with the signature:
68 * int (*match)(String* input_string,
72 * int* capture_output_array,
74 * byte* stack_area_base,
78 #define __ ACCESS_MASM(masm_)
80 RegExpMacroAssemblerX87::RegExpMacroAssemblerX87(
82 int registers_to_save,
84 : NativeRegExpMacroAssembler(zone),
85 masm_(new MacroAssembler(zone->isolate(), NULL, kRegExpCodeSize)),
87 num_registers_(registers_to_save),
88 num_saved_registers_(registers_to_save),
94 DCHECK_EQ(0, registers_to_save % 2);
95 __ jmp(&entry_label_); // We'll write the entry code later.
96 __ bind(&start_label_); // And then continue from here.
100 RegExpMacroAssemblerX87::~RegExpMacroAssemblerX87() {
102 // Unuse labels in case we throw away the assembler without calling GetCode.
103 entry_label_.Unuse();
104 start_label_.Unuse();
105 success_label_.Unuse();
106 backtrack_label_.Unuse();
108 check_preempt_label_.Unuse();
109 stack_overflow_label_.Unuse();
113 int RegExpMacroAssemblerX87::stack_limit_slack() {
114 return RegExpStack::kStackLimitSlack;
118 void RegExpMacroAssemblerX87::AdvanceCurrentPosition(int by) {
120 __ add(edi, Immediate(by * char_size()));
125 void RegExpMacroAssemblerX87::AdvanceRegister(int reg, int by) {
127 DCHECK(reg < num_registers_);
129 __ add(register_location(reg), Immediate(by));
134 void RegExpMacroAssemblerX87::Backtrack() {
136 // Pop Code* offset from backtrack stack, add Code* and jump to location.
138 __ add(ebx, Immediate(masm_->CodeObject()));
143 void RegExpMacroAssemblerX87::Bind(Label* label) {
148 void RegExpMacroAssemblerX87::CheckCharacter(uint32_t c, Label* on_equal) {
149 __ cmp(current_character(), c);
150 BranchOrBacktrack(equal, on_equal);
154 void RegExpMacroAssemblerX87::CheckCharacterGT(uc16 limit, Label* on_greater) {
155 __ cmp(current_character(), limit);
156 BranchOrBacktrack(greater, on_greater);
160 void RegExpMacroAssemblerX87::CheckAtStart(Label* on_at_start) {
162 // Did we start the match at the start of the string at all?
163 __ cmp(Operand(ebp, kStartIndex), Immediate(0));
164 BranchOrBacktrack(not_equal, ¬_at_start);
165 // If we did, are we still at the start of the input?
166 __ lea(eax, Operand(esi, edi, times_1, 0));
167 __ cmp(eax, Operand(ebp, kInputStart));
168 BranchOrBacktrack(equal, on_at_start);
169 __ bind(¬_at_start);
173 void RegExpMacroAssemblerX87::CheckNotAtStart(Label* on_not_at_start) {
174 // Did we start the match at the start of the string at all?
175 __ cmp(Operand(ebp, kStartIndex), Immediate(0));
176 BranchOrBacktrack(not_equal, on_not_at_start);
177 // If we did, are we still at the start of the input?
178 __ lea(eax, Operand(esi, edi, times_1, 0));
179 __ cmp(eax, Operand(ebp, kInputStart));
180 BranchOrBacktrack(not_equal, on_not_at_start);
184 void RegExpMacroAssemblerX87::CheckCharacterLT(uc16 limit, Label* on_less) {
185 __ cmp(current_character(), limit);
186 BranchOrBacktrack(less, on_less);
190 void RegExpMacroAssemblerX87::CheckGreedyLoop(Label* on_equal) {
192 __ cmp(edi, Operand(backtrack_stackpointer(), 0));
193 __ j(not_equal, &fallthrough);
194 __ add(backtrack_stackpointer(), Immediate(kPointerSize)); // Pop.
195 BranchOrBacktrack(no_condition, on_equal);
196 __ bind(&fallthrough);
200 void RegExpMacroAssemblerX87::CheckNotBackReferenceIgnoreCase(
202 Label* on_no_match) {
204 __ mov(edx, register_location(start_reg)); // Index of start of capture
205 __ mov(ebx, register_location(start_reg + 1)); // Index of end of capture
206 __ sub(ebx, edx); // Length of capture.
208 // The length of a capture should not be negative. This can only happen
209 // if the end of the capture is unrecorded, or at a point earlier than
210 // the start of the capture.
211 BranchOrBacktrack(less, on_no_match);
213 // If length is zero, either the capture is empty or it is completely
214 // uncaptured. In either case succeed immediately.
215 __ j(equal, &fallthrough);
217 // Check that there are sufficient characters left in the input.
220 BranchOrBacktrack(greater, on_no_match);
222 if (mode_ == LATIN1) {
225 Label loop_increment;
226 // Save register contents to make the registers available below.
228 __ push(backtrack_stackpointer());
229 // After this, the eax, ecx, and edi registers are available.
231 __ add(edx, esi); // Start of capture
232 __ add(edi, esi); // Start of text to match against capture.
233 __ add(ebx, edi); // End of text to match against capture.
237 __ movzx_b(eax, Operand(edi, 0));
238 __ cmpb_al(Operand(edx, 0));
239 __ j(equal, &loop_increment);
241 // Mismatch, try case-insensitive match (converting letters to lower-case).
242 __ or_(eax, 0x20); // Convert match character to lower-case.
243 __ lea(ecx, Operand(eax, -'a'));
244 __ cmp(ecx, static_cast<int32_t>('z' - 'a')); // Is eax a lowercase letter?
245 Label convert_capture;
246 __ j(below_equal, &convert_capture); // In range 'a'-'z'.
247 // Latin-1: Check for values in range [224,254] but not 247.
248 __ sub(ecx, Immediate(224 - 'a'));
249 __ cmp(ecx, Immediate(254 - 224));
250 __ j(above, &fail); // Weren't Latin-1 letters.
251 __ cmp(ecx, Immediate(247 - 224)); // Check for 247.
253 __ bind(&convert_capture);
254 // Also convert capture character.
255 __ movzx_b(ecx, Operand(edx, 0));
259 __ j(not_equal, &fail);
261 __ bind(&loop_increment);
262 // Increment pointers into match and capture strings.
263 __ add(edx, Immediate(1));
264 __ add(edi, Immediate(1));
265 // Compare to end of match, and loop if not done.
271 // Restore original values before failing.
272 __ pop(backtrack_stackpointer());
274 BranchOrBacktrack(no_condition, on_no_match);
277 // Restore original value before continuing.
278 __ pop(backtrack_stackpointer());
279 // Drop original value of character position.
280 __ add(esp, Immediate(kPointerSize));
281 // Compute new value of character position after the matched part.
284 DCHECK(mode_ == UC16);
285 // Save registers before calling C function.
288 __ push(backtrack_stackpointer());
291 static const int argument_count = 4;
292 __ PrepareCallCFunction(argument_count, ecx);
293 // Put arguments into allocated stack area, last argument highest on stack.
295 // Address byte_offset1 - Address captured substring's start.
296 // Address byte_offset2 - Address of current character position.
297 // size_t byte_length - length of capture in bytes(!)
301 __ mov(Operand(esp, 3 * kPointerSize),
302 Immediate(ExternalReference::isolate_address(isolate())));
304 __ mov(Operand(esp, 2 * kPointerSize), ebx);
306 // Found by adding negative string-end offset of current position (edi)
309 __ mov(Operand(esp, 1 * kPointerSize), edi);
311 // Start of capture, where edx already holds string-end negative offset.
313 __ mov(Operand(esp, 0 * kPointerSize), edx);
316 AllowExternalCallThatCantCauseGC scope(masm_);
317 ExternalReference compare =
318 ExternalReference::re_case_insensitive_compare_uc16(isolate());
319 __ CallCFunction(compare, argument_count);
321 // Pop original values before reacting on result value.
323 __ pop(backtrack_stackpointer());
327 // Check if function returned non-zero for success or zero for failure.
329 BranchOrBacktrack(zero, on_no_match);
330 // On success, increment position by length of capture.
333 __ bind(&fallthrough);
337 void RegExpMacroAssemblerX87::CheckNotBackReference(
339 Label* on_no_match) {
344 // Find length of back-referenced capture.
345 __ mov(edx, register_location(start_reg));
346 __ mov(eax, register_location(start_reg + 1));
347 __ sub(eax, edx); // Length to check.
348 // Fail on partial or illegal capture (start of capture after end of capture).
349 BranchOrBacktrack(less, on_no_match);
350 // Succeed on empty capture (including no capture)
351 __ j(equal, &fallthrough);
353 // Check that there are sufficient characters left in the input.
356 BranchOrBacktrack(greater, on_no_match);
358 // Save register to make it available below.
359 __ push(backtrack_stackpointer());
361 // Compute pointers to match string and capture string
362 __ lea(ebx, Operand(esi, edi, times_1, 0)); // Start of match.
363 __ add(edx, esi); // Start of capture.
364 __ lea(ecx, Operand(eax, ebx, times_1, 0)); // End of match
368 if (mode_ == LATIN1) {
369 __ movzx_b(eax, Operand(edx, 0));
370 __ cmpb_al(Operand(ebx, 0));
372 DCHECK(mode_ == UC16);
373 __ movzx_w(eax, Operand(edx, 0));
374 __ cmpw_ax(Operand(ebx, 0));
376 __ j(not_equal, &fail);
377 // Increment pointers into capture and match string.
378 __ add(edx, Immediate(char_size()));
379 __ add(ebx, Immediate(char_size()));
380 // Check if we have reached end of match area.
386 // Restore backtrack stackpointer.
387 __ pop(backtrack_stackpointer());
388 BranchOrBacktrack(no_condition, on_no_match);
391 // Move current character position to position after match.
394 // Restore backtrack stackpointer.
395 __ pop(backtrack_stackpointer());
397 __ bind(&fallthrough);
401 void RegExpMacroAssemblerX87::CheckNotCharacter(uint32_t c,
402 Label* on_not_equal) {
403 __ cmp(current_character(), c);
404 BranchOrBacktrack(not_equal, on_not_equal);
408 void RegExpMacroAssemblerX87::CheckCharacterAfterAnd(uint32_t c,
412 __ test(current_character(), Immediate(mask));
415 __ and_(eax, current_character());
418 BranchOrBacktrack(equal, on_equal);
422 void RegExpMacroAssemblerX87::CheckNotCharacterAfterAnd(uint32_t c,
424 Label* on_not_equal) {
426 __ test(current_character(), Immediate(mask));
429 __ and_(eax, current_character());
432 BranchOrBacktrack(not_equal, on_not_equal);
436 void RegExpMacroAssemblerX87::CheckNotCharacterAfterMinusAnd(
440 Label* on_not_equal) {
441 DCHECK(minus < String::kMaxUtf16CodeUnit);
442 __ lea(eax, Operand(current_character(), -minus));
444 __ test(eax, Immediate(mask));
449 BranchOrBacktrack(not_equal, on_not_equal);
453 void RegExpMacroAssemblerX87::CheckCharacterInRange(
456 Label* on_in_range) {
457 __ lea(eax, Operand(current_character(), -from));
458 __ cmp(eax, to - from);
459 BranchOrBacktrack(below_equal, on_in_range);
463 void RegExpMacroAssemblerX87::CheckCharacterNotInRange(
466 Label* on_not_in_range) {
467 __ lea(eax, Operand(current_character(), -from));
468 __ cmp(eax, to - from);
469 BranchOrBacktrack(above, on_not_in_range);
473 void RegExpMacroAssemblerX87::CheckBitInTable(
474 Handle<ByteArray> table,
476 __ mov(eax, Immediate(table));
477 Register index = current_character();
478 if (mode_ != LATIN1 || kTableMask != String::kMaxOneByteCharCode) {
479 __ mov(ebx, kTableSize - 1);
480 __ and_(ebx, current_character());
483 __ cmpb(FieldOperand(eax, index, times_1, ByteArray::kHeaderSize), 0);
484 BranchOrBacktrack(not_equal, on_bit_set);
488 bool RegExpMacroAssemblerX87::CheckSpecialCharacterClass(uc16 type,
489 Label* on_no_match) {
490 // Range checks (c in min..max) are generally implemented by an unsigned
491 // (c - min) <= (max - min) check
494 // Match space-characters
495 if (mode_ == LATIN1) {
496 // One byte space characters are '\t'..'\r', ' ' and \u00a0.
498 __ cmp(current_character(), ' ');
499 __ j(equal, &success, Label::kNear);
500 // Check range 0x09..0x0d
501 __ lea(eax, Operand(current_character(), -'\t'));
502 __ cmp(eax, '\r' - '\t');
503 __ j(below_equal, &success, Label::kNear);
505 __ cmp(eax, 0x00a0 - '\t');
506 BranchOrBacktrack(not_equal, on_no_match);
512 // The emitted code for generic character classes is good enough.
515 // Match ASCII digits ('0'..'9')
516 __ lea(eax, Operand(current_character(), -'0'));
517 __ cmp(eax, '9' - '0');
518 BranchOrBacktrack(above, on_no_match);
521 // Match non ASCII-digits
522 __ lea(eax, Operand(current_character(), -'0'));
523 __ cmp(eax, '9' - '0');
524 BranchOrBacktrack(below_equal, on_no_match);
527 // Match non-newlines (not 0x0a('\n'), 0x0d('\r'), 0x2028 and 0x2029)
528 __ mov(eax, current_character());
529 __ xor_(eax, Immediate(0x01));
530 // See if current character is '\n'^1 or '\r'^1, i.e., 0x0b or 0x0c
531 __ sub(eax, Immediate(0x0b));
532 __ cmp(eax, 0x0c - 0x0b);
533 BranchOrBacktrack(below_equal, on_no_match);
535 // Compare original value to 0x2028 and 0x2029, using the already
536 // computed (current_char ^ 0x01 - 0x0b). I.e., check for
537 // 0x201d (0x2028 - 0x0b) or 0x201e.
538 __ sub(eax, Immediate(0x2028 - 0x0b));
539 __ cmp(eax, 0x2029 - 0x2028);
540 BranchOrBacktrack(below_equal, on_no_match);
545 if (mode_ != LATIN1) {
546 // Table is 256 entries, so all Latin1 characters can be tested.
547 __ cmp(current_character(), Immediate('z'));
548 BranchOrBacktrack(above, on_no_match);
550 DCHECK_EQ(0, word_character_map[0]); // Character '\0' is not a word char.
551 ExternalReference word_map = ExternalReference::re_word_character_map();
552 __ test_b(current_character(),
553 Operand::StaticArray(current_character(), times_1, word_map));
554 BranchOrBacktrack(zero, on_no_match);
559 if (mode_ != LATIN1) {
560 // Table is 256 entries, so all Latin1 characters can be tested.
561 __ cmp(current_character(), Immediate('z'));
564 DCHECK_EQ(0, word_character_map[0]); // Character '\0' is not a word char.
565 ExternalReference word_map = ExternalReference::re_word_character_map();
566 __ test_b(current_character(),
567 Operand::StaticArray(current_character(), times_1, word_map));
568 BranchOrBacktrack(not_zero, on_no_match);
569 if (mode_ != LATIN1) {
574 // Non-standard classes (with no syntactic shorthand) used internally.
576 // Match any character.
579 // Match newlines (0x0a('\n'), 0x0d('\r'), 0x2028 or 0x2029).
580 // The opposite of '.'.
581 __ mov(eax, current_character());
582 __ xor_(eax, Immediate(0x01));
583 // See if current character is '\n'^1 or '\r'^1, i.e., 0x0b or 0x0c
584 __ sub(eax, Immediate(0x0b));
585 __ cmp(eax, 0x0c - 0x0b);
586 if (mode_ == LATIN1) {
587 BranchOrBacktrack(above, on_no_match);
590 BranchOrBacktrack(below_equal, &done);
591 DCHECK_EQ(UC16, mode_);
592 // Compare original value to 0x2028 and 0x2029, using the already
593 // computed (current_char ^ 0x01 - 0x0b). I.e., check for
594 // 0x201d (0x2028 - 0x0b) or 0x201e.
595 __ sub(eax, Immediate(0x2028 - 0x0b));
597 BranchOrBacktrack(above, on_no_match);
602 // No custom implementation (yet): s(UC16), S(UC16).
609 void RegExpMacroAssemblerX87::Fail() {
610 STATIC_ASSERT(FAILURE == 0); // Return value for failure is zero.
612 __ Move(eax, Immediate(FAILURE));
614 __ jmp(&exit_label_);
618 Handle<HeapObject> RegExpMacroAssemblerX87::GetCode(Handle<String> source) {
620 // Finalize code - write the entry point code now we know how many
621 // registers we need.
624 __ bind(&entry_label_);
626 // Tell the system that we have a stack frame. Because the type is MANUAL, no
627 // code is generated.
628 FrameScope scope(masm_, StackFrame::MANUAL);
630 // Actually emit code to start a new stack frame.
633 // Save callee-save registers. Order here should correspond to order of
637 __ push(ebx); // Callee-save on MacOS.
638 __ push(Immediate(0)); // Number of successful matches in a global regexp.
639 __ push(Immediate(0)); // Make room for "input start - 1" constant.
641 // Check if we have space on the stack for registers.
642 Label stack_limit_hit;
645 ExternalReference stack_limit =
646 ExternalReference::address_of_stack_limit(isolate());
648 __ sub(ecx, Operand::StaticVariable(stack_limit));
649 // Handle it if the stack pointer is already below the stack limit.
650 __ j(below_equal, &stack_limit_hit);
651 // Check if there is room for the variable number of registers above
653 __ cmp(ecx, num_registers_ * kPointerSize);
654 __ j(above_equal, &stack_ok);
655 // Exit with OutOfMemory exception. There is not enough space on the stack
656 // for our working registers.
657 __ mov(eax, EXCEPTION);
660 __ bind(&stack_limit_hit);
661 CallCheckStackGuardState(ebx);
663 // If returned value is non-zero, we exit with the returned value as result.
664 __ j(not_zero, &return_eax);
667 // Load start index for later use.
668 __ mov(ebx, Operand(ebp, kStartIndex));
670 // Allocate space on stack for registers.
671 __ sub(esp, Immediate(num_registers_ * kPointerSize));
672 // Load string length.
673 __ mov(esi, Operand(ebp, kInputEnd));
674 // Load input position.
675 __ mov(edi, Operand(ebp, kInputStart));
676 // Set up edi to be negative offset from string end.
679 // Set eax to address of char before start of the string.
680 // (effectively string position -1).
683 __ lea(eax, Operand(edi, ebx, times_2, -char_size()));
685 __ lea(eax, Operand(edi, ebx, times_1, -char_size()));
687 // Store this value in a local variable, for use when clearing
688 // position registers.
689 __ mov(Operand(ebp, kInputStartMinusOne), eax);
692 // Ensure that we write to each stack page, in order. Skipping a page
693 // on Windows can cause segmentation faults. Assuming page size is 4k.
694 const int kPageSize = 4096;
695 const int kRegistersPerPage = kPageSize / kPointerSize;
696 for (int i = num_saved_registers_ + kRegistersPerPage - 1;
698 i += kRegistersPerPage) {
699 __ mov(register_location(i), eax); // One write every page.
703 Label load_char_start_regexp, start_regexp;
704 // Load newline if index is at start, previous character otherwise.
705 __ cmp(Operand(ebp, kStartIndex), Immediate(0));
706 __ j(not_equal, &load_char_start_regexp, Label::kNear);
707 __ mov(current_character(), '\n');
708 __ jmp(&start_regexp, Label::kNear);
710 // Global regexp restarts matching here.
711 __ bind(&load_char_start_regexp);
712 // Load previous char as initial value of current character register.
713 LoadCurrentCharacterUnchecked(-1, 1);
714 __ bind(&start_regexp);
716 // Initialize on-stack registers.
717 if (num_saved_registers_ > 0) { // Always is, if generated from a regexp.
718 // Fill saved registers with initial value = start offset - 1
719 // Fill in stack push order, to avoid accessing across an unwritten
720 // page (a problem on Windows).
721 if (num_saved_registers_ > 8) {
722 __ mov(ecx, kRegisterZero);
725 __ mov(Operand(ebp, ecx, times_1, 0), eax);
726 __ sub(ecx, Immediate(kPointerSize));
727 __ cmp(ecx, kRegisterZero - num_saved_registers_ * kPointerSize);
728 __ j(greater, &init_loop);
729 } else { // Unroll the loop.
730 for (int i = 0; i < num_saved_registers_; i++) {
731 __ mov(register_location(i), eax);
736 // Initialize backtrack stack pointer.
737 __ mov(backtrack_stackpointer(), Operand(ebp, kStackHighEnd));
739 __ jmp(&start_label_);
742 if (success_label_.is_linked()) {
743 // Save captures when successful.
744 __ bind(&success_label_);
745 if (num_saved_registers_ > 0) {
746 // copy captures to output
747 __ mov(ebx, Operand(ebp, kRegisterOutput));
748 __ mov(ecx, Operand(ebp, kInputEnd));
749 __ mov(edx, Operand(ebp, kStartIndex));
750 __ sub(ecx, Operand(ebp, kInputStart));
752 __ lea(ecx, Operand(ecx, edx, times_2, 0));
756 for (int i = 0; i < num_saved_registers_; i++) {
757 __ mov(eax, register_location(i));
758 if (i == 0 && global_with_zero_length_check()) {
759 // Keep capture start in edx for the zero-length check later.
762 // Convert to index from start of string, not end.
765 __ sar(eax, 1); // Convert byte index to character index.
767 __ mov(Operand(ebx, i * kPointerSize), eax);
772 // Restart matching if the regular expression is flagged as global.
773 // Increment success counter.
774 __ inc(Operand(ebp, kSuccessfulCaptures));
775 // Capture results have been stored, so the number of remaining global
776 // output registers is reduced by the number of stored captures.
777 __ mov(ecx, Operand(ebp, kNumOutputRegisters));
778 __ sub(ecx, Immediate(num_saved_registers_));
779 // Check whether we have enough room for another set of capture results.
780 __ cmp(ecx, Immediate(num_saved_registers_));
781 __ j(less, &exit_label_);
783 __ mov(Operand(ebp, kNumOutputRegisters), ecx);
784 // Advance the location for output.
785 __ add(Operand(ebp, kRegisterOutput),
786 Immediate(num_saved_registers_ * kPointerSize));
788 // Prepare eax to initialize registers with its value in the next run.
789 __ mov(eax, Operand(ebp, kInputStartMinusOne));
791 if (global_with_zero_length_check()) {
792 // Special case for zero-length matches.
793 // edx: capture start index
795 // Not a zero-length match, restart.
796 __ j(not_equal, &load_char_start_regexp);
797 // edi (offset from the end) is zero if we already reached the end.
799 __ j(zero, &exit_label_, Label::kNear);
800 // Advance current position after a zero-length match.
802 __ add(edi, Immediate(2));
808 __ jmp(&load_char_start_regexp);
810 __ mov(eax, Immediate(SUCCESS));
814 __ bind(&exit_label_);
816 // Return the number of successful captures.
817 __ mov(eax, Operand(ebp, kSuccessfulCaptures));
820 __ bind(&return_eax);
821 // Skip esp past regexp registers.
822 __ lea(esp, Operand(ebp, kBackup_ebx));
823 // Restore callee-save registers.
827 // Exit function frame, restore previous one.
831 // Backtrack code (branch target for conditional backtracks).
832 if (backtrack_label_.is_linked()) {
833 __ bind(&backtrack_label_);
837 Label exit_with_exception;
840 if (check_preempt_label_.is_linked()) {
841 SafeCallTarget(&check_preempt_label_);
843 __ push(backtrack_stackpointer());
846 CallCheckStackGuardState(ebx);
848 // If returning non-zero, we should end execution with the given
849 // result as return value.
850 __ j(not_zero, &return_eax);
853 __ pop(backtrack_stackpointer());
854 // String might have moved: Reload esi from frame.
855 __ mov(esi, Operand(ebp, kInputEnd));
859 // Backtrack stack overflow code.
860 if (stack_overflow_label_.is_linked()) {
861 SafeCallTarget(&stack_overflow_label_);
862 // Reached if the backtrack-stack limit has been hit.
865 // Save registers before calling C function
869 // Call GrowStack(backtrack_stackpointer())
870 static const int num_arguments = 3;
871 __ PrepareCallCFunction(num_arguments, ebx);
872 __ mov(Operand(esp, 2 * kPointerSize),
873 Immediate(ExternalReference::isolate_address(isolate())));
874 __ lea(eax, Operand(ebp, kStackHighEnd));
875 __ mov(Operand(esp, 1 * kPointerSize), eax);
876 __ mov(Operand(esp, 0 * kPointerSize), backtrack_stackpointer());
877 ExternalReference grow_stack =
878 ExternalReference::re_grow_stack(isolate());
879 __ CallCFunction(grow_stack, num_arguments);
880 // If return NULL, we have failed to grow the stack, and
881 // must exit with a stack-overflow exception.
883 __ j(equal, &exit_with_exception);
884 // Otherwise use return value as new stack pointer.
885 __ mov(backtrack_stackpointer(), eax);
886 // Restore saved registers and continue.
892 if (exit_with_exception.is_linked()) {
893 // If any of the code above needed to exit with an exception.
894 __ bind(&exit_with_exception);
895 // Exit with Result EXCEPTION(-1) to signal thrown exception.
896 __ mov(eax, EXCEPTION);
901 masm_->GetCode(&code_desc);
903 isolate()->factory()->NewCode(code_desc,
904 Code::ComputeFlags(Code::REGEXP),
905 masm_->CodeObject());
906 PROFILE(isolate(), RegExpCodeCreateEvent(*code, *source));
907 return Handle<HeapObject>::cast(code);
911 void RegExpMacroAssemblerX87::GoTo(Label* to) {
912 BranchOrBacktrack(no_condition, to);
916 void RegExpMacroAssemblerX87::IfRegisterGE(int reg,
919 __ cmp(register_location(reg), Immediate(comparand));
920 BranchOrBacktrack(greater_equal, if_ge);
924 void RegExpMacroAssemblerX87::IfRegisterLT(int reg,
927 __ cmp(register_location(reg), Immediate(comparand));
928 BranchOrBacktrack(less, if_lt);
932 void RegExpMacroAssemblerX87::IfRegisterEqPos(int reg,
934 __ cmp(edi, register_location(reg));
935 BranchOrBacktrack(equal, if_eq);
939 RegExpMacroAssembler::IrregexpImplementation
940 RegExpMacroAssemblerX87::Implementation() {
941 return kX87Implementation;
945 void RegExpMacroAssemblerX87::LoadCurrentCharacter(int cp_offset,
946 Label* on_end_of_input,
949 DCHECK(cp_offset >= -1); // ^ and \b can look behind one character.
950 DCHECK(cp_offset < (1<<30)); // Be sane! (And ensure negation works)
952 CheckPosition(cp_offset + characters - 1, on_end_of_input);
954 LoadCurrentCharacterUnchecked(cp_offset, characters);
958 void RegExpMacroAssemblerX87::PopCurrentPosition() {
963 void RegExpMacroAssemblerX87::PopRegister(int register_index) {
965 __ mov(register_location(register_index), eax);
969 void RegExpMacroAssemblerX87::PushBacktrack(Label* label) {
970 Push(Immediate::CodeRelativeOffset(label));
975 void RegExpMacroAssemblerX87::PushCurrentPosition() {
980 void RegExpMacroAssemblerX87::PushRegister(int register_index,
981 StackCheckFlag check_stack_limit) {
982 __ mov(eax, register_location(register_index));
984 if (check_stack_limit) CheckStackLimit();
988 void RegExpMacroAssemblerX87::ReadCurrentPositionFromRegister(int reg) {
989 __ mov(edi, register_location(reg));
993 void RegExpMacroAssemblerX87::ReadStackPointerFromRegister(int reg) {
994 __ mov(backtrack_stackpointer(), register_location(reg));
995 __ add(backtrack_stackpointer(), Operand(ebp, kStackHighEnd));
998 void RegExpMacroAssemblerX87::SetCurrentPositionFromEnd(int by) {
999 Label after_position;
1000 __ cmp(edi, -by * char_size());
1001 __ j(greater_equal, &after_position, Label::kNear);
1002 __ mov(edi, -by * char_size());
1003 // On RegExp code entry (where this operation is used), the character before
1004 // the current position is expected to be already loaded.
1005 // We have advanced the position, so it's safe to read backwards.
1006 LoadCurrentCharacterUnchecked(-1, 1);
1007 __ bind(&after_position);
1011 void RegExpMacroAssemblerX87::SetRegister(int register_index, int to) {
1012 DCHECK(register_index >= num_saved_registers_); // Reserved for positions!
1013 __ mov(register_location(register_index), Immediate(to));
1017 bool RegExpMacroAssemblerX87::Succeed() {
1018 __ jmp(&success_label_);
1023 void RegExpMacroAssemblerX87::WriteCurrentPositionToRegister(int reg,
1025 if (cp_offset == 0) {
1026 __ mov(register_location(reg), edi);
1028 __ lea(eax, Operand(edi, cp_offset * char_size()));
1029 __ mov(register_location(reg), eax);
1034 void RegExpMacroAssemblerX87::ClearRegisters(int reg_from, int reg_to) {
1035 DCHECK(reg_from <= reg_to);
1036 __ mov(eax, Operand(ebp, kInputStartMinusOne));
1037 for (int reg = reg_from; reg <= reg_to; reg++) {
1038 __ mov(register_location(reg), eax);
1043 void RegExpMacroAssemblerX87::WriteStackPointerToRegister(int reg) {
1044 __ mov(eax, backtrack_stackpointer());
1045 __ sub(eax, Operand(ebp, kStackHighEnd));
1046 __ mov(register_location(reg), eax);
1052 void RegExpMacroAssemblerX87::CallCheckStackGuardState(Register scratch) {
1053 static const int num_arguments = 3;
1054 __ PrepareCallCFunction(num_arguments, scratch);
1055 // RegExp code frame pointer.
1056 __ mov(Operand(esp, 2 * kPointerSize), ebp);
1058 __ mov(Operand(esp, 1 * kPointerSize), Immediate(masm_->CodeObject()));
1059 // Next address on the stack (will be address of return address).
1060 __ lea(eax, Operand(esp, -kPointerSize));
1061 __ mov(Operand(esp, 0 * kPointerSize), eax);
1062 ExternalReference check_stack_guard =
1063 ExternalReference::re_check_stack_guard_state(isolate());
1064 __ CallCFunction(check_stack_guard, num_arguments);
1068 // Helper function for reading a value out of a stack frame.
1069 template <typename T>
1070 static T& frame_entry(Address re_frame, int frame_offset) {
1071 return reinterpret_cast<T&>(Memory::int32_at(re_frame + frame_offset));
1075 int RegExpMacroAssemblerX87::CheckStackGuardState(Address* return_address,
1078 Isolate* isolate = frame_entry<Isolate*>(re_frame, kIsolate);
1079 StackLimitCheck check(isolate);
1080 if (check.JsHasOverflowed()) {
1081 isolate->StackOverflow();
1085 // If not real stack overflow the stack guard was used to interrupt
1086 // execution for another purpose.
1088 // If this is a direct call from JavaScript retry the RegExp forcing the call
1089 // through the runtime system. Currently the direct call cannot handle a GC.
1090 if (frame_entry<int>(re_frame, kDirectCall) == 1) {
1094 // Prepare for possible GC.
1095 HandleScope handles(isolate);
1096 Handle<Code> code_handle(re_code);
1098 Handle<String> subject(frame_entry<String*>(re_frame, kInputString));
1101 bool is_one_byte = subject->IsOneByteRepresentationUnderneath();
1103 DCHECK(re_code->instruction_start() <= *return_address);
1104 DCHECK(*return_address <=
1105 re_code->instruction_start() + re_code->instruction_size());
1107 Object* result = isolate->stack_guard()->HandleInterrupts();
1109 if (*code_handle != re_code) { // Return address no longer valid
1110 int delta = code_handle->address() - re_code->address();
1111 // Overwrite the return address on the stack.
1112 *return_address += delta;
1115 if (result->IsException()) {
1119 Handle<String> subject_tmp = subject;
1120 int slice_offset = 0;
1122 // Extract the underlying string and the slice offset.
1123 if (StringShape(*subject_tmp).IsCons()) {
1124 subject_tmp = Handle<String>(ConsString::cast(*subject_tmp)->first());
1125 } else if (StringShape(*subject_tmp).IsSliced()) {
1126 SlicedString* slice = SlicedString::cast(*subject_tmp);
1127 subject_tmp = Handle<String>(slice->parent());
1128 slice_offset = slice->offset();
1131 // String might have changed.
1132 if (subject_tmp->IsOneByteRepresentation() != is_one_byte) {
1133 // If we changed between an LATIN1 and an UC16 string, the specialized
1134 // code cannot be used, and we need to restart regexp matching from
1135 // scratch (including, potentially, compiling a new version of the code).
1139 // Otherwise, the content of the string might have moved. It must still
1140 // be a sequential or external string with the same content.
1141 // Update the start and end pointers in the stack frame to the current
1142 // location (whether it has actually moved or not).
1143 DCHECK(StringShape(*subject_tmp).IsSequential() ||
1144 StringShape(*subject_tmp).IsExternal());
1146 // The original start address of the characters to match.
1147 const byte* start_address = frame_entry<const byte*>(re_frame, kInputStart);
1149 // Find the current start address of the same character at the current string
1151 int start_index = frame_entry<int>(re_frame, kStartIndex);
1152 const byte* new_address = StringCharacterPosition(*subject_tmp,
1153 start_index + slice_offset);
1155 if (start_address != new_address) {
1156 // If there is a difference, update the object pointer and start and end
1157 // addresses in the RegExp stack frame to match the new value.
1158 const byte* end_address = frame_entry<const byte* >(re_frame, kInputEnd);
1159 int byte_length = static_cast<int>(end_address - start_address);
1160 frame_entry<const String*>(re_frame, kInputString) = *subject;
1161 frame_entry<const byte*>(re_frame, kInputStart) = new_address;
1162 frame_entry<const byte*>(re_frame, kInputEnd) = new_address + byte_length;
1163 } else if (frame_entry<const String*>(re_frame, kInputString) != *subject) {
1164 // Subject string might have been a ConsString that underwent
1165 // short-circuiting during GC. That will not change start_address but
1166 // will change pointer inside the subject handle.
1167 frame_entry<const String*>(re_frame, kInputString) = *subject;
1174 Operand RegExpMacroAssemblerX87::register_location(int register_index) {
1175 DCHECK(register_index < (1<<30));
1176 if (num_registers_ <= register_index) {
1177 num_registers_ = register_index + 1;
1179 return Operand(ebp, kRegisterZero - register_index * kPointerSize);
1183 void RegExpMacroAssemblerX87::CheckPosition(int cp_offset,
1184 Label* on_outside_input) {
1185 __ cmp(edi, -cp_offset * char_size());
1186 BranchOrBacktrack(greater_equal, on_outside_input);
1190 void RegExpMacroAssemblerX87::BranchOrBacktrack(Condition condition,
1192 if (condition < 0) { // No condition
1201 __ j(condition, &backtrack_label_);
1204 __ j(condition, to);
1208 void RegExpMacroAssemblerX87::SafeCall(Label* to) {
1210 __ push(Immediate::CodeRelativeOffset(&return_to));
1212 __ bind(&return_to);
1216 void RegExpMacroAssemblerX87::SafeReturn() {
1218 __ add(ebx, Immediate(masm_->CodeObject()));
1223 void RegExpMacroAssemblerX87::SafeCallTarget(Label* name) {
1228 void RegExpMacroAssemblerX87::Push(Register source) {
1229 DCHECK(!source.is(backtrack_stackpointer()));
1230 // Notice: This updates flags, unlike normal Push.
1231 __ sub(backtrack_stackpointer(), Immediate(kPointerSize));
1232 __ mov(Operand(backtrack_stackpointer(), 0), source);
1236 void RegExpMacroAssemblerX87::Push(Immediate value) {
1237 // Notice: This updates flags, unlike normal Push.
1238 __ sub(backtrack_stackpointer(), Immediate(kPointerSize));
1239 __ mov(Operand(backtrack_stackpointer(), 0), value);
1243 void RegExpMacroAssemblerX87::Pop(Register target) {
1244 DCHECK(!target.is(backtrack_stackpointer()));
1245 __ mov(target, Operand(backtrack_stackpointer(), 0));
1246 // Notice: This updates flags, unlike normal Pop.
1247 __ add(backtrack_stackpointer(), Immediate(kPointerSize));
1251 void RegExpMacroAssemblerX87::CheckPreemption() {
1252 // Check for preemption.
1254 ExternalReference stack_limit =
1255 ExternalReference::address_of_stack_limit(isolate());
1256 __ cmp(esp, Operand::StaticVariable(stack_limit));
1257 __ j(above, &no_preempt);
1259 SafeCall(&check_preempt_label_);
1261 __ bind(&no_preempt);
1265 void RegExpMacroAssemblerX87::CheckStackLimit() {
1266 Label no_stack_overflow;
1267 ExternalReference stack_limit =
1268 ExternalReference::address_of_regexp_stack_limit(isolate());
1269 __ cmp(backtrack_stackpointer(), Operand::StaticVariable(stack_limit));
1270 __ j(above, &no_stack_overflow);
1272 SafeCall(&stack_overflow_label_);
1274 __ bind(&no_stack_overflow);
1278 void RegExpMacroAssemblerX87::LoadCurrentCharacterUnchecked(int cp_offset,
1280 if (mode_ == LATIN1) {
1281 if (characters == 4) {
1282 __ mov(current_character(), Operand(esi, edi, times_1, cp_offset));
1283 } else if (characters == 2) {
1284 __ movzx_w(current_character(), Operand(esi, edi, times_1, cp_offset));
1286 DCHECK(characters == 1);
1287 __ movzx_b(current_character(), Operand(esi, edi, times_1, cp_offset));
1290 DCHECK(mode_ == UC16);
1291 if (characters == 2) {
1292 __ mov(current_character(),
1293 Operand(esi, edi, times_1, cp_offset * sizeof(uc16)));
1295 DCHECK(characters == 1);
1296 __ movzx_w(current_character(),
1297 Operand(esi, edi, times_1, cp_offset * sizeof(uc16)));
1305 #endif // V8_INTERPRETED_REGEXP
1307 }} // namespace v8::internal
1309 #endif // V8_TARGET_ARCH_X87