Line data Source code
1 : // Copyright 2012 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #if V8_TARGET_ARCH_X64
6 :
7 : #include "src/regexp/x64/regexp-macro-assembler-x64.h"
8 :
9 : #include "src/factory.h"
10 : #include "src/log.h"
11 : #include "src/macro-assembler.h"
12 : #include "src/objects-inl.h"
13 : #include "src/regexp/regexp-macro-assembler.h"
14 : #include "src/regexp/regexp-stack.h"
15 : #include "src/unicode.h"
16 :
17 : namespace v8 {
18 : namespace internal {
19 :
20 : #ifndef V8_INTERPRETED_REGEXP
21 :
22 : /*
23 : * This assembler uses the following register assignment convention
24 : * - rdx : Currently loaded character(s) as Latin1 or UC16. Must be loaded
25 : * using LoadCurrentCharacter before using any of the dispatch methods.
26 : * Temporarily stores the index of capture start after a matching pass
27 : * for a global regexp.
28 : * - rdi : Current position in input, as negative offset from end of string.
29 : * Please notice that this is the byte offset, not the character
30 : * offset! Is always a 32-bit signed (negative) offset, but must be
31 : * maintained sign-extended to 64 bits, since it is used as index.
32 : * - rsi : End of input (points to byte after last character in input),
33 : * so that rsi+rdi points to the current character.
34 : * - rbp : Frame pointer. Used to access arguments, local variables and
35 : * RegExp registers.
36 : * - rsp : Points to tip of C stack.
37 : * - rcx : Points to tip of backtrack stack. The backtrack stack contains
38 : * only 32-bit values. Most are offsets from some base (e.g., character
39 : * positions from end of string or code location from Code* pointer).
40 : * - r8 : Code object pointer. Used to convert between absolute and
41 : * code-object-relative addresses.
42 : *
43 : * The registers rax, rbx, r9 and r11 are free to use for computations.
44 : * If changed to use r12+, they should be saved as callee-save registers.
45 : * The macro assembler special register r13 (kRootRegister) isn't special
46 : * during execution of RegExp code (it doesn't hold the value assumed when
47 : * creating JS code), so Root related macro operations can be used.
48 : *
49 : * Each call to a C++ method should retain these registers.
50 : *
51 : * The stack will have the following content, in some order, indexable from the
52 : * frame pointer (see, e.g., kStackHighEnd):
53 : * - Isolate* isolate (address of the current isolate)
54 : * - direct_call (if 1, direct call from JavaScript code, if 0 call
55 : * through the runtime system)
56 : * - stack_area_base (high end of the memory area to use as
57 : * backtracking stack)
58 : * - capture array size (may fit multiple sets of matches)
59 : * - int* capture_array (int[num_saved_registers_], for output).
60 : * - end of input (address of end of string)
61 : * - start of input (address of first character in string)
62 : * - start index (character index of start)
63 : * - String* input_string (input string)
64 : * - return address
65 : * - backup of callee save registers (rbx, possibly rsi and rdi).
66 : * - success counter (only useful for global regexp to count matches)
67 : * - Offset of location before start of input (effectively character
68 : * string start - 1). Used to initialize capture registers to a
69 : * non-position.
70 : * - At start of string (if 1, we are starting at the start of the
71 : * string, otherwise 0)
72 : * - register 0 rbp[-n] (Only positions must be stored in the first
73 : * - register 1 rbp[-n-8] num_saved_registers_ registers)
74 : * - ...
75 : *
76 : * The first num_saved_registers_ registers are initialized to point to
77 : * "character -1" in the string (i.e., char_size() bytes before the first
78 : * character of the string). The remaining registers starts out uninitialized.
79 : *
80 : * The first seven values must be provided by the calling code by
81 : * calling the code's entry address cast to a function pointer with the
82 : * following signature:
83 : * int (*match)(String* input_string,
84 : * int start_index,
85 : * Address start,
86 : * Address end,
87 : * int* capture_output_array,
88 : * int num_capture_registers,
89 : * byte* stack_area_base,
90 : * bool direct_call = false,
91 : * Isolate* isolate);
92 : */
93 :
94 : #define __ ACCESS_MASM((&masm_))
95 :
96 93584 : RegExpMacroAssemblerX64::RegExpMacroAssemblerX64(Isolate* isolate, Zone* zone,
97 : Mode mode,
98 : int registers_to_save)
99 : : NativeRegExpMacroAssembler(isolate, zone),
100 : masm_(isolate, nullptr, kRegExpCodeSize, CodeObjectRequired::kYes),
101 : no_root_array_scope_(&masm_),
102 : code_relative_fixup_positions_(4, zone),
103 : mode_(mode),
104 : num_registers_(registers_to_save),
105 : num_saved_registers_(registers_to_save),
106 : entry_label_(),
107 : start_label_(),
108 : success_label_(),
109 : backtrack_label_(),
110 187168 : exit_label_() {
111 : DCHECK_EQ(0, registers_to_save % 2);
112 93584 : __ jmp(&entry_label_); // We'll write the entry code when we know more.
113 93584 : __ bind(&start_label_); // And then continue from here.
114 93584 : }
115 :
116 :
117 187168 : RegExpMacroAssemblerX64::~RegExpMacroAssemblerX64() {
118 : // Unuse labels in case we throw away the assembler without calling GetCode.
119 : entry_label_.Unuse();
120 : start_label_.Unuse();
121 : success_label_.Unuse();
122 : backtrack_label_.Unuse();
123 : exit_label_.Unuse();
124 : check_preempt_label_.Unuse();
125 : stack_overflow_label_.Unuse();
126 93584 : }
127 :
128 :
129 535199 : int RegExpMacroAssemblerX64::stack_limit_slack() {
130 535199 : return RegExpStack::kStackLimitSlack;
131 : }
132 :
133 :
134 1089066 : void RegExpMacroAssemblerX64::AdvanceCurrentPosition(int by) {
135 544533 : if (by != 0) {
136 1089066 : __ addq(rdi, Immediate(by * char_size()));
137 : }
138 544533 : }
139 :
140 :
141 6922 : void RegExpMacroAssemblerX64::AdvanceRegister(int reg, int by) {
142 : DCHECK_LE(0, reg);
143 : DCHECK_GT(num_registers_, reg);
144 6922 : if (by != 0) {
145 6922 : __ addp(register_location(reg), Immediate(by));
146 : }
147 6922 : }
148 :
149 :
150 232102 : void RegExpMacroAssemblerX64::Backtrack() {
151 232102 : CheckPreemption();
152 : // Pop Code* offset from backtrack stack, add Code* and jump to location.
153 232102 : Pop(rbx);
154 232102 : __ addp(rbx, code_object_pointer());
155 232102 : __ jmp(rbx);
156 232102 : }
157 :
158 :
159 2629015 : void RegExpMacroAssemblerX64::Bind(Label* label) {
160 2629015 : __ bind(label);
161 2629015 : }
162 :
163 :
164 155337 : void RegExpMacroAssemblerX64::CheckCharacter(uint32_t c, Label* on_equal) {
165 310674 : __ cmpl(current_character(), Immediate(c));
166 155337 : BranchOrBacktrack(equal, on_equal);
167 155337 : }
168 :
169 :
170 19819 : void RegExpMacroAssemblerX64::CheckCharacterGT(uc16 limit, Label* on_greater) {
171 39638 : __ cmpl(current_character(), Immediate(limit));
172 19819 : BranchOrBacktrack(greater, on_greater);
173 19819 : }
174 :
175 :
176 940 : void RegExpMacroAssemblerX64::CheckAtStart(Label* on_at_start) {
177 1880 : __ leap(rax, Operand(rdi, -char_size()));
178 1880 : __ cmpp(rax, Operand(rbp, kStringStartMinusOne));
179 940 : BranchOrBacktrack(equal, on_at_start);
180 940 : }
181 :
182 :
183 4283 : void RegExpMacroAssemblerX64::CheckNotAtStart(int cp_offset,
184 4283 : Label* on_not_at_start) {
185 8566 : __ leap(rax, Operand(rdi, -char_size() + cp_offset * char_size()));
186 8566 : __ cmpp(rax, Operand(rbp, kStringStartMinusOne));
187 4283 : BranchOrBacktrack(not_equal, on_not_at_start);
188 4283 : }
189 :
190 :
191 16868 : void RegExpMacroAssemblerX64::CheckCharacterLT(uc16 limit, Label* on_less) {
192 33736 : __ cmpl(current_character(), Immediate(limit));
193 16868 : BranchOrBacktrack(less, on_less);
194 16868 : }
195 :
196 :
197 14815 : void RegExpMacroAssemblerX64::CheckGreedyLoop(Label* on_equal) {
198 : Label fallthrough;
199 29630 : __ cmpl(rdi, Operand(backtrack_stackpointer(), 0));
200 14815 : __ j(not_equal, &fallthrough);
201 : Drop();
202 14815 : BranchOrBacktrack(no_condition, on_equal);
203 14815 : __ bind(&fallthrough);
204 14815 : }
205 :
206 :
207 1949 : void RegExpMacroAssemblerX64::CheckNotBackReferenceIgnoreCase(
208 : int start_reg, bool read_backward, bool unicode, Label* on_no_match) {
209 : Label fallthrough;
210 1949 : ReadPositionFromRegister(rdx, start_reg); // Offset of start of capture
211 1949 : ReadPositionFromRegister(rbx, start_reg + 1); // Offset of end of capture
212 1949 : __ subp(rbx, rdx); // Length of capture.
213 :
214 : // -----------------------
215 : // rdx = Start offset of capture.
216 : // rbx = Length of capture
217 :
218 : // At this point, the capture registers are either both set or both cleared.
219 : // If the capture length is zero, then the capture is either empty or cleared.
220 : // Fall through in both cases.
221 1949 : __ j(equal, &fallthrough);
222 :
223 : // -----------------------
224 : // rdx - Start of capture
225 : // rbx - length of capture
226 : // Check that there are sufficient characters left in the input.
227 1949 : if (read_backward) {
228 130 : __ movl(rax, Operand(rbp, kStringStartMinusOne));
229 65 : __ addl(rax, rbx);
230 65 : __ cmpl(rdi, rax);
231 65 : BranchOrBacktrack(less_equal, on_no_match);
232 : } else {
233 : __ movl(rax, rdi);
234 1884 : __ addl(rax, rbx);
235 1884 : BranchOrBacktrack(greater, on_no_match);
236 : }
237 :
238 1949 : if (mode_ == LATIN1) {
239 : Label loop_increment;
240 1684 : if (on_no_match == nullptr) {
241 1660 : on_no_match = &backtrack_label_;
242 : }
243 :
244 3368 : __ leap(r9, Operand(rsi, rdx, times_1, 0));
245 3368 : __ leap(r11, Operand(rsi, rdi, times_1, 0));
246 1684 : if (read_backward) {
247 60 : __ subp(r11, rbx); // Offset by length when matching backwards.
248 : }
249 1684 : __ addp(rbx, r9); // End of capture
250 : // ---------------------
251 : // r11 - current input character address
252 : // r9 - current capture character address
253 : // rbx - end of capture
254 :
255 : Label loop;
256 1684 : __ bind(&loop);
257 3368 : __ movzxbl(rdx, Operand(r9, 0));
258 3368 : __ movzxbl(rax, Operand(r11, 0));
259 : // al - input character
260 : // dl - capture character
261 : __ cmpb(rax, rdx);
262 1684 : __ j(equal, &loop_increment);
263 :
264 : // Mismatch, try case-insensitive match (converting letters to lower-case).
265 : // I.e., if or-ing with 0x20 makes values equal and in range 'a'-'z', it's
266 : // a match.
267 1684 : __ orp(rax, Immediate(0x20)); // Convert match character to lower-case.
268 1684 : __ orp(rdx, Immediate(0x20)); // Convert capture character to lower-case.
269 : __ cmpb(rax, rdx);
270 1684 : __ j(not_equal, on_no_match); // Definitely not equal.
271 : __ subb(rax, Immediate('a'));
272 : __ cmpb(rax, Immediate('z' - 'a'));
273 1684 : __ j(below_equal, &loop_increment); // In range 'a'-'z'.
274 : // Latin-1: Check for values in range [224,254] but not 247.
275 : __ subb(rax, Immediate(224 - 'a'));
276 : __ cmpb(rax, Immediate(254 - 224));
277 1684 : __ j(above, on_no_match); // Weren't Latin-1 letters.
278 : __ cmpb(rax, Immediate(247 - 224)); // Check for 247.
279 1684 : __ j(equal, on_no_match);
280 1684 : __ bind(&loop_increment);
281 : // Increment pointers into match and capture strings.
282 1684 : __ addp(r11, Immediate(1));
283 1684 : __ addp(r9, Immediate(1));
284 : // Compare to end of capture, and loop if not done.
285 1684 : __ cmpp(r9, rbx);
286 1684 : __ j(below, &loop);
287 :
288 : // Compute new value of character position after the matched part.
289 : __ movp(rdi, r11);
290 1684 : __ subq(rdi, rsi);
291 1684 : if (read_backward) {
292 : // Subtract match length if we matched backward.
293 60 : __ addq(rdi, register_location(start_reg));
294 60 : __ subq(rdi, register_location(start_reg + 1));
295 : }
296 : } else {
297 : DCHECK(mode_ == UC16);
298 : // Save important/volatile registers before calling C function.
299 : #ifndef _WIN64
300 : // Caller save on Linux and callee save in Windows.
301 265 : __ pushq(rsi);
302 265 : __ pushq(rdi);
303 : #endif
304 265 : __ pushq(backtrack_stackpointer());
305 :
306 : static const int num_arguments = 4;
307 265 : __ PrepareCallCFunction(num_arguments);
308 :
309 : // Put arguments into parameter registers. Parameters are
310 : // Address byte_offset1 - Address captured substring's start.
311 : // Address byte_offset2 - Address of current character position.
312 : // size_t byte_length - length of capture in bytes(!)
313 : // Isolate* isolate or 0 if unicode flag.
314 : #ifdef _WIN64
315 : DCHECK(rcx == arg_reg_1);
316 : DCHECK(rdx == arg_reg_2);
317 : // Compute and set byte_offset1 (start of capture).
318 : __ leap(rcx, Operand(rsi, rdx, times_1, 0));
319 : // Set byte_offset2.
320 : __ leap(rdx, Operand(rsi, rdi, times_1, 0));
321 : if (read_backward) {
322 : __ subq(rdx, rbx);
323 : }
324 : #else // AMD64 calling convention
325 : DCHECK(rdi == arg_reg_1);
326 : DCHECK(rsi == arg_reg_2);
327 : // Compute byte_offset2 (current position = rsi+rdi).
328 530 : __ leap(rax, Operand(rsi, rdi, times_1, 0));
329 : // Compute and set byte_offset1 (start of capture).
330 530 : __ leap(rdi, Operand(rsi, rdx, times_1, 0));
331 : // Set byte_offset2.
332 : __ movp(rsi, rax);
333 265 : if (read_backward) {
334 5 : __ subq(rsi, rbx);
335 : }
336 : #endif // _WIN64
337 :
338 : // Set byte_length.
339 : __ movp(arg_reg_3, rbx);
340 : // Isolate.
341 : #ifdef V8_INTL_SUPPORT
342 265 : if (unicode) {
343 : __ movp(arg_reg_4, Immediate(0));
344 : } else // NOLINT
345 : #endif // V8_INTL_SUPPORT
346 : {
347 250 : __ LoadAddress(arg_reg_4, ExternalReference::isolate_address(isolate()));
348 : }
349 :
350 : { // NOLINT: Can't find a way to open this scope without confusing the
351 : // linter.
352 265 : AllowExternalCallThatCantCauseGC scope(&masm_);
353 : ExternalReference compare =
354 265 : ExternalReference::re_case_insensitive_compare_uc16(isolate());
355 265 : __ CallCFunction(compare, num_arguments);
356 : }
357 :
358 : // Restore original values before reacting on result value.
359 265 : __ Move(code_object_pointer(), masm_.CodeObject());
360 265 : __ popq(backtrack_stackpointer());
361 : #ifndef _WIN64
362 265 : __ popq(rdi);
363 265 : __ popq(rsi);
364 : #endif
365 :
366 : // Check if function returned non-zero for success or zero for failure.
367 : __ testp(rax, rax);
368 265 : BranchOrBacktrack(zero, on_no_match);
369 : // On success, advance position by length of capture.
370 : // Requires that rbx is callee save (true for both Win64 and AMD64 ABIs).
371 265 : if (read_backward) {
372 5 : __ subq(rdi, rbx);
373 : } else {
374 260 : __ addq(rdi, rbx);
375 : }
376 : }
377 1949 : __ bind(&fallthrough);
378 1949 : }
379 :
380 :
381 654 : void RegExpMacroAssemblerX64::CheckNotBackReference(int start_reg,
382 : bool read_backward,
383 1308 : Label* on_no_match) {
384 : Label fallthrough;
385 :
386 : // Find length of back-referenced capture.
387 654 : ReadPositionFromRegister(rdx, start_reg); // Offset of start of capture
388 654 : ReadPositionFromRegister(rax, start_reg + 1); // Offset of end of capture
389 654 : __ subp(rax, rdx); // Length to check.
390 :
391 : // At this point, the capture registers are either both set or both cleared.
392 : // If the capture length is zero, then the capture is either empty or cleared.
393 : // Fall through in both cases.
394 654 : __ j(equal, &fallthrough);
395 :
396 : // -----------------------
397 : // rdx - Start of capture
398 : // rax - length of capture
399 : // Check that there are sufficient characters left in the input.
400 654 : if (read_backward) {
401 240 : __ movl(rbx, Operand(rbp, kStringStartMinusOne));
402 120 : __ addl(rbx, rax);
403 120 : __ cmpl(rdi, rbx);
404 120 : BranchOrBacktrack(less_equal, on_no_match);
405 : } else {
406 : __ movl(rbx, rdi);
407 534 : __ addl(rbx, rax);
408 534 : BranchOrBacktrack(greater, on_no_match);
409 : }
410 :
411 : // Compute pointers to match string and capture string
412 1308 : __ leap(rbx, Operand(rsi, rdi, times_1, 0)); // Start of match.
413 654 : if (read_backward) {
414 120 : __ subq(rbx, rax); // Offset by length when matching backwards.
415 : }
416 654 : __ addp(rdx, rsi); // Start of capture.
417 1308 : __ leap(r9, Operand(rdx, rax, times_1, 0)); // End of capture
418 :
419 : // -----------------------
420 : // rbx - current capture character address.
421 : // rbx - current input character address .
422 : // r9 - end of input to match (capture length after rbx).
423 :
424 : Label loop;
425 654 : __ bind(&loop);
426 654 : if (mode_ == LATIN1) {
427 1154 : __ movzxbl(rax, Operand(rdx, 0));
428 1154 : __ cmpb(rax, Operand(rbx, 0));
429 : } else {
430 : DCHECK(mode_ == UC16);
431 154 : __ movzxwl(rax, Operand(rdx, 0));
432 154 : __ cmpw(rax, Operand(rbx, 0));
433 : }
434 654 : BranchOrBacktrack(not_equal, on_no_match);
435 : // Increment pointers into capture and match string.
436 654 : __ addp(rbx, Immediate(char_size()));
437 654 : __ addp(rdx, Immediate(char_size()));
438 : // Check if we have reached end of match area.
439 654 : __ cmpp(rdx, r9);
440 654 : __ j(below, &loop);
441 :
442 : // Success.
443 : // Set current character position to position after match.
444 : __ movp(rdi, rbx);
445 654 : __ subq(rdi, rsi);
446 654 : if (read_backward) {
447 : // Subtract match length if we matched backward.
448 120 : __ addq(rdi, register_location(start_reg));
449 120 : __ subq(rdi, register_location(start_reg + 1));
450 : }
451 :
452 654 : __ bind(&fallthrough);
453 654 : }
454 :
455 :
456 633413 : void RegExpMacroAssemblerX64::CheckNotCharacter(uint32_t c,
457 : Label* on_not_equal) {
458 1266826 : __ cmpl(current_character(), Immediate(c));
459 633413 : BranchOrBacktrack(not_equal, on_not_equal);
460 633413 : }
461 :
462 :
463 57131 : void RegExpMacroAssemblerX64::CheckCharacterAfterAnd(uint32_t c,
464 : uint32_t mask,
465 : Label* on_equal) {
466 57131 : if (c == 0) {
467 1700 : __ testl(current_character(), Immediate(mask));
468 : } else {
469 55431 : __ movl(rax, Immediate(mask));
470 55431 : __ andp(rax, current_character());
471 110862 : __ cmpl(rax, Immediate(c));
472 : }
473 57131 : BranchOrBacktrack(equal, on_equal);
474 57131 : }
475 :
476 :
477 18530 : void RegExpMacroAssemblerX64::CheckNotCharacterAfterAnd(uint32_t c,
478 : uint32_t mask,
479 : Label* on_not_equal) {
480 18530 : if (c == 0) {
481 602 : __ testl(current_character(), Immediate(mask));
482 : } else {
483 17928 : __ movl(rax, Immediate(mask));
484 17928 : __ andp(rax, current_character());
485 35856 : __ cmpl(rax, Immediate(c));
486 : }
487 18530 : BranchOrBacktrack(not_equal, on_not_equal);
488 18530 : }
489 :
490 :
491 87 : void RegExpMacroAssemblerX64::CheckNotCharacterAfterMinusAnd(
492 : uc16 c,
493 : uc16 minus,
494 : uc16 mask,
495 : Label* on_not_equal) {
496 : DCHECK_GT(String::kMaxUtf16CodeUnit, minus);
497 174 : __ leap(rax, Operand(current_character(), -minus));
498 174 : __ andp(rax, Immediate(mask));
499 174 : __ cmpl(rax, Immediate(c));
500 87 : BranchOrBacktrack(not_equal, on_not_equal);
501 87 : }
502 :
503 :
504 25435 : void RegExpMacroAssemblerX64::CheckCharacterInRange(
505 : uc16 from,
506 : uc16 to,
507 : Label* on_in_range) {
508 50870 : __ leal(rax, Operand(current_character(), -from));
509 50870 : __ cmpl(rax, Immediate(to - from));
510 25435 : BranchOrBacktrack(below_equal, on_in_range);
511 25435 : }
512 :
513 :
514 102601 : void RegExpMacroAssemblerX64::CheckCharacterNotInRange(
515 : uc16 from,
516 : uc16 to,
517 : Label* on_not_in_range) {
518 205202 : __ leal(rax, Operand(current_character(), -from));
519 205202 : __ cmpl(rax, Immediate(to - from));
520 102601 : BranchOrBacktrack(above, on_not_in_range);
521 102601 : }
522 :
523 :
524 14736 : void RegExpMacroAssemblerX64::CheckBitInTable(
525 : Handle<ByteArray> table,
526 : Label* on_bit_set) {
527 14736 : __ Move(rax, table);
528 : Register index = current_character();
529 : if (mode_ != LATIN1 || kTableMask != String::kMaxOneByteCharCode) {
530 14736 : __ movp(rbx, current_character());
531 14736 : __ andp(rbx, Immediate(kTableMask));
532 : index = rbx;
533 : }
534 : __ cmpb(FieldOperand(rax, index, times_1, ByteArray::kHeaderSize),
535 29472 : Immediate(0));
536 14736 : BranchOrBacktrack(not_equal, on_bit_set);
537 14736 : }
538 :
539 :
540 17542 : bool RegExpMacroAssemblerX64::CheckSpecialCharacterClass(uc16 type,
541 : Label* on_no_match) {
542 : // Range checks (c in min..max) are generally implemented by an unsigned
543 : // (c - min) <= (max - min) check, using the sequence:
544 : // leap(rax, Operand(current_character(), -min)) or sub(rax, Immediate(min))
545 : // cmp(rax, Immediate(max - min))
546 17542 : switch (type) {
547 : case 's':
548 : // Match space-characters
549 2676 : if (mode_ == LATIN1) {
550 : // One byte space characters are '\t'..'\r', ' ' and \u00a0.
551 : Label success;
552 2592 : __ cmpl(current_character(), Immediate(' '));
553 2592 : __ j(equal, &success, Label::kNear);
554 : // Check range 0x09..0x0d
555 5184 : __ leap(rax, Operand(current_character(), -'\t'));
556 2592 : __ cmpl(rax, Immediate('\r' - '\t'));
557 2592 : __ j(below_equal, &success, Label::kNear);
558 : // \u00a0 (NBSP).
559 2592 : __ cmpl(rax, Immediate(0x00a0 - '\t'));
560 2592 : BranchOrBacktrack(not_equal, on_no_match);
561 2592 : __ bind(&success);
562 : return true;
563 : }
564 : return false;
565 : case 'S':
566 : // The emitted code for generic character classes is good enough.
567 : return false;
568 : case 'd':
569 : // Match ASCII digits ('0'..'9')
570 0 : __ leap(rax, Operand(current_character(), -'0'));
571 0 : __ cmpl(rax, Immediate('9' - '0'));
572 0 : BranchOrBacktrack(above, on_no_match);
573 0 : return true;
574 : case 'D':
575 : // Match non ASCII-digits
576 0 : __ leap(rax, Operand(current_character(), -'0'));
577 0 : __ cmpl(rax, Immediate('9' - '0'));
578 0 : BranchOrBacktrack(below_equal, on_no_match);
579 0 : return true;
580 : case '.': {
581 : // Match non-newlines (not 0x0a('\n'), 0x0d('\r'), 0x2028 and 0x2029)
582 8714 : __ movl(rax, current_character());
583 8714 : __ xorp(rax, Immediate(0x01));
584 : // See if current character is '\n'^1 or '\r'^1, i.e., 0x0b or 0x0c
585 8714 : __ subl(rax, Immediate(0x0b));
586 8714 : __ cmpl(rax, Immediate(0x0c - 0x0b));
587 8714 : BranchOrBacktrack(below_equal, on_no_match);
588 8714 : if (mode_ == UC16) {
589 : // Compare original value to 0x2028 and 0x2029, using the already
590 : // computed (current_char ^ 0x01 - 0x0b). I.e., check for
591 : // 0x201d (0x2028 - 0x0b) or 0x201e.
592 409 : __ subl(rax, Immediate(0x2028 - 0x0b));
593 409 : __ cmpl(rax, Immediate(0x2029 - 0x2028));
594 409 : BranchOrBacktrack(below_equal, on_no_match);
595 : }
596 : return true;
597 : }
598 : case 'n': {
599 : // Match newlines (0x0a('\n'), 0x0d('\r'), 0x2028 and 0x2029)
600 865 : __ movl(rax, current_character());
601 865 : __ xorp(rax, Immediate(0x01));
602 : // See if current character is '\n'^1 or '\r'^1, i.e., 0x0b or 0x0c
603 865 : __ subl(rax, Immediate(0x0b));
604 865 : __ cmpl(rax, Immediate(0x0c - 0x0b));
605 865 : if (mode_ == LATIN1) {
606 836 : BranchOrBacktrack(above, on_no_match);
607 : } else {
608 : Label done;
609 29 : BranchOrBacktrack(below_equal, &done);
610 : // Compare original value to 0x2028 and 0x2029, using the already
611 : // computed (current_char ^ 0x01 - 0x0b). I.e., check for
612 : // 0x201d (0x2028 - 0x0b) or 0x201e.
613 29 : __ subl(rax, Immediate(0x2028 - 0x0b));
614 29 : __ cmpl(rax, Immediate(0x2029 - 0x2028));
615 29 : BranchOrBacktrack(above, on_no_match);
616 29 : __ bind(&done);
617 : }
618 : return true;
619 : }
620 : case 'w': {
621 4503 : if (mode_ != LATIN1) {
622 : // Table is 256 entries, so all Latin1 characters can be tested.
623 46 : __ cmpl(current_character(), Immediate('z'));
624 46 : BranchOrBacktrack(above, on_no_match);
625 : }
626 4503 : __ Move(rbx, ExternalReference::re_word_character_map());
627 : DCHECK_EQ(0, word_character_map[0]); // Character '\0' is not a word char.
628 : __ testb(Operand(rbx, current_character(), times_1, 0),
629 4503 : current_character());
630 4503 : BranchOrBacktrack(zero, on_no_match);
631 4503 : return true;
632 : }
633 : case 'W': {
634 : Label done;
635 544 : if (mode_ != LATIN1) {
636 : // Table is 256 entries, so all Latin1 characters can be tested.
637 74 : __ cmpl(current_character(), Immediate('z'));
638 74 : __ j(above, &done);
639 : }
640 544 : __ Move(rbx, ExternalReference::re_word_character_map());
641 : DCHECK_EQ(0, word_character_map[0]); // Character '\0' is not a word char.
642 : __ testb(Operand(rbx, current_character(), times_1, 0),
643 544 : current_character());
644 544 : BranchOrBacktrack(not_zero, on_no_match);
645 544 : if (mode_ != LATIN1) {
646 74 : __ bind(&done);
647 : }
648 : return true;
649 : }
650 :
651 : case '*':
652 : // Match any character.
653 0 : return true;
654 : // No custom implementation (yet): s(UC16), S(UC16).
655 : default:
656 : return false;
657 : }
658 : }
659 :
660 :
661 93590 : void RegExpMacroAssemblerX64::Fail() {
662 : STATIC_ASSERT(FAILURE == 0); // Return value for failure is zero.
663 93590 : if (!global()) {
664 86434 : __ Set(rax, FAILURE);
665 : }
666 93590 : __ jmp(&exit_label_);
667 93590 : }
668 :
669 :
670 93584 : Handle<HeapObject> RegExpMacroAssemblerX64::GetCode(Handle<String> source) {
671 : Label return_rax;
672 : // Finalize code - write the entry point code now we know how many
673 : // registers we need.
674 : // Entry code:
675 93584 : __ bind(&entry_label_);
676 :
677 : // Tell the system that we have a stack frame. Because the type is MANUAL, no
678 : // is generated.
679 187168 : FrameScope scope(&masm_, StackFrame::MANUAL);
680 :
681 : // Actually emit code to start a new stack frame.
682 93584 : __ pushq(rbp);
683 : __ movp(rbp, rsp);
684 : // Save parameters and callee-save registers. Order here should correspond
685 : // to order of kBackup_ebx etc.
686 : #ifdef _WIN64
687 : // MSVC passes arguments in rcx, rdx, r8, r9, with backing stack slots.
688 : // Store register parameters in pre-allocated stack slots,
689 : __ movq(Operand(rbp, kInputString), rcx);
690 : __ movq(Operand(rbp, kStartIndex), rdx); // Passed as int32 in edx.
691 : __ movq(Operand(rbp, kInputStart), r8);
692 : __ movq(Operand(rbp, kInputEnd), r9);
693 : // Callee-save on Win64.
694 : __ pushq(rsi);
695 : __ pushq(rdi);
696 : __ pushq(rbx);
697 : #else
698 : // GCC passes arguments in rdi, rsi, rdx, rcx, r8, r9 (and then on stack).
699 : // Push register parameters on stack for reference.
700 : DCHECK_EQ(kInputString, -1 * kRegisterSize);
701 : DCHECK_EQ(kStartIndex, -2 * kRegisterSize);
702 : DCHECK_EQ(kInputStart, -3 * kRegisterSize);
703 : DCHECK_EQ(kInputEnd, -4 * kRegisterSize);
704 : DCHECK_EQ(kRegisterOutput, -5 * kRegisterSize);
705 : DCHECK_EQ(kNumOutputRegisters, -6 * kRegisterSize);
706 93584 : __ pushq(rdi);
707 93584 : __ pushq(rsi);
708 93584 : __ pushq(rdx);
709 93584 : __ pushq(rcx);
710 93584 : __ pushq(r8);
711 93584 : __ pushq(r9);
712 :
713 93584 : __ pushq(rbx); // Callee-save
714 : #endif
715 :
716 93584 : __ Push(Immediate(0)); // Number of successful matches in a global regexp.
717 93584 : __ Push(Immediate(0)); // Make room for "string start - 1" constant.
718 :
719 : // Check if we have space on the stack for registers.
720 : Label stack_limit_hit;
721 : Label stack_ok;
722 :
723 : ExternalReference stack_limit =
724 93584 : ExternalReference::address_of_stack_limit(isolate());
725 : __ movp(rcx, rsp);
726 : __ Move(kScratchRegister, stack_limit);
727 187168 : __ subp(rcx, Operand(kScratchRegister, 0));
728 : // Handle it if the stack pointer is already below the stack limit.
729 93584 : __ j(below_equal, &stack_limit_hit);
730 : // Check if there is room for the variable number of registers above
731 : // the stack limit.
732 187168 : __ cmpp(rcx, Immediate(num_registers_ * kPointerSize));
733 93584 : __ j(above_equal, &stack_ok);
734 : // Exit with OutOfMemory exception. There is not enough space on the stack
735 : // for our working registers.
736 93584 : __ Set(rax, EXCEPTION);
737 93584 : __ jmp(&return_rax);
738 :
739 93584 : __ bind(&stack_limit_hit);
740 93584 : __ Move(code_object_pointer(), masm_.CodeObject());
741 93584 : CallCheckStackGuardState(); // Preserves no registers beside rbp and rsp.
742 : __ testp(rax, rax);
743 : // If returned value is non-zero, we exit with the returned value as result.
744 93584 : __ j(not_zero, &return_rax);
745 :
746 93584 : __ bind(&stack_ok);
747 :
748 : // Allocate space on stack for registers.
749 187168 : __ subp(rsp, Immediate(num_registers_ * kPointerSize));
750 : // Load string length.
751 187168 : __ movp(rsi, Operand(rbp, kInputEnd));
752 : // Load input position.
753 187168 : __ movp(rdi, Operand(rbp, kInputStart));
754 : // Set up rdi to be negative offset from string end.
755 93584 : __ subq(rdi, rsi);
756 : // Set rax to address of char before start of the string
757 : // (effectively string position -1).
758 187168 : __ movp(rbx, Operand(rbp, kStartIndex));
759 : __ negq(rbx);
760 93584 : if (mode_ == UC16) {
761 142930 : __ leap(rax, Operand(rdi, rbx, times_2, -char_size()));
762 : } else {
763 44238 : __ leap(rax, Operand(rdi, rbx, times_1, -char_size()));
764 : }
765 : // Store this value in a local variable, for use when clearing
766 : // position registers.
767 187168 : __ movp(Operand(rbp, kStringStartMinusOne), rax);
768 :
769 : #if V8_OS_WIN
770 : // Ensure that we have written to each stack page, in order. Skipping a page
771 : // on Windows can cause segmentation faults. Assuming page size is 4k.
772 : const int kPageSize = 4096;
773 : const int kRegistersPerPage = kPageSize / kPointerSize;
774 : for (int i = num_saved_registers_ + kRegistersPerPage - 1;
775 : i < num_registers_;
776 : i += kRegistersPerPage) {
777 : __ movp(register_location(i), rax); // One write every page.
778 : }
779 : #endif // V8_OS_WIN
780 :
781 : // Initialize code object pointer.
782 93584 : __ Move(code_object_pointer(), masm_.CodeObject());
783 :
784 : Label load_char_start_regexp, start_regexp;
785 : // Load newline if index is at start, previous character otherwise.
786 187168 : __ cmpl(Operand(rbp, kStartIndex), Immediate(0));
787 93584 : __ j(not_equal, &load_char_start_regexp, Label::kNear);
788 93584 : __ Set(current_character(), '\n');
789 93584 : __ jmp(&start_regexp, Label::kNear);
790 :
791 : // Global regexp restarts matching here.
792 93584 : __ bind(&load_char_start_regexp);
793 : // Load previous char as initial value of current character register.
794 93584 : LoadCurrentCharacterUnchecked(-1, 1);
795 93584 : __ bind(&start_regexp);
796 :
797 : // Initialize on-stack registers.
798 93584 : if (num_saved_registers_ > 0) {
799 : // Fill saved registers with initial value = start offset - 1
800 : // Fill in stack push order, to avoid accessing across an unwritten
801 : // page (a problem on Windows).
802 93566 : if (num_saved_registers_ > 8) {
803 976 : __ Set(rcx, kRegisterZero);
804 : Label init_loop;
805 976 : __ bind(&init_loop);
806 1952 : __ movp(Operand(rbp, rcx, times_1, 0), rax);
807 976 : __ subq(rcx, Immediate(kPointerSize));
808 : __ cmpq(rcx,
809 1952 : Immediate(kRegisterZero - num_saved_registers_ * kPointerSize));
810 976 : __ j(greater, &init_loop);
811 : } else { // Unroll the loop.
812 205440 : for (int i = 0; i < num_saved_registers_; i++) {
813 205440 : __ movp(register_location(i), rax);
814 : }
815 : }
816 : }
817 :
818 : // Initialize backtrack stack pointer.
819 187168 : __ movp(backtrack_stackpointer(), Operand(rbp, kStackHighEnd));
820 :
821 93584 : __ jmp(&start_label_);
822 :
823 : // Exit code:
824 93584 : if (success_label_.is_linked()) {
825 : // Save captures when successful.
826 93261 : __ bind(&success_label_);
827 93261 : if (num_saved_registers_ > 0) {
828 : // copy captures to output
829 186498 : __ movp(rdx, Operand(rbp, kStartIndex));
830 186498 : __ movp(rbx, Operand(rbp, kRegisterOutput));
831 186498 : __ movp(rcx, Operand(rbp, kInputEnd));
832 186498 : __ subp(rcx, Operand(rbp, kInputStart));
833 93249 : if (mode_ == UC16) {
834 142930 : __ leap(rcx, Operand(rcx, rdx, times_2, 0));
835 : } else {
836 21784 : __ addp(rcx, rdx);
837 : }
838 258338 : for (int i = 0; i < num_saved_registers_; i++) {
839 258338 : __ movp(rax, register_location(i));
840 545722 : if (i == 0 && global_with_zero_length_check()) {
841 : // Keep capture start in rdx for the zero-length check later.
842 : __ movp(rdx, rax);
843 : }
844 258338 : __ addp(rax, rcx); // Convert to index from start, not end.
845 258338 : if (mode_ == UC16) {
846 : __ sarp(rax, Immediate(1)); // Convert byte index to character index.
847 : }
848 516676 : __ movl(Operand(rbx, i * kIntSize), rax);
849 : }
850 : }
851 :
852 93261 : if (global()) {
853 : // Restart matching if the regular expression is flagged as global.
854 : // Increment success counter.
855 14298 : __ incp(Operand(rbp, kSuccessfulCaptures));
856 : // Capture results have been stored, so the number of remaining global
857 : // output registers is reduced by the number of stored captures.
858 7149 : __ movsxlq(rcx, Operand(rbp, kNumOutputRegisters));
859 14298 : __ subp(rcx, Immediate(num_saved_registers_));
860 : // Check whether we have enough room for another set of capture results.
861 14298 : __ cmpp(rcx, Immediate(num_saved_registers_));
862 7149 : __ j(less, &exit_label_);
863 :
864 14298 : __ movp(Operand(rbp, kNumOutputRegisters), rcx);
865 : // Advance the location for output.
866 : __ addp(Operand(rbp, kRegisterOutput),
867 14298 : Immediate(num_saved_registers_ * kIntSize));
868 :
869 : // Prepare rax to initialize registers with its value in the next run.
870 14298 : __ movp(rax, Operand(rbp, kStringStartMinusOne));
871 :
872 7149 : if (global_with_zero_length_check()) {
873 : // Special case for zero-length matches.
874 : // rdx: capture start index
875 141 : __ cmpp(rdi, rdx);
876 : // Not a zero-length match, restart.
877 141 : __ j(not_equal, &load_char_start_regexp);
878 : // rdi (offset from the end) is zero if we already reached the end.
879 : __ testp(rdi, rdi);
880 141 : __ j(zero, &exit_label_, Label::kNear);
881 : // Advance current position after a zero-length match.
882 : Label advance;
883 141 : __ bind(&advance);
884 141 : if (mode_ == UC16) {
885 23 : __ addq(rdi, Immediate(2));
886 : } else {
887 : __ incq(rdi);
888 : }
889 141 : if (global_unicode()) CheckNotInSurrogatePair(0, &advance);
890 : }
891 :
892 7149 : __ jmp(&load_char_start_regexp);
893 : } else {
894 : __ movp(rax, Immediate(SUCCESS));
895 : }
896 : }
897 :
898 93584 : __ bind(&exit_label_);
899 93584 : if (global()) {
900 : // Return the number of successful captures.
901 14312 : __ movp(rax, Operand(rbp, kSuccessfulCaptures));
902 : }
903 :
904 93584 : __ bind(&return_rax);
905 : #ifdef _WIN64
906 : // Restore callee save registers.
907 : __ leap(rsp, Operand(rbp, kLastCalleeSaveRegister));
908 : __ popq(rbx);
909 : __ popq(rdi);
910 : __ popq(rsi);
911 : // Stack now at rbp.
912 : #else
913 : // Restore callee save register.
914 187168 : __ movp(rbx, Operand(rbp, kBackup_rbx));
915 : // Skip rsp to rbp.
916 : __ movp(rsp, rbp);
917 : #endif
918 : // Exit function frame, restore previous one.
919 93584 : __ popq(rbp);
920 93584 : __ ret(0);
921 :
922 : // Backtrack code (branch target for conditional backtracks).
923 93584 : if (backtrack_label_.is_linked()) {
924 93229 : __ bind(&backtrack_label_);
925 93229 : Backtrack();
926 : }
927 :
928 : Label exit_with_exception;
929 :
930 : // Preempt-code
931 93584 : if (check_preempt_label_.is_linked()) {
932 93542 : SafeCallTarget(&check_preempt_label_);
933 :
934 93542 : __ pushq(backtrack_stackpointer());
935 93542 : __ pushq(rdi);
936 :
937 93542 : CallCheckStackGuardState();
938 : __ testp(rax, rax);
939 : // If returning non-zero, we should end execution with the given
940 : // result as return value.
941 93542 : __ j(not_zero, &return_rax);
942 :
943 : // Restore registers.
944 93542 : __ Move(code_object_pointer(), masm_.CodeObject());
945 93542 : __ popq(rdi);
946 93542 : __ popq(backtrack_stackpointer());
947 : // String might have moved: Reload esi from frame.
948 187084 : __ movp(rsi, Operand(rbp, kInputEnd));
949 93542 : SafeReturn();
950 : }
951 :
952 : // Backtrack stack overflow code.
953 93584 : if (stack_overflow_label_.is_linked()) {
954 93548 : SafeCallTarget(&stack_overflow_label_);
955 : // Reached if the backtrack-stack limit has been hit.
956 :
957 : Label grow_failed;
958 : // Save registers before calling C function
959 : #ifndef _WIN64
960 : // Callee-save in Microsoft 64-bit ABI, but not in AMD64 ABI.
961 93548 : __ pushq(rsi);
962 93548 : __ pushq(rdi);
963 : #endif
964 :
965 : // Call GrowStack(backtrack_stackpointer())
966 : static const int num_arguments = 3;
967 93548 : __ PrepareCallCFunction(num_arguments);
968 : #ifdef _WIN64
969 : // Microsoft passes parameters in rcx, rdx, r8.
970 : // First argument, backtrack stackpointer, is already in rcx.
971 : __ leap(rdx, Operand(rbp, kStackHighEnd)); // Second argument
972 : __ LoadAddress(r8, ExternalReference::isolate_address(isolate()));
973 : #else
974 : // AMD64 ABI passes parameters in rdi, rsi, rdx.
975 : __ movp(rdi, backtrack_stackpointer()); // First argument.
976 187096 : __ leap(rsi, Operand(rbp, kStackHighEnd)); // Second argument.
977 93548 : __ LoadAddress(rdx, ExternalReference::isolate_address(isolate()));
978 : #endif
979 : ExternalReference grow_stack =
980 93548 : ExternalReference::re_grow_stack(isolate());
981 93548 : __ CallCFunction(grow_stack, num_arguments);
982 : // If return nullptr, we have failed to grow the stack, and
983 : // must exit with a stack-overflow exception.
984 : __ testp(rax, rax);
985 93548 : __ j(equal, &exit_with_exception);
986 : // Otherwise use return value as new stack pointer.
987 : __ movp(backtrack_stackpointer(), rax);
988 : // Restore saved registers and continue.
989 93548 : __ Move(code_object_pointer(), masm_.CodeObject());
990 : #ifndef _WIN64
991 93548 : __ popq(rdi);
992 93548 : __ popq(rsi);
993 : #endif
994 93548 : SafeReturn();
995 : }
996 :
997 93584 : if (exit_with_exception.is_linked()) {
998 : // If any of the code above needed to exit with an exception.
999 93548 : __ bind(&exit_with_exception);
1000 : // Exit with Result EXCEPTION(-1) to signal thrown exception.
1001 93548 : __ Set(rax, EXCEPTION);
1002 93548 : __ jmp(&return_rax);
1003 : }
1004 :
1005 93584 : FixupCodeRelativePositions();
1006 :
1007 : CodeDesc code_desc;
1008 : Isolate* isolate = this->isolate();
1009 93584 : masm_.GetCode(isolate, &code_desc);
1010 : Handle<Code> code =
1011 93584 : isolate->factory()->NewCode(code_desc, Code::REGEXP, masm_.CodeObject());
1012 93584 : PROFILE(isolate, RegExpCodeCreateEvent(AbstractCode::cast(*code), *source));
1013 93584 : return Handle<HeapObject>::cast(code);
1014 : }
1015 :
1016 :
1017 1248068 : void RegExpMacroAssemblerX64::GoTo(Label* to) {
1018 1248068 : BranchOrBacktrack(no_condition, to);
1019 1248068 : }
1020 :
1021 :
1022 4416 : void RegExpMacroAssemblerX64::IfRegisterGE(int reg,
1023 : int comparand,
1024 : Label* if_ge) {
1025 4416 : __ cmpp(register_location(reg), Immediate(comparand));
1026 4416 : BranchOrBacktrack(greater_equal, if_ge);
1027 4416 : }
1028 :
1029 :
1030 3154 : void RegExpMacroAssemblerX64::IfRegisterLT(int reg,
1031 : int comparand,
1032 : Label* if_lt) {
1033 3154 : __ cmpp(register_location(reg), Immediate(comparand));
1034 3154 : BranchOrBacktrack(less, if_lt);
1035 3154 : }
1036 :
1037 :
1038 316 : void RegExpMacroAssemblerX64::IfRegisterEqPos(int reg,
1039 : Label* if_eq) {
1040 316 : __ cmpp(rdi, register_location(reg));
1041 316 : BranchOrBacktrack(equal, if_eq);
1042 316 : }
1043 :
1044 :
1045 : RegExpMacroAssembler::IrregexpImplementation
1046 0 : RegExpMacroAssemblerX64::Implementation() {
1047 0 : return kX64Implementation;
1048 : }
1049 :
1050 :
1051 962855 : void RegExpMacroAssemblerX64::LoadCurrentCharacter(int cp_offset,
1052 : Label* on_end_of_input,
1053 : bool check_bounds,
1054 : int characters) {
1055 : DCHECK(cp_offset < (1<<30)); // Be sane! (And ensure negation works)
1056 962855 : if (check_bounds) {
1057 452615 : if (cp_offset >= 0) {
1058 449428 : CheckPosition(cp_offset + characters - 1, on_end_of_input);
1059 : } else {
1060 3187 : CheckPosition(cp_offset, on_end_of_input);
1061 : }
1062 : }
1063 962855 : LoadCurrentCharacterUnchecked(cp_offset, characters);
1064 962855 : }
1065 :
1066 :
1067 419465 : void RegExpMacroAssemblerX64::PopCurrentPosition() {
1068 419465 : Pop(rdi);
1069 419465 : }
1070 :
1071 :
1072 77031 : void RegExpMacroAssemblerX64::PopRegister(int register_index) {
1073 77031 : Pop(rax);
1074 77031 : __ movp(register_location(register_index), rax);
1075 77031 : }
1076 :
1077 :
1078 628765 : void RegExpMacroAssemblerX64::PushBacktrack(Label* label) {
1079 628765 : Push(label);
1080 628765 : CheckStackLimit();
1081 628765 : }
1082 :
1083 :
1084 434268 : void RegExpMacroAssemblerX64::PushCurrentPosition() {
1085 434268 : Push(rdi);
1086 434268 : }
1087 :
1088 :
1089 77049 : void RegExpMacroAssemblerX64::PushRegister(int register_index,
1090 : StackCheckFlag check_stack_limit) {
1091 77049 : __ movp(rax, register_location(register_index));
1092 77049 : Push(rax);
1093 77049 : if (check_stack_limit) CheckStackLimit();
1094 77049 : }
1095 :
1096 :
1097 : STATIC_ASSERT(kPointerSize == kInt64Size || kPointerSize == kInt32Size);
1098 :
1099 :
1100 4486 : void RegExpMacroAssemblerX64::ReadCurrentPositionFromRegister(int reg) {
1101 : if (kPointerSize == kInt64Size) {
1102 4486 : __ movq(rdi, register_location(reg));
1103 : } else {
1104 : // Need sign extension for x32 as rdi might be used as an index register.
1105 : __ movsxlq(rdi, register_location(reg));
1106 : }
1107 4486 : }
1108 :
1109 :
1110 5206 : void RegExpMacroAssemblerX64::ReadPositionFromRegister(Register dst, int reg) {
1111 : if (kPointerSize == kInt64Size) {
1112 5206 : __ movq(dst, register_location(reg));
1113 : } else {
1114 : // Need sign extension for x32 as dst might be used as an index register.
1115 : __ movsxlq(dst, register_location(reg));
1116 : }
1117 5206 : }
1118 :
1119 :
1120 4480 : void RegExpMacroAssemblerX64::ReadStackPointerFromRegister(int reg) {
1121 4480 : __ movp(backtrack_stackpointer(), register_location(reg));
1122 8960 : __ addp(backtrack_stackpointer(), Operand(rbp, kStackHighEnd));
1123 4480 : }
1124 :
1125 :
1126 690 : void RegExpMacroAssemblerX64::SetCurrentPositionFromEnd(int by) {
1127 : Label after_position;
1128 690 : __ cmpp(rdi, Immediate(-by * char_size()));
1129 230 : __ j(greater_equal, &after_position, Label::kNear);
1130 230 : __ movq(rdi, Immediate(-by * char_size()));
1131 : // On RegExp code entry (where this operation is used), the character before
1132 : // the current position is expected to be already loaded.
1133 : // We have advanced the position, so it's safe to read backwards.
1134 230 : LoadCurrentCharacterUnchecked(-1, 1);
1135 230 : __ bind(&after_position);
1136 230 : }
1137 :
1138 :
1139 6025 : void RegExpMacroAssemblerX64::SetRegister(int register_index, int to) {
1140 : DCHECK(register_index >= num_saved_registers_); // Reserved for positions!
1141 6025 : __ movp(register_location(register_index), Immediate(to));
1142 6025 : }
1143 :
1144 :
1145 103689 : bool RegExpMacroAssemblerX64::Succeed() {
1146 103689 : __ jmp(&success_label_);
1147 207378 : return global();
1148 : }
1149 :
1150 :
1151 487477 : void RegExpMacroAssemblerX64::WriteCurrentPositionToRegister(int reg,
1152 219954 : int cp_offset) {
1153 487477 : if (cp_offset == 0) {
1154 267523 : __ movp(register_location(reg), rdi);
1155 : } else {
1156 439908 : __ leap(rax, Operand(rdi, cp_offset * char_size()));
1157 219954 : __ movp(register_location(reg), rax);
1158 : }
1159 487477 : }
1160 :
1161 :
1162 152971 : void RegExpMacroAssemblerX64::ClearRegisters(int reg_from, int reg_to) {
1163 : DCHECK(reg_from <= reg_to);
1164 305942 : __ movp(rax, Operand(rbp, kStringStartMinusOne));
1165 445700 : for (int reg = reg_from; reg <= reg_to; reg++) {
1166 292729 : __ movp(register_location(reg), rax);
1167 : }
1168 152971 : }
1169 :
1170 :
1171 4445 : void RegExpMacroAssemblerX64::WriteStackPointerToRegister(int reg) {
1172 4445 : __ movp(rax, backtrack_stackpointer());
1173 8890 : __ subp(rax, Operand(rbp, kStackHighEnd));
1174 4445 : __ movp(register_location(reg), rax);
1175 4445 : }
1176 :
1177 :
1178 : // Private methods:
1179 :
1180 187126 : void RegExpMacroAssemblerX64::CallCheckStackGuardState() {
1181 : // This function call preserves no register values. Caller should
1182 : // store anything volatile in a C call or overwritten by this function.
1183 : static const int num_arguments = 3;
1184 187126 : __ PrepareCallCFunction(num_arguments);
1185 : #ifdef _WIN64
1186 : // Second argument: Code* of self. (Do this before overwriting r8).
1187 : __ movp(rdx, code_object_pointer());
1188 : // Third argument: RegExp code frame pointer.
1189 : __ movp(r8, rbp);
1190 : // First argument: Next address on the stack (will be address of
1191 : // return address).
1192 : __ leap(rcx, Operand(rsp, -kPointerSize));
1193 : #else
1194 : // Third argument: RegExp code frame pointer.
1195 187126 : __ movp(rdx, rbp);
1196 : // Second argument: Code* of self.
1197 : __ movp(rsi, code_object_pointer());
1198 : // First argument: Next address on the stack (will be address of
1199 : // return address).
1200 374252 : __ leap(rdi, Operand(rsp, -kRegisterSize));
1201 : #endif
1202 : ExternalReference stack_check =
1203 187126 : ExternalReference::re_check_stack_guard_state(isolate());
1204 187126 : __ CallCFunction(stack_check, num_arguments);
1205 187126 : }
1206 :
1207 :
1208 : // Helper function for reading a value out of a stack frame.
1209 : template <typename T>
1210 : static T& frame_entry(Address re_frame, int frame_offset) {
1211 : return reinterpret_cast<T&>(Memory::int32_at(re_frame + frame_offset));
1212 : }
1213 :
1214 :
1215 : template <typename T>
1216 : static T* frame_entry_address(Address re_frame, int frame_offset) {
1217 : return reinterpret_cast<T*>(re_frame + frame_offset);
1218 : }
1219 :
1220 :
1221 379 : int RegExpMacroAssemblerX64::CheckStackGuardState(Address* return_address,
1222 : Code* re_code,
1223 : Address re_frame) {
1224 : return NativeRegExpMacroAssembler::CheckStackGuardState(
1225 : frame_entry<Isolate*>(re_frame, kIsolate),
1226 : frame_entry<int>(re_frame, kStartIndex),
1227 : frame_entry<int>(re_frame, kDirectCall) == 1, return_address, re_code,
1228 : frame_entry_address<String*>(re_frame, kInputString),
1229 : frame_entry_address<const byte*>(re_frame, kInputStart),
1230 379 : frame_entry_address<const byte*>(re_frame, kInputEnd));
1231 : }
1232 :
1233 :
1234 1437874 : Operand RegExpMacroAssemblerX64::register_location(int register_index) {
1235 : DCHECK(register_index < (1<<30));
1236 1437874 : if (num_registers_ <= register_index) {
1237 6364 : num_registers_ = register_index + 1;
1238 : }
1239 1437874 : return Operand(rbp, kRegisterZero - register_index * kPointerSize);
1240 : }
1241 :
1242 :
1243 513593 : void RegExpMacroAssemblerX64::CheckPosition(int cp_offset,
1244 513593 : Label* on_outside_input) {
1245 513593 : if (cp_offset >= 0) {
1246 1531203 : __ cmpl(rdi, Immediate(-cp_offset * char_size()));
1247 510401 : BranchOrBacktrack(greater_equal, on_outside_input);
1248 : } else {
1249 6384 : __ leap(rax, Operand(rdi, cp_offset * char_size()));
1250 6384 : __ cmpp(rax, Operand(rbp, kStringStartMinusOne));
1251 3192 : BranchOrBacktrack(less_equal, on_outside_input);
1252 : }
1253 513593 : }
1254 :
1255 :
1256 2854766 : void RegExpMacroAssemblerX64::BranchOrBacktrack(Condition condition,
1257 : Label* to) {
1258 2854766 : if (condition < 0) { // No condition
1259 1262883 : if (to == nullptr) {
1260 19648 : Backtrack();
1261 19648 : return;
1262 : }
1263 1243235 : __ jmp(to);
1264 1243235 : return;
1265 : }
1266 1591883 : if (to == nullptr) {
1267 507333 : __ j(condition, &backtrack_label_);
1268 507333 : return;
1269 : }
1270 1084550 : __ j(condition, to);
1271 : }
1272 :
1273 :
1274 : void RegExpMacroAssemblerX64::SafeCall(Label* to) {
1275 863639 : __ call(to);
1276 : }
1277 :
1278 :
1279 187090 : void RegExpMacroAssemblerX64::SafeCallTarget(Label* label) {
1280 187090 : __ bind(label);
1281 374180 : __ subp(Operand(rsp, 0), code_object_pointer());
1282 187090 : }
1283 :
1284 :
1285 187090 : void RegExpMacroAssemblerX64::SafeReturn() {
1286 374180 : __ addp(Operand(rsp, 0), code_object_pointer());
1287 187090 : __ ret(0);
1288 187090 : }
1289 :
1290 :
1291 511317 : void RegExpMacroAssemblerX64::Push(Register source) {
1292 : DCHECK(source != backtrack_stackpointer());
1293 : // Notice: This updates flags, unlike normal Push.
1294 511317 : __ subp(backtrack_stackpointer(), Immediate(kIntSize));
1295 1022634 : __ movl(Operand(backtrack_stackpointer(), 0), source);
1296 511317 : }
1297 :
1298 :
1299 : void RegExpMacroAssemblerX64::Push(Immediate value) {
1300 : // Notice: This updates flags, unlike normal Push.
1301 : __ subp(backtrack_stackpointer(), Immediate(kIntSize));
1302 : __ movl(Operand(backtrack_stackpointer(), 0), value);
1303 : }
1304 :
1305 :
1306 93584 : void RegExpMacroAssemblerX64::FixupCodeRelativePositions() {
1307 722349 : for (int i = 0, n = code_relative_fixup_positions_.length(); i < n; i++) {
1308 628765 : int position = code_relative_fixup_positions_[i];
1309 : // The position succeeds a relative label offset from position.
1310 : // Patch the relative offset to be relative to the Code object pointer
1311 : // instead.
1312 628765 : int patch_position = position - kIntSize;
1313 628765 : int offset = masm_.long_at(patch_position);
1314 : masm_.long_at_put(patch_position,
1315 : offset
1316 628765 : + position
1317 : + Code::kHeaderSize
1318 628765 : - kHeapObjectTag);
1319 : }
1320 : code_relative_fixup_positions_.Clear();
1321 93584 : }
1322 :
1323 :
1324 628765 : void RegExpMacroAssemblerX64::Push(Label* backtrack_target) {
1325 628765 : __ subp(backtrack_stackpointer(), Immediate(kIntSize));
1326 628765 : __ movl(Operand(backtrack_stackpointer(), 0), backtrack_target);
1327 628765 : MarkPositionForCodeRelativeFixup();
1328 628765 : }
1329 :
1330 :
1331 728598 : void RegExpMacroAssemblerX64::Pop(Register target) {
1332 : DCHECK(target != backtrack_stackpointer());
1333 728598 : __ movsxlq(target, Operand(backtrack_stackpointer(), 0));
1334 : // Notice: This updates flags, unlike normal Pop.
1335 728598 : __ addp(backtrack_stackpointer(), Immediate(kIntSize));
1336 728598 : }
1337 :
1338 :
1339 : void RegExpMacroAssemblerX64::Drop() {
1340 14815 : __ addp(backtrack_stackpointer(), Immediate(kIntSize));
1341 : }
1342 :
1343 :
1344 232102 : void RegExpMacroAssemblerX64::CheckPreemption() {
1345 : // Check for preemption.
1346 : Label no_preempt;
1347 : ExternalReference stack_limit =
1348 232102 : ExternalReference::address_of_stack_limit(isolate());
1349 232102 : __ load_rax(stack_limit);
1350 232102 : __ cmpp(rsp, rax);
1351 232102 : __ j(above, &no_preempt);
1352 :
1353 232102 : SafeCall(&check_preempt_label_);
1354 :
1355 232102 : __ bind(&no_preempt);
1356 232102 : }
1357 :
1358 :
1359 631537 : void RegExpMacroAssemblerX64::CheckStackLimit() {
1360 : Label no_stack_overflow;
1361 : ExternalReference stack_limit =
1362 631537 : ExternalReference::address_of_regexp_stack_limit(isolate());
1363 631537 : __ load_rax(stack_limit);
1364 631537 : __ cmpp(backtrack_stackpointer(), rax);
1365 631537 : __ j(above, &no_stack_overflow);
1366 :
1367 631537 : SafeCall(&stack_overflow_label_);
1368 :
1369 631537 : __ bind(&no_stack_overflow);
1370 631537 : }
1371 :
1372 :
1373 1056669 : void RegExpMacroAssemblerX64::LoadCurrentCharacterUnchecked(int cp_offset,
1374 : int characters) {
1375 1056669 : if (mode_ == LATIN1) {
1376 856019 : if (characters == 4) {
1377 27256 : __ movl(current_character(), Operand(rsi, rdi, times_1, cp_offset));
1378 842391 : } else if (characters == 2) {
1379 323984 : __ movzxwl(current_character(), Operand(rsi, rdi, times_1, cp_offset));
1380 : } else {
1381 : DCHECK_EQ(1, characters);
1382 1360798 : __ movzxbl(current_character(), Operand(rsi, rdi, times_1, cp_offset));
1383 : }
1384 : } else {
1385 : DCHECK(mode_ == UC16);
1386 200650 : if (characters == 2) {
1387 : __ movl(current_character(),
1388 3260 : Operand(rsi, rdi, times_1, cp_offset * sizeof(uc16)));
1389 : } else {
1390 : DCHECK_EQ(1, characters);
1391 : __ movzxwl(current_character(),
1392 398040 : Operand(rsi, rdi, times_1, cp_offset * sizeof(uc16)));
1393 : }
1394 : }
1395 1056669 : }
1396 :
1397 : #undef __
1398 :
1399 : #endif // V8_INTERPRETED_REGEXP
1400 :
1401 : } // namespace internal
1402 : } // namespace v8
1403 :
1404 : #endif // V8_TARGET_ARCH_X64
|