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