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