blob: 9c55af6645b1b507ba239099c20caf36abda3f4b [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 }
Ben Murdochda12d292016-06-02 14:46:10 +0100514 __ cmpb(FieldOperand(eax, index, times_1, ByteArray::kHeaderSize),
515 Immediate(0));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000516 BranchOrBacktrack(not_equal, on_bit_set);
517}
518
519
520bool RegExpMacroAssemblerIA32::CheckSpecialCharacterClass(uc16 type,
521 Label* on_no_match) {
522 // Range checks (c in min..max) are generally implemented by an unsigned
523 // (c - min) <= (max - min) check
524 switch (type) {
525 case 's':
526 // Match space-characters
527 if (mode_ == LATIN1) {
528 // One byte space characters are '\t'..'\r', ' ' and \u00a0.
529 Label success;
530 __ cmp(current_character(), ' ');
531 __ j(equal, &success, Label::kNear);
532 // Check range 0x09..0x0d
533 __ lea(eax, Operand(current_character(), -'\t'));
534 __ cmp(eax, '\r' - '\t');
535 __ j(below_equal, &success, Label::kNear);
536 // \u00a0 (NBSP).
537 __ cmp(eax, 0x00a0 - '\t');
538 BranchOrBacktrack(not_equal, on_no_match);
539 __ bind(&success);
540 return true;
541 }
542 return false;
543 case 'S':
544 // The emitted code for generic character classes is good enough.
545 return false;
546 case 'd':
547 // Match ASCII digits ('0'..'9')
548 __ lea(eax, Operand(current_character(), -'0'));
549 __ cmp(eax, '9' - '0');
550 BranchOrBacktrack(above, on_no_match);
551 return true;
552 case 'D':
553 // Match non ASCII-digits
554 __ lea(eax, Operand(current_character(), -'0'));
555 __ cmp(eax, '9' - '0');
556 BranchOrBacktrack(below_equal, on_no_match);
557 return true;
558 case '.': {
559 // Match non-newlines (not 0x0a('\n'), 0x0d('\r'), 0x2028 and 0x2029)
560 __ mov(eax, current_character());
561 __ xor_(eax, Immediate(0x01));
562 // See if current character is '\n'^1 or '\r'^1, i.e., 0x0b or 0x0c
563 __ sub(eax, Immediate(0x0b));
564 __ cmp(eax, 0x0c - 0x0b);
565 BranchOrBacktrack(below_equal, on_no_match);
566 if (mode_ == UC16) {
567 // Compare original value to 0x2028 and 0x2029, using the already
568 // computed (current_char ^ 0x01 - 0x0b). I.e., check for
569 // 0x201d (0x2028 - 0x0b) or 0x201e.
570 __ sub(eax, Immediate(0x2028 - 0x0b));
571 __ cmp(eax, 0x2029 - 0x2028);
572 BranchOrBacktrack(below_equal, on_no_match);
573 }
574 return true;
575 }
576 case 'w': {
577 if (mode_ != LATIN1) {
578 // Table is 256 entries, so all Latin1 characters can be tested.
579 __ cmp(current_character(), Immediate('z'));
580 BranchOrBacktrack(above, on_no_match);
581 }
582 DCHECK_EQ(0, word_character_map[0]); // Character '\0' is not a word char.
583 ExternalReference word_map = ExternalReference::re_word_character_map();
584 __ test_b(current_character(),
585 Operand::StaticArray(current_character(), times_1, word_map));
586 BranchOrBacktrack(zero, on_no_match);
587 return true;
588 }
589 case 'W': {
590 Label done;
591 if (mode_ != LATIN1) {
592 // Table is 256 entries, so all Latin1 characters can be tested.
593 __ cmp(current_character(), Immediate('z'));
594 __ j(above, &done);
595 }
596 DCHECK_EQ(0, word_character_map[0]); // Character '\0' is not a word char.
597 ExternalReference word_map = ExternalReference::re_word_character_map();
598 __ test_b(current_character(),
599 Operand::StaticArray(current_character(), times_1, word_map));
600 BranchOrBacktrack(not_zero, on_no_match);
601 if (mode_ != LATIN1) {
602 __ bind(&done);
603 }
604 return true;
605 }
606 // Non-standard classes (with no syntactic shorthand) used internally.
607 case '*':
608 // Match any character.
609 return true;
610 case 'n': {
611 // Match newlines (0x0a('\n'), 0x0d('\r'), 0x2028 or 0x2029).
612 // The opposite of '.'.
613 __ mov(eax, current_character());
614 __ xor_(eax, Immediate(0x01));
615 // See if current character is '\n'^1 or '\r'^1, i.e., 0x0b or 0x0c
616 __ sub(eax, Immediate(0x0b));
617 __ cmp(eax, 0x0c - 0x0b);
618 if (mode_ == LATIN1) {
619 BranchOrBacktrack(above, on_no_match);
620 } else {
621 Label done;
622 BranchOrBacktrack(below_equal, &done);
623 DCHECK_EQ(UC16, mode_);
624 // Compare original value to 0x2028 and 0x2029, using the already
625 // computed (current_char ^ 0x01 - 0x0b). I.e., check for
626 // 0x201d (0x2028 - 0x0b) or 0x201e.
627 __ sub(eax, Immediate(0x2028 - 0x0b));
628 __ cmp(eax, 1);
629 BranchOrBacktrack(above, on_no_match);
630 __ bind(&done);
631 }
632 return true;
633 }
634 // No custom implementation (yet): s(UC16), S(UC16).
635 default:
636 return false;
637 }
638}
639
640
641void RegExpMacroAssemblerIA32::Fail() {
642 STATIC_ASSERT(FAILURE == 0); // Return value for failure is zero.
643 if (!global()) {
644 __ Move(eax, Immediate(FAILURE));
645 }
646 __ jmp(&exit_label_);
647}
648
649
650Handle<HeapObject> RegExpMacroAssemblerIA32::GetCode(Handle<String> source) {
651 Label return_eax;
652 // Finalize code - write the entry point code now we know how many
653 // registers we need.
654
655 // Entry code:
656 __ bind(&entry_label_);
657
658 // Tell the system that we have a stack frame. Because the type is MANUAL, no
659 // code is generated.
660 FrameScope scope(masm_, StackFrame::MANUAL);
661
662 // Actually emit code to start a new stack frame.
663 __ push(ebp);
664 __ mov(ebp, esp);
665 // Save callee-save registers. Order here should correspond to order of
666 // kBackup_ebx etc.
667 __ push(esi);
668 __ push(edi);
669 __ push(ebx); // Callee-save on MacOS.
670 __ push(Immediate(0)); // Number of successful matches in a global regexp.
671 __ push(Immediate(0)); // Make room for "string start - 1" constant.
672
673 // Check if we have space on the stack for registers.
674 Label stack_limit_hit;
675 Label stack_ok;
676
677 ExternalReference stack_limit =
678 ExternalReference::address_of_stack_limit(isolate());
679 __ mov(ecx, esp);
680 __ sub(ecx, Operand::StaticVariable(stack_limit));
681 // Handle it if the stack pointer is already below the stack limit.
682 __ j(below_equal, &stack_limit_hit);
683 // Check if there is room for the variable number of registers above
684 // the stack limit.
685 __ cmp(ecx, num_registers_ * kPointerSize);
686 __ j(above_equal, &stack_ok);
687 // Exit with OutOfMemory exception. There is not enough space on the stack
688 // for our working registers.
689 __ mov(eax, EXCEPTION);
690 __ jmp(&return_eax);
691
692 __ bind(&stack_limit_hit);
693 CallCheckStackGuardState(ebx);
694 __ or_(eax, eax);
695 // If returned value is non-zero, we exit with the returned value as result.
696 __ j(not_zero, &return_eax);
697
698 __ bind(&stack_ok);
699 // Load start index for later use.
700 __ mov(ebx, Operand(ebp, kStartIndex));
701
702 // Allocate space on stack for registers.
703 __ sub(esp, Immediate(num_registers_ * kPointerSize));
704 // Load string length.
705 __ mov(esi, Operand(ebp, kInputEnd));
706 // Load input position.
707 __ mov(edi, Operand(ebp, kInputStart));
708 // Set up edi to be negative offset from string end.
709 __ sub(edi, esi);
710
711 // Set eax to address of char before start of the string.
712 // (effectively string position -1).
713 __ neg(ebx);
714 if (mode_ == UC16) {
715 __ lea(eax, Operand(edi, ebx, times_2, -char_size()));
716 } else {
717 __ lea(eax, Operand(edi, ebx, times_1, -char_size()));
718 }
719 // Store this value in a local variable, for use when clearing
720 // position registers.
721 __ mov(Operand(ebp, kStringStartMinusOne), eax);
722
723#if V8_OS_WIN
724 // Ensure that we write to each stack page, in order. Skipping a page
725 // on Windows can cause segmentation faults. Assuming page size is 4k.
726 const int kPageSize = 4096;
727 const int kRegistersPerPage = kPageSize / kPointerSize;
728 for (int i = num_saved_registers_ + kRegistersPerPage - 1;
729 i < num_registers_;
730 i += kRegistersPerPage) {
731 __ mov(register_location(i), eax); // One write every page.
732 }
733#endif // V8_OS_WIN
734
735 Label load_char_start_regexp, start_regexp;
736 // Load newline if index is at start, previous character otherwise.
737 __ cmp(Operand(ebp, kStartIndex), Immediate(0));
738 __ j(not_equal, &load_char_start_regexp, Label::kNear);
739 __ mov(current_character(), '\n');
740 __ jmp(&start_regexp, Label::kNear);
741
742 // Global regexp restarts matching here.
743 __ bind(&load_char_start_regexp);
744 // Load previous char as initial value of current character register.
745 LoadCurrentCharacterUnchecked(-1, 1);
746 __ bind(&start_regexp);
747
748 // Initialize on-stack registers.
749 if (num_saved_registers_ > 0) { // Always is, if generated from a regexp.
750 // Fill saved registers with initial value = start offset - 1
751 // Fill in stack push order, to avoid accessing across an unwritten
752 // page (a problem on Windows).
753 if (num_saved_registers_ > 8) {
754 __ mov(ecx, kRegisterZero);
755 Label init_loop;
756 __ bind(&init_loop);
757 __ mov(Operand(ebp, ecx, times_1, 0), eax);
758 __ sub(ecx, Immediate(kPointerSize));
759 __ cmp(ecx, kRegisterZero - num_saved_registers_ * kPointerSize);
760 __ j(greater, &init_loop);
761 } else { // Unroll the loop.
762 for (int i = 0; i < num_saved_registers_; i++) {
763 __ mov(register_location(i), eax);
764 }
765 }
766 }
767
768 // Initialize backtrack stack pointer.
769 __ mov(backtrack_stackpointer(), Operand(ebp, kStackHighEnd));
770
771 __ jmp(&start_label_);
772
773 // Exit code:
774 if (success_label_.is_linked()) {
775 // Save captures when successful.
776 __ bind(&success_label_);
777 if (num_saved_registers_ > 0) {
778 // copy captures to output
779 __ mov(ebx, Operand(ebp, kRegisterOutput));
780 __ mov(ecx, Operand(ebp, kInputEnd));
781 __ mov(edx, Operand(ebp, kStartIndex));
782 __ sub(ecx, Operand(ebp, kInputStart));
783 if (mode_ == UC16) {
784 __ lea(ecx, Operand(ecx, edx, times_2, 0));
785 } else {
786 __ add(ecx, edx);
787 }
788 for (int i = 0; i < num_saved_registers_; i++) {
789 __ mov(eax, register_location(i));
790 if (i == 0 && global_with_zero_length_check()) {
791 // Keep capture start in edx for the zero-length check later.
792 __ mov(edx, eax);
793 }
794 // Convert to index from start of string, not end.
795 __ add(eax, ecx);
796 if (mode_ == UC16) {
797 __ sar(eax, 1); // Convert byte index to character index.
798 }
799 __ mov(Operand(ebx, i * kPointerSize), eax);
800 }
801 }
802
803 if (global()) {
804 // Restart matching if the regular expression is flagged as global.
805 // Increment success counter.
806 __ inc(Operand(ebp, kSuccessfulCaptures));
807 // Capture results have been stored, so the number of remaining global
808 // output registers is reduced by the number of stored captures.
809 __ mov(ecx, Operand(ebp, kNumOutputRegisters));
810 __ sub(ecx, Immediate(num_saved_registers_));
811 // Check whether we have enough room for another set of capture results.
812 __ cmp(ecx, Immediate(num_saved_registers_));
813 __ j(less, &exit_label_);
814
815 __ mov(Operand(ebp, kNumOutputRegisters), ecx);
816 // Advance the location for output.
817 __ add(Operand(ebp, kRegisterOutput),
818 Immediate(num_saved_registers_ * kPointerSize));
819
820 // Prepare eax to initialize registers with its value in the next run.
821 __ mov(eax, Operand(ebp, kStringStartMinusOne));
822
823 if (global_with_zero_length_check()) {
824 // Special case for zero-length matches.
825 // edx: capture start index
826 __ cmp(edi, edx);
827 // Not a zero-length match, restart.
828 __ j(not_equal, &load_char_start_regexp);
829 // edi (offset from the end) is zero if we already reached the end.
830 __ test(edi, edi);
831 __ j(zero, &exit_label_, Label::kNear);
832 // Advance current position after a zero-length match.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100833 Label advance;
834 __ bind(&advance);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000835 if (mode_ == UC16) {
836 __ add(edi, Immediate(2));
837 } else {
838 __ inc(edi);
839 }
Ben Murdoch097c5b22016-05-18 11:27:45 +0100840 if (global_unicode()) CheckNotInSurrogatePair(0, &advance);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000841 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000842 __ jmp(&load_char_start_regexp);
843 } else {
844 __ mov(eax, Immediate(SUCCESS));
845 }
846 }
847
848 __ bind(&exit_label_);
849 if (global()) {
850 // Return the number of successful captures.
851 __ mov(eax, Operand(ebp, kSuccessfulCaptures));
852 }
853
854 __ bind(&return_eax);
855 // Skip esp past regexp registers.
856 __ lea(esp, Operand(ebp, kBackup_ebx));
857 // Restore callee-save registers.
858 __ pop(ebx);
859 __ pop(edi);
860 __ pop(esi);
861 // Exit function frame, restore previous one.
862 __ pop(ebp);
863 __ ret(0);
864
865 // Backtrack code (branch target for conditional backtracks).
866 if (backtrack_label_.is_linked()) {
867 __ bind(&backtrack_label_);
868 Backtrack();
869 }
870
871 Label exit_with_exception;
872
873 // Preempt-code
874 if (check_preempt_label_.is_linked()) {
875 SafeCallTarget(&check_preempt_label_);
876
877 __ push(backtrack_stackpointer());
878 __ push(edi);
879
880 CallCheckStackGuardState(ebx);
881 __ or_(eax, eax);
882 // If returning non-zero, we should end execution with the given
883 // result as return value.
884 __ j(not_zero, &return_eax);
885
886 __ pop(edi);
887 __ pop(backtrack_stackpointer());
888 // String might have moved: Reload esi from frame.
889 __ mov(esi, Operand(ebp, kInputEnd));
890 SafeReturn();
891 }
892
893 // Backtrack stack overflow code.
894 if (stack_overflow_label_.is_linked()) {
895 SafeCallTarget(&stack_overflow_label_);
896 // Reached if the backtrack-stack limit has been hit.
897
898 Label grow_failed;
899 // Save registers before calling C function
900 __ push(esi);
901 __ push(edi);
902
903 // Call GrowStack(backtrack_stackpointer())
904 static const int num_arguments = 3;
905 __ PrepareCallCFunction(num_arguments, ebx);
906 __ mov(Operand(esp, 2 * kPointerSize),
907 Immediate(ExternalReference::isolate_address(isolate())));
908 __ lea(eax, Operand(ebp, kStackHighEnd));
909 __ mov(Operand(esp, 1 * kPointerSize), eax);
910 __ mov(Operand(esp, 0 * kPointerSize), backtrack_stackpointer());
911 ExternalReference grow_stack =
912 ExternalReference::re_grow_stack(isolate());
913 __ CallCFunction(grow_stack, num_arguments);
914 // If return NULL, we have failed to grow the stack, and
915 // must exit with a stack-overflow exception.
916 __ or_(eax, eax);
917 __ j(equal, &exit_with_exception);
918 // Otherwise use return value as new stack pointer.
919 __ mov(backtrack_stackpointer(), eax);
920 // Restore saved registers and continue.
921 __ pop(edi);
922 __ pop(esi);
923 SafeReturn();
924 }
925
926 if (exit_with_exception.is_linked()) {
927 // If any of the code above needed to exit with an exception.
928 __ bind(&exit_with_exception);
929 // Exit with Result EXCEPTION(-1) to signal thrown exception.
930 __ mov(eax, EXCEPTION);
931 __ jmp(&return_eax);
932 }
933
934 CodeDesc code_desc;
935 masm_->GetCode(&code_desc);
936 Handle<Code> code =
937 isolate()->factory()->NewCode(code_desc,
938 Code::ComputeFlags(Code::REGEXP),
939 masm_->CodeObject());
Ben Murdochda12d292016-06-02 14:46:10 +0100940 PROFILE(masm_->isolate(),
941 RegExpCodeCreateEvent(AbstractCode::cast(*code), *source));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000942 return Handle<HeapObject>::cast(code);
943}
944
945
946void RegExpMacroAssemblerIA32::GoTo(Label* to) {
947 BranchOrBacktrack(no_condition, to);
948}
949
950
951void RegExpMacroAssemblerIA32::IfRegisterGE(int reg,
952 int comparand,
953 Label* if_ge) {
954 __ cmp(register_location(reg), Immediate(comparand));
955 BranchOrBacktrack(greater_equal, if_ge);
956}
957
958
959void RegExpMacroAssemblerIA32::IfRegisterLT(int reg,
960 int comparand,
961 Label* if_lt) {
962 __ cmp(register_location(reg), Immediate(comparand));
963 BranchOrBacktrack(less, if_lt);
964}
965
966
967void RegExpMacroAssemblerIA32::IfRegisterEqPos(int reg,
968 Label* if_eq) {
969 __ cmp(edi, register_location(reg));
970 BranchOrBacktrack(equal, if_eq);
971}
972
973
974RegExpMacroAssembler::IrregexpImplementation
975 RegExpMacroAssemblerIA32::Implementation() {
976 return kIA32Implementation;
977}
978
979
980void RegExpMacroAssemblerIA32::LoadCurrentCharacter(int cp_offset,
981 Label* on_end_of_input,
982 bool check_bounds,
983 int characters) {
984 DCHECK(cp_offset < (1<<30)); // Be sane! (And ensure negation works)
985 if (check_bounds) {
986 if (cp_offset >= 0) {
987 CheckPosition(cp_offset + characters - 1, on_end_of_input);
988 } else {
989 CheckPosition(cp_offset, on_end_of_input);
990 }
991 }
992 LoadCurrentCharacterUnchecked(cp_offset, characters);
993}
994
995
996void RegExpMacroAssemblerIA32::PopCurrentPosition() {
997 Pop(edi);
998}
999
1000
1001void RegExpMacroAssemblerIA32::PopRegister(int register_index) {
1002 Pop(eax);
1003 __ mov(register_location(register_index), eax);
1004}
1005
1006
1007void RegExpMacroAssemblerIA32::PushBacktrack(Label* label) {
1008 Push(Immediate::CodeRelativeOffset(label));
1009 CheckStackLimit();
1010}
1011
1012
1013void RegExpMacroAssemblerIA32::PushCurrentPosition() {
1014 Push(edi);
1015}
1016
1017
1018void RegExpMacroAssemblerIA32::PushRegister(int register_index,
1019 StackCheckFlag check_stack_limit) {
1020 __ mov(eax, register_location(register_index));
1021 Push(eax);
1022 if (check_stack_limit) CheckStackLimit();
1023}
1024
1025
1026void RegExpMacroAssemblerIA32::ReadCurrentPositionFromRegister(int reg) {
1027 __ mov(edi, register_location(reg));
1028}
1029
1030
1031void RegExpMacroAssemblerIA32::ReadStackPointerFromRegister(int reg) {
1032 __ mov(backtrack_stackpointer(), register_location(reg));
1033 __ add(backtrack_stackpointer(), Operand(ebp, kStackHighEnd));
1034}
1035
1036void RegExpMacroAssemblerIA32::SetCurrentPositionFromEnd(int by) {
1037 Label after_position;
1038 __ cmp(edi, -by * char_size());
1039 __ j(greater_equal, &after_position, Label::kNear);
1040 __ mov(edi, -by * char_size());
1041 // On RegExp code entry (where this operation is used), the character before
1042 // the current position is expected to be already loaded.
1043 // We have advanced the position, so it's safe to read backwards.
1044 LoadCurrentCharacterUnchecked(-1, 1);
1045 __ bind(&after_position);
1046}
1047
1048
1049void RegExpMacroAssemblerIA32::SetRegister(int register_index, int to) {
1050 DCHECK(register_index >= num_saved_registers_); // Reserved for positions!
1051 __ mov(register_location(register_index), Immediate(to));
1052}
1053
1054
1055bool RegExpMacroAssemblerIA32::Succeed() {
1056 __ jmp(&success_label_);
1057 return global();
1058}
1059
1060
1061void RegExpMacroAssemblerIA32::WriteCurrentPositionToRegister(int reg,
1062 int cp_offset) {
1063 if (cp_offset == 0) {
1064 __ mov(register_location(reg), edi);
1065 } else {
1066 __ lea(eax, Operand(edi, cp_offset * char_size()));
1067 __ mov(register_location(reg), eax);
1068 }
1069}
1070
1071
1072void RegExpMacroAssemblerIA32::ClearRegisters(int reg_from, int reg_to) {
1073 DCHECK(reg_from <= reg_to);
1074 __ mov(eax, Operand(ebp, kStringStartMinusOne));
1075 for (int reg = reg_from; reg <= reg_to; reg++) {
1076 __ mov(register_location(reg), eax);
1077 }
1078}
1079
1080
1081void RegExpMacroAssemblerIA32::WriteStackPointerToRegister(int reg) {
1082 __ mov(eax, backtrack_stackpointer());
1083 __ sub(eax, Operand(ebp, kStackHighEnd));
1084 __ mov(register_location(reg), eax);
1085}
1086
1087
1088// Private methods:
1089
1090void RegExpMacroAssemblerIA32::CallCheckStackGuardState(Register scratch) {
1091 static const int num_arguments = 3;
1092 __ PrepareCallCFunction(num_arguments, scratch);
1093 // RegExp code frame pointer.
1094 __ mov(Operand(esp, 2 * kPointerSize), ebp);
1095 // Code* of self.
1096 __ mov(Operand(esp, 1 * kPointerSize), Immediate(masm_->CodeObject()));
1097 // Next address on the stack (will be address of return address).
1098 __ lea(eax, Operand(esp, -kPointerSize));
1099 __ mov(Operand(esp, 0 * kPointerSize), eax);
1100 ExternalReference check_stack_guard =
1101 ExternalReference::re_check_stack_guard_state(isolate());
1102 __ CallCFunction(check_stack_guard, num_arguments);
1103}
1104
1105
1106// Helper function for reading a value out of a stack frame.
1107template <typename T>
1108static T& frame_entry(Address re_frame, int frame_offset) {
1109 return reinterpret_cast<T&>(Memory::int32_at(re_frame + frame_offset));
1110}
1111
1112
1113template <typename T>
1114static T* frame_entry_address(Address re_frame, int frame_offset) {
1115 return reinterpret_cast<T*>(re_frame + frame_offset);
1116}
1117
1118
1119int RegExpMacroAssemblerIA32::CheckStackGuardState(Address* return_address,
1120 Code* re_code,
1121 Address re_frame) {
1122 return NativeRegExpMacroAssembler::CheckStackGuardState(
1123 frame_entry<Isolate*>(re_frame, kIsolate),
1124 frame_entry<int>(re_frame, kStartIndex),
1125 frame_entry<int>(re_frame, kDirectCall) == 1, return_address, re_code,
1126 frame_entry_address<String*>(re_frame, kInputString),
1127 frame_entry_address<const byte*>(re_frame, kInputStart),
1128 frame_entry_address<const byte*>(re_frame, kInputEnd));
1129}
1130
1131
1132Operand RegExpMacroAssemblerIA32::register_location(int register_index) {
1133 DCHECK(register_index < (1<<30));
1134 if (num_registers_ <= register_index) {
1135 num_registers_ = register_index + 1;
1136 }
1137 return Operand(ebp, kRegisterZero - register_index * kPointerSize);
1138}
1139
1140
1141void RegExpMacroAssemblerIA32::CheckPosition(int cp_offset,
1142 Label* on_outside_input) {
1143 if (cp_offset >= 0) {
1144 __ cmp(edi, -cp_offset * char_size());
1145 BranchOrBacktrack(greater_equal, on_outside_input);
1146 } else {
1147 __ lea(eax, Operand(edi, cp_offset * char_size()));
1148 __ cmp(eax, Operand(ebp, kStringStartMinusOne));
1149 BranchOrBacktrack(less_equal, on_outside_input);
1150 }
1151}
1152
1153
1154void RegExpMacroAssemblerIA32::BranchOrBacktrack(Condition condition,
1155 Label* to) {
1156 if (condition < 0) { // No condition
1157 if (to == NULL) {
1158 Backtrack();
1159 return;
1160 }
1161 __ jmp(to);
1162 return;
1163 }
1164 if (to == NULL) {
1165 __ j(condition, &backtrack_label_);
1166 return;
1167 }
1168 __ j(condition, to);
1169}
1170
1171
1172void RegExpMacroAssemblerIA32::SafeCall(Label* to) {
1173 Label return_to;
1174 __ push(Immediate::CodeRelativeOffset(&return_to));
1175 __ jmp(to);
1176 __ bind(&return_to);
1177}
1178
1179
1180void RegExpMacroAssemblerIA32::SafeReturn() {
1181 __ pop(ebx);
1182 __ add(ebx, Immediate(masm_->CodeObject()));
1183 __ jmp(ebx);
1184}
1185
1186
1187void RegExpMacroAssemblerIA32::SafeCallTarget(Label* name) {
1188 __ bind(name);
1189}
1190
1191
1192void RegExpMacroAssemblerIA32::Push(Register source) {
1193 DCHECK(!source.is(backtrack_stackpointer()));
1194 // Notice: This updates flags, unlike normal Push.
1195 __ sub(backtrack_stackpointer(), Immediate(kPointerSize));
1196 __ mov(Operand(backtrack_stackpointer(), 0), source);
1197}
1198
1199
1200void RegExpMacroAssemblerIA32::Push(Immediate value) {
1201 // Notice: This updates flags, unlike normal Push.
1202 __ sub(backtrack_stackpointer(), Immediate(kPointerSize));
1203 __ mov(Operand(backtrack_stackpointer(), 0), value);
1204}
1205
1206
1207void RegExpMacroAssemblerIA32::Pop(Register target) {
1208 DCHECK(!target.is(backtrack_stackpointer()));
1209 __ mov(target, Operand(backtrack_stackpointer(), 0));
1210 // Notice: This updates flags, unlike normal Pop.
1211 __ add(backtrack_stackpointer(), Immediate(kPointerSize));
1212}
1213
1214
1215void RegExpMacroAssemblerIA32::CheckPreemption() {
1216 // Check for preemption.
1217 Label no_preempt;
1218 ExternalReference stack_limit =
1219 ExternalReference::address_of_stack_limit(isolate());
1220 __ cmp(esp, Operand::StaticVariable(stack_limit));
1221 __ j(above, &no_preempt);
1222
1223 SafeCall(&check_preempt_label_);
1224
1225 __ bind(&no_preempt);
1226}
1227
1228
1229void RegExpMacroAssemblerIA32::CheckStackLimit() {
1230 Label no_stack_overflow;
1231 ExternalReference stack_limit =
1232 ExternalReference::address_of_regexp_stack_limit(isolate());
1233 __ cmp(backtrack_stackpointer(), Operand::StaticVariable(stack_limit));
1234 __ j(above, &no_stack_overflow);
1235
1236 SafeCall(&stack_overflow_label_);
1237
1238 __ bind(&no_stack_overflow);
1239}
1240
1241
1242void RegExpMacroAssemblerIA32::LoadCurrentCharacterUnchecked(int cp_offset,
1243 int characters) {
1244 if (mode_ == LATIN1) {
1245 if (characters == 4) {
1246 __ mov(current_character(), Operand(esi, edi, times_1, cp_offset));
1247 } else if (characters == 2) {
1248 __ movzx_w(current_character(), Operand(esi, edi, times_1, cp_offset));
1249 } else {
1250 DCHECK(characters == 1);
1251 __ movzx_b(current_character(), Operand(esi, edi, times_1, cp_offset));
1252 }
1253 } else {
1254 DCHECK(mode_ == UC16);
1255 if (characters == 2) {
1256 __ mov(current_character(),
1257 Operand(esi, edi, times_1, cp_offset * sizeof(uc16)));
1258 } else {
1259 DCHECK(characters == 1);
1260 __ movzx_w(current_character(),
1261 Operand(esi, edi, times_1, cp_offset * sizeof(uc16)));
1262 }
1263 }
1264}
1265
1266
1267#undef __
1268
1269#endif // V8_INTERPRETED_REGEXP
1270
1271} // namespace internal
1272} // namespace v8
1273
1274#endif // V8_TARGET_ARCH_IA32