Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(205)

Side by Side Diff: src/a64/regexp-macro-assembler-a64.cc

Issue 207823003: Rename A64 port to ARM64 port (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: retry Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/a64/regexp-macro-assembler-a64.h ('k') | src/a64/simulator-a64.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28 #include "v8.h"
29
30 #if V8_TARGET_ARCH_A64
31
32 #include "cpu-profiler.h"
33 #include "unicode.h"
34 #include "log.h"
35 #include "code-stubs.h"
36 #include "regexp-stack.h"
37 #include "macro-assembler.h"
38 #include "regexp-macro-assembler.h"
39 #include "a64/regexp-macro-assembler-a64.h"
40
41 namespace v8 {
42 namespace internal {
43
44 #ifndef V8_INTERPRETED_REGEXP
45 /*
46 * This assembler uses the following register assignment convention:
47 * - w19 : Used to temporarely store a value before a call to C code.
48 * See CheckNotBackReferenceIgnoreCase.
49 * - x20 : Pointer to the current code object (Code*),
50 * it includes the heap object tag.
51 * - w21 : Current position in input, as negative offset from
52 * the end of the string. Please notice that this is
53 * the byte offset, not the character offset!
54 * - w22 : Currently loaded character. Must be loaded using
55 * LoadCurrentCharacter before using any of the dispatch methods.
56 * - x23 : Points to tip of backtrack stack.
57 * - w24 : Position of the first character minus one: non_position_value.
58 * Used to initialize capture registers.
59 * - x25 : Address at the end of the input string: input_end.
60 * Points to byte after last character in input.
61 * - x26 : Address at the start of the input string: input_start.
62 * - w27 : Where to start in the input string.
63 * - x28 : Output array pointer.
64 * - x29/fp : Frame pointer. Used to access arguments, local variables and
65 * RegExp registers.
66 * - x16/x17 : IP registers, used by assembler. Very volatile.
67 * - csp : Points to tip of C stack.
68 *
69 * - x0-x7 : Used as a cache to store 32 bit capture registers. These
70 * registers need to be retained every time a call to C code
71 * is done.
72 *
73 * The remaining registers are free for computations.
74 * Each call to a public method should retain this convention.
75 *
76 * The stack will have the following structure:
77 *
78 * Location Name Description
79 * (as referred to in
80 * the code)
81 *
82 * - fp[104] isolate Address of the current isolate.
83 * - fp[96] return_address Secondary link/return address
84 * used by an exit frame if this is a
85 * native call.
86 * ^^^ csp when called ^^^
87 * - fp[88] lr Return from the RegExp code.
88 * - fp[80] r29 Old frame pointer (CalleeSaved).
89 * - fp[0..72] r19-r28 Backup of CalleeSaved registers.
90 * - fp[-8] direct_call 1 => Direct call from JavaScript code.
91 * 0 => Call through the runtime system.
92 * - fp[-16] stack_base High end of the memory area to use as
93 * the backtracking stack.
94 * - fp[-24] output_size Output may fit multiple sets of matches.
95 * - fp[-32] input Handle containing the input string.
96 * - fp[-40] success_counter
97 * ^^^^^^^^^^^^^ From here and downwards we store 32 bit values ^^^^^^^^^^^^^
98 * - fp[-44] register N Capture registers initialized with
99 * - fp[-48] register N + 1 non_position_value.
100 * ... The first kNumCachedRegisters (N) registers
101 * ... are cached in x0 to x7.
102 * ... Only positions must be stored in the first
103 * - ... num_saved_registers_ registers.
104 * - ...
105 * - register N + num_registers - 1
106 * ^^^^^^^^^ csp ^^^^^^^^^
107 *
108 * The first num_saved_registers_ registers are initialized to point to
109 * "character -1" in the string (i.e., char_size() bytes before the first
110 * character of the string). The remaining registers start out as garbage.
111 *
112 * The data up to the return address must be placed there by the calling
113 * code and the remaining arguments are passed in registers, e.g. by calling the
114 * code entry as cast to a function with the signature:
115 * int (*match)(String* input,
116 * int start_offset,
117 * Address input_start,
118 * Address input_end,
119 * int* output,
120 * int output_size,
121 * Address stack_base,
122 * bool direct_call = false,
123 * Address secondary_return_address, // Only used by native call.
124 * Isolate* isolate)
125 * The call is performed by NativeRegExpMacroAssembler::Execute()
126 * (in regexp-macro-assembler.cc) via the CALL_GENERATED_REGEXP_CODE macro
127 * in a64/simulator-a64.h.
128 * When calling as a non-direct call (i.e., from C++ code), the return address
129 * area is overwritten with the LR register by the RegExp code. When doing a
130 * direct call from generated code, the return address is placed there by
131 * the calling code, as in a normal exit frame.
132 */
133
134 #define __ ACCESS_MASM(masm_)
135
136 RegExpMacroAssemblerA64::RegExpMacroAssemblerA64(
137 Mode mode,
138 int registers_to_save,
139 Zone* zone)
140 : NativeRegExpMacroAssembler(zone),
141 masm_(new MacroAssembler(zone->isolate(), NULL, kRegExpCodeSize)),
142 mode_(mode),
143 num_registers_(registers_to_save),
144 num_saved_registers_(registers_to_save),
145 entry_label_(),
146 start_label_(),
147 success_label_(),
148 backtrack_label_(),
149 exit_label_() {
150 __ SetStackPointer(csp);
151 ASSERT_EQ(0, registers_to_save % 2);
152 // We can cache at most 16 W registers in x0-x7.
153 STATIC_ASSERT(kNumCachedRegisters <= 16);
154 STATIC_ASSERT((kNumCachedRegisters % 2) == 0);
155 __ B(&entry_label_); // We'll write the entry code later.
156 __ Bind(&start_label_); // And then continue from here.
157 }
158
159
160 RegExpMacroAssemblerA64::~RegExpMacroAssemblerA64() {
161 delete masm_;
162 // Unuse labels in case we throw away the assembler without calling GetCode.
163 entry_label_.Unuse();
164 start_label_.Unuse();
165 success_label_.Unuse();
166 backtrack_label_.Unuse();
167 exit_label_.Unuse();
168 check_preempt_label_.Unuse();
169 stack_overflow_label_.Unuse();
170 }
171
172 int RegExpMacroAssemblerA64::stack_limit_slack() {
173 return RegExpStack::kStackLimitSlack;
174 }
175
176
177 void RegExpMacroAssemblerA64::AdvanceCurrentPosition(int by) {
178 if (by != 0) {
179 __ Add(current_input_offset(),
180 current_input_offset(), by * char_size());
181 }
182 }
183
184
185 void RegExpMacroAssemblerA64::AdvanceRegister(int reg, int by) {
186 ASSERT((reg >= 0) && (reg < num_registers_));
187 if (by != 0) {
188 Register to_advance;
189 RegisterState register_state = GetRegisterState(reg);
190 switch (register_state) {
191 case STACKED:
192 __ Ldr(w10, register_location(reg));
193 __ Add(w10, w10, by);
194 __ Str(w10, register_location(reg));
195 break;
196 case CACHED_LSW:
197 to_advance = GetCachedRegister(reg);
198 __ Add(to_advance, to_advance, by);
199 break;
200 case CACHED_MSW:
201 to_advance = GetCachedRegister(reg);
202 __ Add(to_advance, to_advance,
203 static_cast<int64_t>(by) << kWRegSizeInBits);
204 break;
205 default:
206 UNREACHABLE();
207 break;
208 }
209 }
210 }
211
212
213 void RegExpMacroAssemblerA64::Backtrack() {
214 CheckPreemption();
215 Pop(w10);
216 __ Add(x10, code_pointer(), Operand(w10, UXTW));
217 __ Br(x10);
218 }
219
220
221 void RegExpMacroAssemblerA64::Bind(Label* label) {
222 __ Bind(label);
223 }
224
225
226 void RegExpMacroAssemblerA64::CheckCharacter(uint32_t c, Label* on_equal) {
227 CompareAndBranchOrBacktrack(current_character(), c, eq, on_equal);
228 }
229
230
231 void RegExpMacroAssemblerA64::CheckCharacterGT(uc16 limit, Label* on_greater) {
232 CompareAndBranchOrBacktrack(current_character(), limit, hi, on_greater);
233 }
234
235
236 void RegExpMacroAssemblerA64::CheckAtStart(Label* on_at_start) {
237 Label not_at_start;
238 // Did we start the match at the start of the input string?
239 CompareAndBranchOrBacktrack(start_offset(), 0, ne, &not_at_start);
240 // If we did, are we still at the start of the input string?
241 __ Add(x10, input_end(), Operand(current_input_offset(), SXTW));
242 __ Cmp(x10, input_start());
243 BranchOrBacktrack(eq, on_at_start);
244 __ Bind(&not_at_start);
245 }
246
247
248 void RegExpMacroAssemblerA64::CheckNotAtStart(Label* on_not_at_start) {
249 // Did we start the match at the start of the input string?
250 CompareAndBranchOrBacktrack(start_offset(), 0, ne, on_not_at_start);
251 // If we did, are we still at the start of the input string?
252 __ Add(x10, input_end(), Operand(current_input_offset(), SXTW));
253 __ Cmp(x10, input_start());
254 BranchOrBacktrack(ne, on_not_at_start);
255 }
256
257
258 void RegExpMacroAssemblerA64::CheckCharacterLT(uc16 limit, Label* on_less) {
259 CompareAndBranchOrBacktrack(current_character(), limit, lo, on_less);
260 }
261
262
263 void RegExpMacroAssemblerA64::CheckCharacters(Vector<const uc16> str,
264 int cp_offset,
265 Label* on_failure,
266 bool check_end_of_string) {
267 // This method is only ever called from the cctests.
268
269 if (check_end_of_string) {
270 // Is last character of required match inside string.
271 CheckPosition(cp_offset + str.length() - 1, on_failure);
272 }
273
274 Register characters_address = x11;
275
276 __ Add(characters_address,
277 input_end(),
278 Operand(current_input_offset(), SXTW));
279 if (cp_offset != 0) {
280 __ Add(characters_address, characters_address, cp_offset * char_size());
281 }
282
283 for (int i = 0; i < str.length(); i++) {
284 if (mode_ == ASCII) {
285 __ Ldrb(w10, MemOperand(characters_address, 1, PostIndex));
286 ASSERT(str[i] <= String::kMaxOneByteCharCode);
287 } else {
288 __ Ldrh(w10, MemOperand(characters_address, 2, PostIndex));
289 }
290 CompareAndBranchOrBacktrack(w10, str[i], ne, on_failure);
291 }
292 }
293
294
295 void RegExpMacroAssemblerA64::CheckGreedyLoop(Label* on_equal) {
296 __ Ldr(w10, MemOperand(backtrack_stackpointer()));
297 __ Cmp(current_input_offset(), w10);
298 __ Cset(x11, eq);
299 __ Add(backtrack_stackpointer(),
300 backtrack_stackpointer(), Operand(x11, LSL, kWRegSizeLog2));
301 BranchOrBacktrack(eq, on_equal);
302 }
303
304 void RegExpMacroAssemblerA64::CheckNotBackReferenceIgnoreCase(
305 int start_reg,
306 Label* on_no_match) {
307 Label fallthrough;
308
309 Register capture_start_offset = w10;
310 // Save the capture length in a callee-saved register so it will
311 // be preserved if we call a C helper.
312 Register capture_length = w19;
313 ASSERT(kCalleeSaved.IncludesAliasOf(capture_length));
314
315 // Find length of back-referenced capture.
316 ASSERT((start_reg % 2) == 0);
317 if (start_reg < kNumCachedRegisters) {
318 __ Mov(capture_start_offset.X(), GetCachedRegister(start_reg));
319 __ Lsr(x11, GetCachedRegister(start_reg), kWRegSizeInBits);
320 } else {
321 __ Ldp(w11, capture_start_offset, capture_location(start_reg, x10));
322 }
323 __ Sub(capture_length, w11, capture_start_offset); // Length to check.
324 // Succeed on empty capture (including no capture).
325 __ Cbz(capture_length, &fallthrough);
326
327 // Check that there are enough characters left in the input.
328 __ Cmn(capture_length, current_input_offset());
329 BranchOrBacktrack(gt, on_no_match);
330
331 if (mode_ == ASCII) {
332 Label success;
333 Label fail;
334 Label loop_check;
335
336 Register capture_start_address = x12;
337 Register capture_end_addresss = x13;
338 Register current_position_address = x14;
339
340 __ Add(capture_start_address,
341 input_end(),
342 Operand(capture_start_offset, SXTW));
343 __ Add(capture_end_addresss,
344 capture_start_address,
345 Operand(capture_length, SXTW));
346 __ Add(current_position_address,
347 input_end(),
348 Operand(current_input_offset(), SXTW));
349
350 Label loop;
351 __ Bind(&loop);
352 __ Ldrb(w10, MemOperand(capture_start_address, 1, PostIndex));
353 __ Ldrb(w11, MemOperand(current_position_address, 1, PostIndex));
354 __ Cmp(w10, w11);
355 __ B(eq, &loop_check);
356
357 // Mismatch, try case-insensitive match (converting letters to lower-case).
358 __ Orr(w10, w10, 0x20); // Convert capture character to lower-case.
359 __ Orr(w11, w11, 0x20); // Also convert input character.
360 __ Cmp(w11, w10);
361 __ B(ne, &fail);
362 __ Sub(w10, w10, 'a');
363 __ Cmp(w10, 'z' - 'a'); // Is w10 a lowercase letter?
364 __ B(ls, &loop_check); // In range 'a'-'z'.
365 // Latin-1: Check for values in range [224,254] but not 247.
366 __ Sub(w10, w10, 224 - 'a');
367 __ Cmp(w10, 254 - 224);
368 __ Ccmp(w10, 247 - 224, ZFlag, ls); // Check for 247.
369 __ B(eq, &fail); // Weren't Latin-1 letters.
370
371 __ Bind(&loop_check);
372 __ Cmp(capture_start_address, capture_end_addresss);
373 __ B(lt, &loop);
374 __ B(&success);
375
376 __ Bind(&fail);
377 BranchOrBacktrack(al, on_no_match);
378
379 __ Bind(&success);
380 // Compute new value of character position after the matched part.
381 __ Sub(current_input_offset().X(), current_position_address, input_end());
382 if (masm_->emit_debug_code()) {
383 __ Cmp(current_input_offset().X(), Operand(current_input_offset(), SXTW));
384 __ Ccmp(current_input_offset(), 0, NoFlag, eq);
385 // The current input offset should be <= 0, and fit in a W register.
386 __ Check(le, kOffsetOutOfRange);
387 }
388 } else {
389 ASSERT(mode_ == UC16);
390 int argument_count = 4;
391
392 // The cached registers need to be retained.
393 CPURegList cached_registers(CPURegister::kRegister, kXRegSizeInBits, 0, 7);
394 ASSERT((cached_registers.Count() * 2) == kNumCachedRegisters);
395 __ PushCPURegList(cached_registers);
396
397 // Put arguments into arguments registers.
398 // Parameters are
399 // x0: Address byte_offset1 - Address captured substring's start.
400 // x1: Address byte_offset2 - Address of current character position.
401 // w2: size_t byte_length - length of capture in bytes(!)
402 // x3: Isolate* isolate
403
404 // Address of start of capture.
405 __ Add(x0, input_end(), Operand(capture_start_offset, SXTW));
406 // Length of capture.
407 __ Mov(w2, capture_length);
408 // Address of current input position.
409 __ Add(x1, input_end(), Operand(current_input_offset(), SXTW));
410 // Isolate.
411 __ Mov(x3, ExternalReference::isolate_address(isolate()));
412
413 {
414 AllowExternalCallThatCantCauseGC scope(masm_);
415 ExternalReference function =
416 ExternalReference::re_case_insensitive_compare_uc16(isolate());
417 __ CallCFunction(function, argument_count);
418 }
419
420 // Check if function returned non-zero for success or zero for failure.
421 CompareAndBranchOrBacktrack(x0, 0, eq, on_no_match);
422 // On success, increment position by length of capture.
423 __ Add(current_input_offset(), current_input_offset(), capture_length);
424 // Reset the cached registers.
425 __ PopCPURegList(cached_registers);
426 }
427
428 __ Bind(&fallthrough);
429 }
430
431 void RegExpMacroAssemblerA64::CheckNotBackReference(
432 int start_reg,
433 Label* on_no_match) {
434 Label fallthrough;
435
436 Register capture_start_address = x12;
437 Register capture_end_address = x13;
438 Register current_position_address = x14;
439 Register capture_length = w15;
440
441 // Find length of back-referenced capture.
442 ASSERT((start_reg % 2) == 0);
443 if (start_reg < kNumCachedRegisters) {
444 __ Mov(x10, GetCachedRegister(start_reg));
445 __ Lsr(x11, GetCachedRegister(start_reg), kWRegSizeInBits);
446 } else {
447 __ Ldp(w11, w10, capture_location(start_reg, x10));
448 }
449 __ Sub(capture_length, w11, w10); // Length to check.
450 // Succeed on empty capture (including no capture).
451 __ Cbz(capture_length, &fallthrough);
452
453 // Check that there are enough characters left in the input.
454 __ Cmn(capture_length, current_input_offset());
455 BranchOrBacktrack(gt, on_no_match);
456
457 // Compute pointers to match string and capture string
458 __ Add(capture_start_address, input_end(), Operand(w10, SXTW));
459 __ Add(capture_end_address,
460 capture_start_address,
461 Operand(capture_length, SXTW));
462 __ Add(current_position_address,
463 input_end(),
464 Operand(current_input_offset(), SXTW));
465
466 Label loop;
467 __ Bind(&loop);
468 if (mode_ == ASCII) {
469 __ Ldrb(w10, MemOperand(capture_start_address, 1, PostIndex));
470 __ Ldrb(w11, MemOperand(current_position_address, 1, PostIndex));
471 } else {
472 ASSERT(mode_ == UC16);
473 __ Ldrh(w10, MemOperand(capture_start_address, 2, PostIndex));
474 __ Ldrh(w11, MemOperand(current_position_address, 2, PostIndex));
475 }
476 __ Cmp(w10, w11);
477 BranchOrBacktrack(ne, on_no_match);
478 __ Cmp(capture_start_address, capture_end_address);
479 __ B(lt, &loop);
480
481 // Move current character position to position after match.
482 __ Sub(current_input_offset().X(), current_position_address, input_end());
483 if (masm_->emit_debug_code()) {
484 __ Cmp(current_input_offset().X(), Operand(current_input_offset(), SXTW));
485 __ Ccmp(current_input_offset(), 0, NoFlag, eq);
486 // The current input offset should be <= 0, and fit in a W register.
487 __ Check(le, kOffsetOutOfRange);
488 }
489 __ Bind(&fallthrough);
490 }
491
492
493 void RegExpMacroAssemblerA64::CheckNotCharacter(unsigned c,
494 Label* on_not_equal) {
495 CompareAndBranchOrBacktrack(current_character(), c, ne, on_not_equal);
496 }
497
498
499 void RegExpMacroAssemblerA64::CheckCharacterAfterAnd(uint32_t c,
500 uint32_t mask,
501 Label* on_equal) {
502 __ And(w10, current_character(), mask);
503 CompareAndBranchOrBacktrack(w10, c, eq, on_equal);
504 }
505
506
507 void RegExpMacroAssemblerA64::CheckNotCharacterAfterAnd(unsigned c,
508 unsigned mask,
509 Label* on_not_equal) {
510 __ And(w10, current_character(), mask);
511 CompareAndBranchOrBacktrack(w10, c, ne, on_not_equal);
512 }
513
514
515 void RegExpMacroAssemblerA64::CheckNotCharacterAfterMinusAnd(
516 uc16 c,
517 uc16 minus,
518 uc16 mask,
519 Label* on_not_equal) {
520 ASSERT(minus < String::kMaxUtf16CodeUnit);
521 __ Sub(w10, current_character(), minus);
522 __ And(w10, w10, mask);
523 CompareAndBranchOrBacktrack(w10, c, ne, on_not_equal);
524 }
525
526
527 void RegExpMacroAssemblerA64::CheckCharacterInRange(
528 uc16 from,
529 uc16 to,
530 Label* on_in_range) {
531 __ Sub(w10, current_character(), from);
532 // Unsigned lower-or-same condition.
533 CompareAndBranchOrBacktrack(w10, to - from, ls, on_in_range);
534 }
535
536
537 void RegExpMacroAssemblerA64::CheckCharacterNotInRange(
538 uc16 from,
539 uc16 to,
540 Label* on_not_in_range) {
541 __ Sub(w10, current_character(), from);
542 // Unsigned higher condition.
543 CompareAndBranchOrBacktrack(w10, to - from, hi, on_not_in_range);
544 }
545
546
547 void RegExpMacroAssemblerA64::CheckBitInTable(
548 Handle<ByteArray> table,
549 Label* on_bit_set) {
550 __ Mov(x11, Operand(table));
551 if ((mode_ != ASCII) || (kTableMask != String::kMaxOneByteCharCode)) {
552 __ And(w10, current_character(), kTableMask);
553 __ Add(w10, w10, ByteArray::kHeaderSize - kHeapObjectTag);
554 } else {
555 __ Add(w10, current_character(), ByteArray::kHeaderSize - kHeapObjectTag);
556 }
557 __ Ldrb(w11, MemOperand(x11, w10, UXTW));
558 CompareAndBranchOrBacktrack(w11, 0, ne, on_bit_set);
559 }
560
561
562 bool RegExpMacroAssemblerA64::CheckSpecialCharacterClass(uc16 type,
563 Label* on_no_match) {
564 // Range checks (c in min..max) are generally implemented by an unsigned
565 // (c - min) <= (max - min) check
566 switch (type) {
567 case 's':
568 // Match space-characters
569 if (mode_ == ASCII) {
570 // One byte space characters are '\t'..'\r', ' ' and \u00a0.
571 Label success;
572 // Check for ' ' or 0x00a0.
573 __ Cmp(current_character(), ' ');
574 __ Ccmp(current_character(), 0x00a0, ZFlag, ne);
575 __ B(eq, &success);
576 // Check range 0x09..0x0d.
577 __ Sub(w10, current_character(), '\t');
578 CompareAndBranchOrBacktrack(w10, '\r' - '\t', hi, on_no_match);
579 __ Bind(&success);
580 return true;
581 }
582 return false;
583 case 'S':
584 // The emitted code for generic character classes is good enough.
585 return false;
586 case 'd':
587 // Match ASCII digits ('0'..'9').
588 __ Sub(w10, current_character(), '0');
589 CompareAndBranchOrBacktrack(w10, '9' - '0', hi, on_no_match);
590 return true;
591 case 'D':
592 // Match ASCII non-digits.
593 __ Sub(w10, current_character(), '0');
594 CompareAndBranchOrBacktrack(w10, '9' - '0', ls, on_no_match);
595 return true;
596 case '.': {
597 // Match non-newlines (not 0x0a('\n'), 0x0d('\r'), 0x2028 and 0x2029)
598 // Here we emit the conditional branch only once at the end to make branch
599 // prediction more efficient, even though we could branch out of here
600 // as soon as a character matches.
601 __ Cmp(current_character(), 0x0a);
602 __ Ccmp(current_character(), 0x0d, ZFlag, ne);
603 if (mode_ == UC16) {
604 __ Sub(w10, current_character(), 0x2028);
605 // If the Z flag was set we clear the flags to force a branch.
606 __ Ccmp(w10, 0x2029 - 0x2028, NoFlag, ne);
607 // ls -> !((C==1) && (Z==0))
608 BranchOrBacktrack(ls, on_no_match);
609 } else {
610 BranchOrBacktrack(eq, on_no_match);
611 }
612 return true;
613 }
614 case 'n': {
615 // Match newlines (0x0a('\n'), 0x0d('\r'), 0x2028 and 0x2029)
616 // We have to check all 4 newline characters before emitting
617 // the conditional branch.
618 __ Cmp(current_character(), 0x0a);
619 __ Ccmp(current_character(), 0x0d, ZFlag, ne);
620 if (mode_ == UC16) {
621 __ Sub(w10, current_character(), 0x2028);
622 // If the Z flag was set we clear the flags to force a fall-through.
623 __ Ccmp(w10, 0x2029 - 0x2028, NoFlag, ne);
624 // hi -> (C==1) && (Z==0)
625 BranchOrBacktrack(hi, on_no_match);
626 } else {
627 BranchOrBacktrack(ne, on_no_match);
628 }
629 return true;
630 }
631 case 'w': {
632 if (mode_ != ASCII) {
633 // Table is 128 entries, so all ASCII characters can be tested.
634 CompareAndBranchOrBacktrack(current_character(), 'z', hi, on_no_match);
635 }
636 ExternalReference map = ExternalReference::re_word_character_map();
637 __ Mov(x10, map);
638 __ Ldrb(w10, MemOperand(x10, current_character(), UXTW));
639 CompareAndBranchOrBacktrack(w10, 0, eq, on_no_match);
640 return true;
641 }
642 case 'W': {
643 Label done;
644 if (mode_ != ASCII) {
645 // Table is 128 entries, so all ASCII characters can be tested.
646 __ Cmp(current_character(), 'z');
647 __ B(hi, &done);
648 }
649 ExternalReference map = ExternalReference::re_word_character_map();
650 __ Mov(x10, map);
651 __ Ldrb(w10, MemOperand(x10, current_character(), UXTW));
652 CompareAndBranchOrBacktrack(w10, 0, ne, on_no_match);
653 __ Bind(&done);
654 return true;
655 }
656 case '*':
657 // Match any character.
658 return true;
659 // No custom implementation (yet): s(UC16), S(UC16).
660 default:
661 return false;
662 }
663 }
664
665
666 void RegExpMacroAssemblerA64::Fail() {
667 __ Mov(w0, FAILURE);
668 __ B(&exit_label_);
669 }
670
671
672 Handle<HeapObject> RegExpMacroAssemblerA64::GetCode(Handle<String> source) {
673 Label return_w0;
674 // Finalize code - write the entry point code now we know how many
675 // registers we need.
676
677 // Entry code:
678 __ Bind(&entry_label_);
679
680 // Arguments on entry:
681 // x0: String* input
682 // x1: int start_offset
683 // x2: byte* input_start
684 // x3: byte* input_end
685 // x4: int* output array
686 // x5: int output array size
687 // x6: Address stack_base
688 // x7: int direct_call
689
690 // The stack pointer should be csp on entry.
691 // csp[8]: address of the current isolate
692 // csp[0]: secondary link/return address used by native call
693
694 // Tell the system that we have a stack frame. Because the type is MANUAL, no
695 // code is generated.
696 FrameScope scope(masm_, StackFrame::MANUAL);
697
698 // Push registers on the stack, only push the argument registers that we need.
699 CPURegList argument_registers(x0, x5, x6, x7);
700
701 CPURegList registers_to_retain = kCalleeSaved;
702 ASSERT(kCalleeSaved.Count() == 11);
703 registers_to_retain.Combine(lr);
704
705 ASSERT(csp.Is(__ StackPointer()));
706 __ PushCPURegList(registers_to_retain);
707 __ PushCPURegList(argument_registers);
708
709 // Set frame pointer in place.
710 __ Add(frame_pointer(), csp, argument_registers.Count() * kPointerSize);
711
712 // Initialize callee-saved registers.
713 __ Mov(start_offset(), w1);
714 __ Mov(input_start(), x2);
715 __ Mov(input_end(), x3);
716 __ Mov(output_array(), x4);
717
718 // Set the number of registers we will need to allocate, that is:
719 // - success_counter (X register)
720 // - (num_registers_ - kNumCachedRegisters) (W registers)
721 int num_wreg_to_allocate = num_registers_ - kNumCachedRegisters;
722 // Do not allocate registers on the stack if they can all be cached.
723 if (num_wreg_to_allocate < 0) { num_wreg_to_allocate = 0; }
724 // Make room for the success_counter.
725 num_wreg_to_allocate += 2;
726
727 // Make sure the stack alignment will be respected.
728 int alignment = masm_->ActivationFrameAlignment();
729 ASSERT_EQ(alignment % 16, 0);
730 int align_mask = (alignment / kWRegSize) - 1;
731 num_wreg_to_allocate = (num_wreg_to_allocate + align_mask) & ~align_mask;
732
733 // Check if we have space on the stack.
734 Label stack_limit_hit;
735 Label stack_ok;
736
737 ExternalReference stack_limit =
738 ExternalReference::address_of_stack_limit(isolate());
739 __ Mov(x10, stack_limit);
740 __ Ldr(x10, MemOperand(x10));
741 __ Subs(x10, csp, x10);
742
743 // Handle it if the stack pointer is already below the stack limit.
744 __ B(ls, &stack_limit_hit);
745
746 // Check if there is room for the variable number of registers above
747 // the stack limit.
748 __ Cmp(x10, num_wreg_to_allocate * kWRegSize);
749 __ B(hs, &stack_ok);
750
751 // Exit with OutOfMemory exception. There is not enough space on the stack
752 // for our working registers.
753 __ Mov(w0, EXCEPTION);
754 __ B(&return_w0);
755
756 __ Bind(&stack_limit_hit);
757 CallCheckStackGuardState(x10);
758 // If returned value is non-zero, we exit with the returned value as result.
759 __ Cbnz(w0, &return_w0);
760
761 __ Bind(&stack_ok);
762
763 // Allocate space on stack.
764 __ Claim(num_wreg_to_allocate, kWRegSize);
765
766 // Initialize success_counter with 0.
767 __ Str(wzr, MemOperand(frame_pointer(), kSuccessCounter));
768
769 // Find negative length (offset of start relative to end).
770 __ Sub(x10, input_start(), input_end());
771 if (masm_->emit_debug_code()) {
772 // Check that the input string length is < 2^30.
773 __ Neg(x11, x10);
774 __ Cmp(x11, (1<<30) - 1);
775 __ Check(ls, kInputStringTooLong);
776 }
777 __ Mov(current_input_offset(), w10);
778
779 // The non-position value is used as a clearing value for the
780 // capture registers, it corresponds to the position of the first character
781 // minus one.
782 __ Sub(non_position_value(), current_input_offset(), char_size());
783 __ Sub(non_position_value(), non_position_value(),
784 Operand(start_offset(), LSL, (mode_ == UC16) ? 1 : 0));
785 // We can store this value twice in an X register for initializing
786 // on-stack registers later.
787 __ Orr(twice_non_position_value(),
788 non_position_value().X(),
789 Operand(non_position_value().X(), LSL, kWRegSizeInBits));
790
791 // Initialize code pointer register.
792 __ Mov(code_pointer(), Operand(masm_->CodeObject()));
793
794 Label load_char_start_regexp, start_regexp;
795 // Load newline if index is at start, previous character otherwise.
796 __ Cbnz(start_offset(), &load_char_start_regexp);
797 __ Mov(current_character(), '\n');
798 __ B(&start_regexp);
799
800 // Global regexp restarts matching here.
801 __ Bind(&load_char_start_regexp);
802 // Load previous char as initial value of current character register.
803 LoadCurrentCharacterUnchecked(-1, 1);
804 __ Bind(&start_regexp);
805 // Initialize on-stack registers.
806 if (num_saved_registers_ > 0) {
807 ClearRegisters(0, num_saved_registers_ - 1);
808 }
809
810 // Initialize backtrack stack pointer.
811 __ Ldr(backtrack_stackpointer(), MemOperand(frame_pointer(), kStackBase));
812
813 // Execute
814 __ B(&start_label_);
815
816 if (backtrack_label_.is_linked()) {
817 __ Bind(&backtrack_label_);
818 Backtrack();
819 }
820
821 if (success_label_.is_linked()) {
822 Register first_capture_start = w15;
823
824 // Save captures when successful.
825 __ Bind(&success_label_);
826
827 if (num_saved_registers_ > 0) {
828 // V8 expects the output to be an int32_t array.
829 Register capture_start = w12;
830 Register capture_end = w13;
831 Register input_length = w14;
832
833 // Copy captures to output.
834
835 // Get string length.
836 __ Sub(x10, input_end(), input_start());
837 if (masm_->emit_debug_code()) {
838 // Check that the input string length is < 2^30.
839 __ Cmp(x10, (1<<30) - 1);
840 __ Check(ls, kInputStringTooLong);
841 }
842 // input_start has a start_offset offset on entry. We need to include
843 // it when computing the length of the whole string.
844 if (mode_ == UC16) {
845 __ Add(input_length, start_offset(), Operand(w10, LSR, 1));
846 } else {
847 __ Add(input_length, start_offset(), w10);
848 }
849
850 // Copy the results to the output array from the cached registers first.
851 for (int i = 0;
852 (i < num_saved_registers_) && (i < kNumCachedRegisters);
853 i += 2) {
854 __ Mov(capture_start.X(), GetCachedRegister(i));
855 __ Lsr(capture_end.X(), capture_start.X(), kWRegSizeInBits);
856 if ((i == 0) && global_with_zero_length_check()) {
857 // Keep capture start for the zero-length check later.
858 __ Mov(first_capture_start, capture_start);
859 }
860 // Offsets need to be relative to the start of the string.
861 if (mode_ == UC16) {
862 __ Add(capture_start, input_length, Operand(capture_start, ASR, 1));
863 __ Add(capture_end, input_length, Operand(capture_end, ASR, 1));
864 } else {
865 __ Add(capture_start, input_length, capture_start);
866 __ Add(capture_end, input_length, capture_end);
867 }
868 // The output pointer advances for a possible global match.
869 __ Stp(capture_start,
870 capture_end,
871 MemOperand(output_array(), kPointerSize, PostIndex));
872 }
873
874 // Only carry on if there are more than kNumCachedRegisters capture
875 // registers.
876 int num_registers_left_on_stack =
877 num_saved_registers_ - kNumCachedRegisters;
878 if (num_registers_left_on_stack > 0) {
879 Register base = x10;
880 // There are always an even number of capture registers. A couple of
881 // registers determine one match with two offsets.
882 ASSERT_EQ(0, num_registers_left_on_stack % 2);
883 __ Add(base, frame_pointer(), kFirstCaptureOnStack);
884
885 // We can unroll the loop here, we should not unroll for less than 2
886 // registers.
887 STATIC_ASSERT(kNumRegistersToUnroll > 2);
888 if (num_registers_left_on_stack <= kNumRegistersToUnroll) {
889 for (int i = 0; i < num_registers_left_on_stack / 2; i++) {
890 __ Ldp(capture_end,
891 capture_start,
892 MemOperand(base, -kPointerSize, PostIndex));
893 if ((i == 0) && global_with_zero_length_check()) {
894 // Keep capture start for the zero-length check later.
895 __ Mov(first_capture_start, capture_start);
896 }
897 // Offsets need to be relative to the start of the string.
898 if (mode_ == UC16) {
899 __ Add(capture_start,
900 input_length,
901 Operand(capture_start, ASR, 1));
902 __ Add(capture_end, input_length, Operand(capture_end, ASR, 1));
903 } else {
904 __ Add(capture_start, input_length, capture_start);
905 __ Add(capture_end, input_length, capture_end);
906 }
907 // The output pointer advances for a possible global match.
908 __ Stp(capture_start,
909 capture_end,
910 MemOperand(output_array(), kPointerSize, PostIndex));
911 }
912 } else {
913 Label loop, start;
914 __ Mov(x11, num_registers_left_on_stack);
915
916 __ Ldp(capture_end,
917 capture_start,
918 MemOperand(base, -kPointerSize, PostIndex));
919 if (global_with_zero_length_check()) {
920 __ Mov(first_capture_start, capture_start);
921 }
922 __ B(&start);
923
924 __ Bind(&loop);
925 __ Ldp(capture_end,
926 capture_start,
927 MemOperand(base, -kPointerSize, PostIndex));
928 __ Bind(&start);
929 if (mode_ == UC16) {
930 __ Add(capture_start, input_length, Operand(capture_start, ASR, 1));
931 __ Add(capture_end, input_length, Operand(capture_end, ASR, 1));
932 } else {
933 __ Add(capture_start, input_length, capture_start);
934 __ Add(capture_end, input_length, capture_end);
935 }
936 // The output pointer advances for a possible global match.
937 __ Stp(capture_start,
938 capture_end,
939 MemOperand(output_array(), kPointerSize, PostIndex));
940 __ Sub(x11, x11, 2);
941 __ Cbnz(x11, &loop);
942 }
943 }
944 }
945
946 if (global()) {
947 Register success_counter = w0;
948 Register output_size = x10;
949 // Restart matching if the regular expression is flagged as global.
950
951 // Increment success counter.
952 __ Ldr(success_counter, MemOperand(frame_pointer(), kSuccessCounter));
953 __ Add(success_counter, success_counter, 1);
954 __ Str(success_counter, MemOperand(frame_pointer(), kSuccessCounter));
955
956 // Capture results have been stored, so the number of remaining global
957 // output registers is reduced by the number of stored captures.
958 __ Ldr(output_size, MemOperand(frame_pointer(), kOutputSize));
959 __ Sub(output_size, output_size, num_saved_registers_);
960 // Check whether we have enough room for another set of capture results.
961 __ Cmp(output_size, num_saved_registers_);
962 __ B(lt, &return_w0);
963
964 // The output pointer is already set to the next field in the output
965 // array.
966 // Update output size on the frame before we restart matching.
967 __ Str(output_size, MemOperand(frame_pointer(), kOutputSize));
968
969 if (global_with_zero_length_check()) {
970 // Special case for zero-length matches.
971 __ Cmp(current_input_offset(), first_capture_start);
972 // Not a zero-length match, restart.
973 __ B(ne, &load_char_start_regexp);
974 // Offset from the end is zero if we already reached the end.
975 __ Cbz(current_input_offset(), &return_w0);
976 // Advance current position after a zero-length match.
977 __ Add(current_input_offset(),
978 current_input_offset(),
979 Operand((mode_ == UC16) ? 2 : 1));
980 }
981
982 __ B(&load_char_start_regexp);
983 } else {
984 __ Mov(w0, SUCCESS);
985 }
986 }
987
988 if (exit_label_.is_linked()) {
989 // Exit and return w0
990 __ Bind(&exit_label_);
991 if (global()) {
992 __ Ldr(w0, MemOperand(frame_pointer(), kSuccessCounter));
993 }
994 }
995
996 __ Bind(&return_w0);
997
998 // Set stack pointer back to first register to retain
999 ASSERT(csp.Is(__ StackPointer()));
1000 __ Mov(csp, fp);
1001
1002 // Restore registers.
1003 __ PopCPURegList(registers_to_retain);
1004
1005 __ Ret();
1006
1007 Label exit_with_exception;
1008 // Registers x0 to x7 are used to store the first captures, they need to be
1009 // retained over calls to C++ code.
1010 CPURegList cached_registers(CPURegister::kRegister, kXRegSizeInBits, 0, 7);
1011 ASSERT((cached_registers.Count() * 2) == kNumCachedRegisters);
1012
1013 if (check_preempt_label_.is_linked()) {
1014 __ Bind(&check_preempt_label_);
1015 SaveLinkRegister();
1016 // The cached registers need to be retained.
1017 __ PushCPURegList(cached_registers);
1018 CallCheckStackGuardState(x10);
1019 // Returning from the regexp code restores the stack (csp <- fp)
1020 // so we don't need to drop the link register from it before exiting.
1021 __ Cbnz(w0, &return_w0);
1022 // Reset the cached registers.
1023 __ PopCPURegList(cached_registers);
1024 RestoreLinkRegister();
1025 __ Ret();
1026 }
1027
1028 if (stack_overflow_label_.is_linked()) {
1029 __ Bind(&stack_overflow_label_);
1030 SaveLinkRegister();
1031 // The cached registers need to be retained.
1032 __ PushCPURegList(cached_registers);
1033 // Call GrowStack(backtrack_stackpointer(), &stack_base)
1034 __ Mov(x2, ExternalReference::isolate_address(isolate()));
1035 __ Add(x1, frame_pointer(), kStackBase);
1036 __ Mov(x0, backtrack_stackpointer());
1037 ExternalReference grow_stack =
1038 ExternalReference::re_grow_stack(isolate());
1039 __ CallCFunction(grow_stack, 3);
1040 // If return NULL, we have failed to grow the stack, and
1041 // must exit with a stack-overflow exception.
1042 // Returning from the regexp code restores the stack (csp <- fp)
1043 // so we don't need to drop the link register from it before exiting.
1044 __ Cbz(w0, &exit_with_exception);
1045 // Otherwise use return value as new stack pointer.
1046 __ Mov(backtrack_stackpointer(), x0);
1047 // Reset the cached registers.
1048 __ PopCPURegList(cached_registers);
1049 RestoreLinkRegister();
1050 __ Ret();
1051 }
1052
1053 if (exit_with_exception.is_linked()) {
1054 __ Bind(&exit_with_exception);
1055 __ Mov(w0, EXCEPTION);
1056 __ B(&return_w0);
1057 }
1058
1059 CodeDesc code_desc;
1060 masm_->GetCode(&code_desc);
1061 Handle<Code> code = isolate()->factory()->NewCode(
1062 code_desc, Code::ComputeFlags(Code::REGEXP), masm_->CodeObject());
1063 PROFILE(masm_->isolate(), RegExpCodeCreateEvent(*code, *source));
1064 return Handle<HeapObject>::cast(code);
1065 }
1066
1067
1068 void RegExpMacroAssemblerA64::GoTo(Label* to) {
1069 BranchOrBacktrack(al, to);
1070 }
1071
1072 void RegExpMacroAssemblerA64::IfRegisterGE(int reg,
1073 int comparand,
1074 Label* if_ge) {
1075 Register to_compare = GetRegister(reg, w10);
1076 CompareAndBranchOrBacktrack(to_compare, comparand, ge, if_ge);
1077 }
1078
1079
1080 void RegExpMacroAssemblerA64::IfRegisterLT(int reg,
1081 int comparand,
1082 Label* if_lt) {
1083 Register to_compare = GetRegister(reg, w10);
1084 CompareAndBranchOrBacktrack(to_compare, comparand, lt, if_lt);
1085 }
1086
1087
1088 void RegExpMacroAssemblerA64::IfRegisterEqPos(int reg,
1089 Label* if_eq) {
1090 Register to_compare = GetRegister(reg, w10);
1091 __ Cmp(to_compare, current_input_offset());
1092 BranchOrBacktrack(eq, if_eq);
1093 }
1094
1095 RegExpMacroAssembler::IrregexpImplementation
1096 RegExpMacroAssemblerA64::Implementation() {
1097 return kA64Implementation;
1098 }
1099
1100
1101 void RegExpMacroAssemblerA64::LoadCurrentCharacter(int cp_offset,
1102 Label* on_end_of_input,
1103 bool check_bounds,
1104 int characters) {
1105 // TODO(pielan): Make sure long strings are caught before this, and not
1106 // just asserted in debug mode.
1107 ASSERT(cp_offset >= -1); // ^ and \b can look behind one character.
1108 // Be sane! (And ensure that an int32_t can be used to index the string)
1109 ASSERT(cp_offset < (1<<30));
1110 if (check_bounds) {
1111 CheckPosition(cp_offset + characters - 1, on_end_of_input);
1112 }
1113 LoadCurrentCharacterUnchecked(cp_offset, characters);
1114 }
1115
1116
1117 void RegExpMacroAssemblerA64::PopCurrentPosition() {
1118 Pop(current_input_offset());
1119 }
1120
1121
1122 void RegExpMacroAssemblerA64::PopRegister(int register_index) {
1123 Pop(w10);
1124 StoreRegister(register_index, w10);
1125 }
1126
1127
1128 void RegExpMacroAssemblerA64::PushBacktrack(Label* label) {
1129 if (label->is_bound()) {
1130 int target = label->pos();
1131 __ Mov(w10, target + Code::kHeaderSize - kHeapObjectTag);
1132 } else {
1133 __ Adr(x10, label);
1134 __ Sub(x10, x10, code_pointer());
1135 if (masm_->emit_debug_code()) {
1136 __ Cmp(x10, kWRegMask);
1137 // The code offset has to fit in a W register.
1138 __ Check(ls, kOffsetOutOfRange);
1139 }
1140 }
1141 Push(w10);
1142 CheckStackLimit();
1143 }
1144
1145
1146 void RegExpMacroAssemblerA64::PushCurrentPosition() {
1147 Push(current_input_offset());
1148 }
1149
1150
1151 void RegExpMacroAssemblerA64::PushRegister(int register_index,
1152 StackCheckFlag check_stack_limit) {
1153 Register to_push = GetRegister(register_index, w10);
1154 Push(to_push);
1155 if (check_stack_limit) CheckStackLimit();
1156 }
1157
1158
1159 void RegExpMacroAssemblerA64::ReadCurrentPositionFromRegister(int reg) {
1160 Register cached_register;
1161 RegisterState register_state = GetRegisterState(reg);
1162 switch (register_state) {
1163 case STACKED:
1164 __ Ldr(current_input_offset(), register_location(reg));
1165 break;
1166 case CACHED_LSW:
1167 cached_register = GetCachedRegister(reg);
1168 __ Mov(current_input_offset(), cached_register.W());
1169 break;
1170 case CACHED_MSW:
1171 cached_register = GetCachedRegister(reg);
1172 __ Lsr(current_input_offset().X(), cached_register, kWRegSizeInBits);
1173 break;
1174 default:
1175 UNREACHABLE();
1176 break;
1177 }
1178 }
1179
1180
1181 void RegExpMacroAssemblerA64::ReadStackPointerFromRegister(int reg) {
1182 Register read_from = GetRegister(reg, w10);
1183 __ Ldr(x11, MemOperand(frame_pointer(), kStackBase));
1184 __ Add(backtrack_stackpointer(), x11, Operand(read_from, SXTW));
1185 }
1186
1187
1188 void RegExpMacroAssemblerA64::SetCurrentPositionFromEnd(int by) {
1189 Label after_position;
1190 __ Cmp(current_input_offset(), -by * char_size());
1191 __ B(ge, &after_position);
1192 __ Mov(current_input_offset(), -by * char_size());
1193 // On RegExp code entry (where this operation is used), the character before
1194 // the current position is expected to be already loaded.
1195 // We have advanced the position, so it's safe to read backwards.
1196 LoadCurrentCharacterUnchecked(-1, 1);
1197 __ Bind(&after_position);
1198 }
1199
1200
1201 void RegExpMacroAssemblerA64::SetRegister(int register_index, int to) {
1202 ASSERT(register_index >= num_saved_registers_); // Reserved for positions!
1203 Register set_to = wzr;
1204 if (to != 0) {
1205 set_to = w10;
1206 __ Mov(set_to, to);
1207 }
1208 StoreRegister(register_index, set_to);
1209 }
1210
1211
1212 bool RegExpMacroAssemblerA64::Succeed() {
1213 __ B(&success_label_);
1214 return global();
1215 }
1216
1217
1218 void RegExpMacroAssemblerA64::WriteCurrentPositionToRegister(int reg,
1219 int cp_offset) {
1220 Register position = current_input_offset();
1221 if (cp_offset != 0) {
1222 position = w10;
1223 __ Add(position, current_input_offset(), cp_offset * char_size());
1224 }
1225 StoreRegister(reg, position);
1226 }
1227
1228
1229 void RegExpMacroAssemblerA64::ClearRegisters(int reg_from, int reg_to) {
1230 ASSERT(reg_from <= reg_to);
1231 int num_registers = reg_to - reg_from + 1;
1232
1233 // If the first capture register is cached in a hardware register but not
1234 // aligned on a 64-bit one, we need to clear the first one specifically.
1235 if ((reg_from < kNumCachedRegisters) && ((reg_from % 2) != 0)) {
1236 StoreRegister(reg_from, non_position_value());
1237 num_registers--;
1238 reg_from++;
1239 }
1240
1241 // Clear cached registers in pairs as far as possible.
1242 while ((num_registers >= 2) && (reg_from < kNumCachedRegisters)) {
1243 ASSERT(GetRegisterState(reg_from) == CACHED_LSW);
1244 __ Mov(GetCachedRegister(reg_from), twice_non_position_value());
1245 reg_from += 2;
1246 num_registers -= 2;
1247 }
1248
1249 if ((num_registers % 2) == 1) {
1250 StoreRegister(reg_from, non_position_value());
1251 num_registers--;
1252 reg_from++;
1253 }
1254
1255 if (num_registers > 0) {
1256 // If there are some remaining registers, they are stored on the stack.
1257 ASSERT(reg_from >= kNumCachedRegisters);
1258
1259 // Move down the indexes of the registers on stack to get the correct offset
1260 // in memory.
1261 reg_from -= kNumCachedRegisters;
1262 reg_to -= kNumCachedRegisters;
1263 // We should not unroll the loop for less than 2 registers.
1264 STATIC_ASSERT(kNumRegistersToUnroll > 2);
1265 // We position the base pointer to (reg_from + 1).
1266 int base_offset = kFirstRegisterOnStack -
1267 kWRegSize - (kWRegSize * reg_from);
1268 if (num_registers > kNumRegistersToUnroll) {
1269 Register base = x10;
1270 __ Add(base, frame_pointer(), base_offset);
1271
1272 Label loop;
1273 __ Mov(x11, num_registers);
1274 __ Bind(&loop);
1275 __ Str(twice_non_position_value(),
1276 MemOperand(base, -kPointerSize, PostIndex));
1277 __ Sub(x11, x11, 2);
1278 __ Cbnz(x11, &loop);
1279 } else {
1280 for (int i = reg_from; i <= reg_to; i += 2) {
1281 __ Str(twice_non_position_value(),
1282 MemOperand(frame_pointer(), base_offset));
1283 base_offset -= kWRegSize * 2;
1284 }
1285 }
1286 }
1287 }
1288
1289
1290 void RegExpMacroAssemblerA64::WriteStackPointerToRegister(int reg) {
1291 __ Ldr(x10, MemOperand(frame_pointer(), kStackBase));
1292 __ Sub(x10, backtrack_stackpointer(), x10);
1293 if (masm_->emit_debug_code()) {
1294 __ Cmp(x10, Operand(w10, SXTW));
1295 // The stack offset needs to fit in a W register.
1296 __ Check(eq, kOffsetOutOfRange);
1297 }
1298 StoreRegister(reg, w10);
1299 }
1300
1301
1302 // Helper function for reading a value out of a stack frame.
1303 template <typename T>
1304 static T& frame_entry(Address re_frame, int frame_offset) {
1305 return *reinterpret_cast<T*>(re_frame + frame_offset);
1306 }
1307
1308
1309 int RegExpMacroAssemblerA64::CheckStackGuardState(Address* return_address,
1310 Code* re_code,
1311 Address re_frame,
1312 int start_offset,
1313 const byte** input_start,
1314 const byte** input_end) {
1315 Isolate* isolate = frame_entry<Isolate*>(re_frame, kIsolate);
1316 if (isolate->stack_guard()->IsStackOverflow()) {
1317 isolate->StackOverflow();
1318 return EXCEPTION;
1319 }
1320
1321 // If not real stack overflow the stack guard was used to interrupt
1322 // execution for another purpose.
1323
1324 // If this is a direct call from JavaScript retry the RegExp forcing the call
1325 // through the runtime system. Currently the direct call cannot handle a GC.
1326 if (frame_entry<int>(re_frame, kDirectCall) == 1) {
1327 return RETRY;
1328 }
1329
1330 // Prepare for possible GC.
1331 HandleScope handles(isolate);
1332 Handle<Code> code_handle(re_code);
1333
1334 Handle<String> subject(frame_entry<String*>(re_frame, kInput));
1335
1336 // Current string.
1337 bool is_ascii = subject->IsOneByteRepresentationUnderneath();
1338
1339 ASSERT(re_code->instruction_start() <= *return_address);
1340 ASSERT(*return_address <=
1341 re_code->instruction_start() + re_code->instruction_size());
1342
1343 MaybeObject* result = Execution::HandleStackGuardInterrupt(isolate);
1344
1345 if (*code_handle != re_code) { // Return address no longer valid
1346 int delta = code_handle->address() - re_code->address();
1347 // Overwrite the return address on the stack.
1348 *return_address += delta;
1349 }
1350
1351 if (result->IsException()) {
1352 return EXCEPTION;
1353 }
1354
1355 Handle<String> subject_tmp = subject;
1356 int slice_offset = 0;
1357
1358 // Extract the underlying string and the slice offset.
1359 if (StringShape(*subject_tmp).IsCons()) {
1360 subject_tmp = Handle<String>(ConsString::cast(*subject_tmp)->first());
1361 } else if (StringShape(*subject_tmp).IsSliced()) {
1362 SlicedString* slice = SlicedString::cast(*subject_tmp);
1363 subject_tmp = Handle<String>(slice->parent());
1364 slice_offset = slice->offset();
1365 }
1366
1367 // String might have changed.
1368 if (subject_tmp->IsOneByteRepresentation() != is_ascii) {
1369 // If we changed between an ASCII and an UC16 string, the specialized
1370 // code cannot be used, and we need to restart regexp matching from
1371 // scratch (including, potentially, compiling a new version of the code).
1372 return RETRY;
1373 }
1374
1375 // Otherwise, the content of the string might have moved. It must still
1376 // be a sequential or external string with the same content.
1377 // Update the start and end pointers in the stack frame to the current
1378 // location (whether it has actually moved or not).
1379 ASSERT(StringShape(*subject_tmp).IsSequential() ||
1380 StringShape(*subject_tmp).IsExternal());
1381
1382 // The original start address of the characters to match.
1383 const byte* start_address = *input_start;
1384
1385 // Find the current start address of the same character at the current string
1386 // position.
1387 const byte* new_address = StringCharacterPosition(*subject_tmp,
1388 start_offset + slice_offset);
1389
1390 if (start_address != new_address) {
1391 // If there is a difference, update the object pointer and start and end
1392 // addresses in the RegExp stack frame to match the new value.
1393 const byte* end_address = *input_end;
1394 int byte_length = static_cast<int>(end_address - start_address);
1395 frame_entry<const String*>(re_frame, kInput) = *subject;
1396 *input_start = new_address;
1397 *input_end = new_address + byte_length;
1398 } else if (frame_entry<const String*>(re_frame, kInput) != *subject) {
1399 // Subject string might have been a ConsString that underwent
1400 // short-circuiting during GC. That will not change start_address but
1401 // will change pointer inside the subject handle.
1402 frame_entry<const String*>(re_frame, kInput) = *subject;
1403 }
1404
1405 return 0;
1406 }
1407
1408
1409 void RegExpMacroAssemblerA64::CheckPosition(int cp_offset,
1410 Label* on_outside_input) {
1411 CompareAndBranchOrBacktrack(current_input_offset(),
1412 -cp_offset * char_size(),
1413 ge,
1414 on_outside_input);
1415 }
1416
1417
1418 bool RegExpMacroAssemblerA64::CanReadUnaligned() {
1419 // TODO(pielan): See whether or not we should disable unaligned accesses.
1420 return !slow_safe();
1421 }
1422
1423
1424 // Private methods:
1425
1426 void RegExpMacroAssemblerA64::CallCheckStackGuardState(Register scratch) {
1427 // Allocate space on the stack to store the return address. The
1428 // CheckStackGuardState C++ function will override it if the code
1429 // moved. Allocate extra space for 2 arguments passed by pointers.
1430 // AAPCS64 requires the stack to be 16 byte aligned.
1431 int alignment = masm_->ActivationFrameAlignment();
1432 ASSERT_EQ(alignment % 16, 0);
1433 int align_mask = (alignment / kXRegSize) - 1;
1434 int xreg_to_claim = (3 + align_mask) & ~align_mask;
1435
1436 ASSERT(csp.Is(__ StackPointer()));
1437 __ Claim(xreg_to_claim);
1438
1439 // CheckStackGuardState needs the end and start addresses of the input string.
1440 __ Poke(input_end(), 2 * kPointerSize);
1441 __ Add(x5, csp, 2 * kPointerSize);
1442 __ Poke(input_start(), kPointerSize);
1443 __ Add(x4, csp, kPointerSize);
1444
1445 __ Mov(w3, start_offset());
1446 // RegExp code frame pointer.
1447 __ Mov(x2, frame_pointer());
1448 // Code* of self.
1449 __ Mov(x1, Operand(masm_->CodeObject()));
1450
1451 // We need to pass a pointer to the return address as first argument.
1452 // The DirectCEntry stub will place the return address on the stack before
1453 // calling so the stack pointer will point to it.
1454 __ Mov(x0, csp);
1455
1456 ExternalReference check_stack_guard_state =
1457 ExternalReference::re_check_stack_guard_state(isolate());
1458 __ Mov(scratch, check_stack_guard_state);
1459 DirectCEntryStub stub;
1460 stub.GenerateCall(masm_, scratch);
1461
1462 // The input string may have been moved in memory, we need to reload it.
1463 __ Peek(input_start(), kPointerSize);
1464 __ Peek(input_end(), 2 * kPointerSize);
1465
1466 ASSERT(csp.Is(__ StackPointer()));
1467 __ Drop(xreg_to_claim);
1468
1469 // Reload the Code pointer.
1470 __ Mov(code_pointer(), Operand(masm_->CodeObject()));
1471 }
1472
1473 void RegExpMacroAssemblerA64::BranchOrBacktrack(Condition condition,
1474 Label* to) {
1475 if (condition == al) { // Unconditional.
1476 if (to == NULL) {
1477 Backtrack();
1478 return;
1479 }
1480 __ B(to);
1481 return;
1482 }
1483 if (to == NULL) {
1484 to = &backtrack_label_;
1485 }
1486 // TODO(ulan): do direct jump when jump distance is known and fits in imm19.
1487 Condition inverted_condition = InvertCondition(condition);
1488 Label no_branch;
1489 __ B(inverted_condition, &no_branch);
1490 __ B(to);
1491 __ Bind(&no_branch);
1492 }
1493
1494 void RegExpMacroAssemblerA64::CompareAndBranchOrBacktrack(Register reg,
1495 int immediate,
1496 Condition condition,
1497 Label* to) {
1498 if ((immediate == 0) && ((condition == eq) || (condition == ne))) {
1499 if (to == NULL) {
1500 to = &backtrack_label_;
1501 }
1502 // TODO(ulan): do direct jump when jump distance is known and fits in imm19.
1503 Label no_branch;
1504 if (condition == eq) {
1505 __ Cbnz(reg, &no_branch);
1506 } else {
1507 __ Cbz(reg, &no_branch);
1508 }
1509 __ B(to);
1510 __ Bind(&no_branch);
1511 } else {
1512 __ Cmp(reg, immediate);
1513 BranchOrBacktrack(condition, to);
1514 }
1515 }
1516
1517
1518 void RegExpMacroAssemblerA64::CheckPreemption() {
1519 // Check for preemption.
1520 ExternalReference stack_limit =
1521 ExternalReference::address_of_stack_limit(isolate());
1522 __ Mov(x10, stack_limit);
1523 __ Ldr(x10, MemOperand(x10));
1524 ASSERT(csp.Is(__ StackPointer()));
1525 __ Cmp(csp, x10);
1526 CallIf(&check_preempt_label_, ls);
1527 }
1528
1529
1530 void RegExpMacroAssemblerA64::CheckStackLimit() {
1531 ExternalReference stack_limit =
1532 ExternalReference::address_of_regexp_stack_limit(isolate());
1533 __ Mov(x10, stack_limit);
1534 __ Ldr(x10, MemOperand(x10));
1535 __ Cmp(backtrack_stackpointer(), x10);
1536 CallIf(&stack_overflow_label_, ls);
1537 }
1538
1539
1540 void RegExpMacroAssemblerA64::Push(Register source) {
1541 ASSERT(source.Is32Bits());
1542 ASSERT(!source.is(backtrack_stackpointer()));
1543 __ Str(source,
1544 MemOperand(backtrack_stackpointer(),
1545 -static_cast<int>(kWRegSize),
1546 PreIndex));
1547 }
1548
1549
1550 void RegExpMacroAssemblerA64::Pop(Register target) {
1551 ASSERT(target.Is32Bits());
1552 ASSERT(!target.is(backtrack_stackpointer()));
1553 __ Ldr(target,
1554 MemOperand(backtrack_stackpointer(), kWRegSize, PostIndex));
1555 }
1556
1557
1558 Register RegExpMacroAssemblerA64::GetCachedRegister(int register_index) {
1559 ASSERT(register_index < kNumCachedRegisters);
1560 return Register::Create(register_index / 2, kXRegSizeInBits);
1561 }
1562
1563
1564 Register RegExpMacroAssemblerA64::GetRegister(int register_index,
1565 Register maybe_result) {
1566 ASSERT(maybe_result.Is32Bits());
1567 ASSERT(register_index >= 0);
1568 if (num_registers_ <= register_index) {
1569 num_registers_ = register_index + 1;
1570 }
1571 Register result;
1572 RegisterState register_state = GetRegisterState(register_index);
1573 switch (register_state) {
1574 case STACKED:
1575 __ Ldr(maybe_result, register_location(register_index));
1576 result = maybe_result;
1577 break;
1578 case CACHED_LSW:
1579 result = GetCachedRegister(register_index).W();
1580 break;
1581 case CACHED_MSW:
1582 __ Lsr(maybe_result.X(), GetCachedRegister(register_index),
1583 kWRegSizeInBits);
1584 result = maybe_result;
1585 break;
1586 default:
1587 UNREACHABLE();
1588 break;
1589 }
1590 ASSERT(result.Is32Bits());
1591 return result;
1592 }
1593
1594
1595 void RegExpMacroAssemblerA64::StoreRegister(int register_index,
1596 Register source) {
1597 ASSERT(source.Is32Bits());
1598 ASSERT(register_index >= 0);
1599 if (num_registers_ <= register_index) {
1600 num_registers_ = register_index + 1;
1601 }
1602
1603 Register cached_register;
1604 RegisterState register_state = GetRegisterState(register_index);
1605 switch (register_state) {
1606 case STACKED:
1607 __ Str(source, register_location(register_index));
1608 break;
1609 case CACHED_LSW:
1610 cached_register = GetCachedRegister(register_index);
1611 if (!source.Is(cached_register.W())) {
1612 __ Bfi(cached_register, source.X(), 0, kWRegSizeInBits);
1613 }
1614 break;
1615 case CACHED_MSW:
1616 cached_register = GetCachedRegister(register_index);
1617 __ Bfi(cached_register, source.X(), kWRegSizeInBits, kWRegSizeInBits);
1618 break;
1619 default:
1620 UNREACHABLE();
1621 break;
1622 }
1623 }
1624
1625
1626 void RegExpMacroAssemblerA64::CallIf(Label* to, Condition condition) {
1627 Label skip_call;
1628 if (condition != al) __ B(&skip_call, InvertCondition(condition));
1629 __ Bl(to);
1630 __ Bind(&skip_call);
1631 }
1632
1633
1634 void RegExpMacroAssemblerA64::RestoreLinkRegister() {
1635 ASSERT(csp.Is(__ StackPointer()));
1636 __ Pop(lr, xzr);
1637 __ Add(lr, lr, Operand(masm_->CodeObject()));
1638 }
1639
1640
1641 void RegExpMacroAssemblerA64::SaveLinkRegister() {
1642 ASSERT(csp.Is(__ StackPointer()));
1643 __ Sub(lr, lr, Operand(masm_->CodeObject()));
1644 __ Push(xzr, lr);
1645 }
1646
1647
1648 MemOperand RegExpMacroAssemblerA64::register_location(int register_index) {
1649 ASSERT(register_index < (1<<30));
1650 ASSERT(register_index >= kNumCachedRegisters);
1651 if (num_registers_ <= register_index) {
1652 num_registers_ = register_index + 1;
1653 }
1654 register_index -= kNumCachedRegisters;
1655 int offset = kFirstRegisterOnStack - register_index * kWRegSize;
1656 return MemOperand(frame_pointer(), offset);
1657 }
1658
1659 MemOperand RegExpMacroAssemblerA64::capture_location(int register_index,
1660 Register scratch) {
1661 ASSERT(register_index < (1<<30));
1662 ASSERT(register_index < num_saved_registers_);
1663 ASSERT(register_index >= kNumCachedRegisters);
1664 ASSERT_EQ(register_index % 2, 0);
1665 register_index -= kNumCachedRegisters;
1666 int offset = kFirstCaptureOnStack - register_index * kWRegSize;
1667 // capture_location is used with Stp instructions to load/store 2 registers.
1668 // The immediate field in the encoding is limited to 7 bits (signed).
1669 if (is_int7(offset)) {
1670 return MemOperand(frame_pointer(), offset);
1671 } else {
1672 __ Add(scratch, frame_pointer(), offset);
1673 return MemOperand(scratch);
1674 }
1675 }
1676
1677 void RegExpMacroAssemblerA64::LoadCurrentCharacterUnchecked(int cp_offset,
1678 int characters) {
1679 Register offset = current_input_offset();
1680
1681 // The ldr, str, ldrh, strh instructions can do unaligned accesses, if the CPU
1682 // and the operating system running on the target allow it.
1683 // If unaligned load/stores are not supported then this function must only
1684 // be used to load a single character at a time.
1685
1686 // ARMv8 supports unaligned accesses but V8 or the kernel can decide to
1687 // disable it.
1688 // TODO(pielan): See whether or not we should disable unaligned accesses.
1689 if (!CanReadUnaligned()) {
1690 ASSERT(characters == 1);
1691 }
1692
1693 if (cp_offset != 0) {
1694 if (masm_->emit_debug_code()) {
1695 __ Mov(x10, cp_offset * char_size());
1696 __ Add(x10, x10, Operand(current_input_offset(), SXTW));
1697 __ Cmp(x10, Operand(w10, SXTW));
1698 // The offset needs to fit in a W register.
1699 __ Check(eq, kOffsetOutOfRange);
1700 } else {
1701 __ Add(w10, current_input_offset(), cp_offset * char_size());
1702 }
1703 offset = w10;
1704 }
1705
1706 if (mode_ == ASCII) {
1707 if (characters == 4) {
1708 __ Ldr(current_character(), MemOperand(input_end(), offset, SXTW));
1709 } else if (characters == 2) {
1710 __ Ldrh(current_character(), MemOperand(input_end(), offset, SXTW));
1711 } else {
1712 ASSERT(characters == 1);
1713 __ Ldrb(current_character(), MemOperand(input_end(), offset, SXTW));
1714 }
1715 } else {
1716 ASSERT(mode_ == UC16);
1717 if (characters == 2) {
1718 __ Ldr(current_character(), MemOperand(input_end(), offset, SXTW));
1719 } else {
1720 ASSERT(characters == 1);
1721 __ Ldrh(current_character(), MemOperand(input_end(), offset, SXTW));
1722 }
1723 }
1724 }
1725
1726 #endif // V8_INTERPRETED_REGEXP
1727
1728 }} // namespace v8::internal
1729
1730 #endif // V8_TARGET_ARCH_A64
OLDNEW
« no previous file with comments | « src/a64/regexp-macro-assembler-a64.h ('k') | src/a64/simulator-a64.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698