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

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

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