blob: 6fafdfb4ad43cff4ba7068aa22006da4d414af21 [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// Copyright 2012 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#if V8_TARGET_ARCH_ARM
6
7#include "src/regexp/arm/regexp-macro-assembler-arm.h"
8
9#include "src/code-stubs.h"
10#include "src/log.h"
11#include "src/macro-assembler.h"
12#include "src/profiler/cpu-profiler.h"
13#include "src/regexp/regexp-macro-assembler.h"
14#include "src/regexp/regexp-stack.h"
15#include "src/unicode.h"
16
17namespace v8 {
18namespace internal {
19
20#ifndef V8_INTERPRETED_REGEXP
21/*
22 * This assembler uses the following register assignment convention
23 * - r4 : Temporarily stores the index of capture start after a matching pass
24 * for a global regexp.
25 * - r5 : Pointer to current code object (Code*) including heap object tag.
26 * - r6 : Current position in input, as negative offset from end of string.
27 * Please notice that this is the byte offset, not the character offset!
28 * - r7 : Currently loaded character. Must be loaded using
29 * LoadCurrentCharacter before using any of the dispatch methods.
30 * - r8 : Points to tip of backtrack stack
31 * - r9 : Unused, might be used by C code and expected unchanged.
32 * - r10 : End of input (points to byte after last character in input).
33 * - r11 : Frame pointer. Used to access arguments, local variables and
34 * RegExp registers.
35 * - r12 : IP register, used by assembler. Very volatile.
36 * - r13/sp : Points to tip of C stack.
37 *
38 * The remaining registers are free for computations.
39 * Each call to a public method should retain this convention.
40 *
41 * The stack will have the following structure:
42 * - fp[56] Isolate* isolate (address of the current isolate)
43 * - fp[52] direct_call (if 1, direct call from JavaScript code,
44 * if 0, call through the runtime system).
45 * - fp[48] stack_area_base (high end of the memory area to use as
46 * backtracking stack).
47 * - fp[44] capture array size (may fit multiple sets of matches)
48 * - fp[40] int* capture_array (int[num_saved_registers_], for output).
49 * - fp[36] secondary link/return address used by native call.
50 * --- sp when called ---
51 * - fp[32] return address (lr).
52 * - fp[28] old frame pointer (r11).
53 * - fp[0..24] backup of registers r4..r10.
54 * --- frame pointer ----
55 * - fp[-4] end of input (address of end of string).
56 * - fp[-8] start of input (address of first character in string).
57 * - fp[-12] start index (character index of start).
58 * - fp[-16] void* input_string (location of a handle containing the string).
59 * - fp[-20] success counter (only for global regexps to count matches).
60 * - fp[-24] Offset of location before start of input (effectively character
61 * string start - 1). Used to initialize capture registers to a
62 * non-position.
63 * - fp[-28] At start (if 1, we are starting at the start of the
64 * string, otherwise 0)
65 * - fp[-32] register 0 (Only positions must be stored in the first
66 * - register 1 num_saved_registers_ registers)
67 * - ...
68 * - register num_registers-1
69 * --- sp ---
70 *
71 * The first num_saved_registers_ registers are initialized to point to
72 * "character -1" in the string (i.e., char_size() bytes before the first
73 * character of the string). The remaining registers start out as garbage.
74 *
75 * The data up to the return address must be placed there by the calling
76 * code and the remaining arguments are passed in registers, e.g. by calling the
77 * code entry as cast to a function with the signature:
78 * int (*match)(String* input_string,
79 * int start_index,
80 * Address start,
81 * Address end,
82 * Address secondary_return_address, // Only used by native call.
83 * int* capture_output_array,
84 * byte* stack_area_base,
85 * bool direct_call = false)
86 * The call is performed by NativeRegExpMacroAssembler::Execute()
87 * (in regexp-macro-assembler.cc) via the CALL_GENERATED_REGEXP_CODE macro
88 * in arm/simulator-arm.h.
89 * When calling as a non-direct call (i.e., from C++ code), the return address
90 * area is overwritten with the LR register by the RegExp code. When doing a
91 * direct call from generated code, the return address is placed there by
92 * the calling code, as in a normal exit frame.
93 */
94
95#define __ ACCESS_MASM(masm_)
96
97RegExpMacroAssemblerARM::RegExpMacroAssemblerARM(Isolate* isolate, Zone* zone,
98 Mode mode,
99 int registers_to_save)
100 : NativeRegExpMacroAssembler(isolate, zone),
101 masm_(new MacroAssembler(isolate, NULL, kRegExpCodeSize,
102 CodeObjectRequired::kYes)),
103 mode_(mode),
104 num_registers_(registers_to_save),
105 num_saved_registers_(registers_to_save),
106 entry_label_(),
107 start_label_(),
108 success_label_(),
109 backtrack_label_(),
110 exit_label_() {
111 DCHECK_EQ(0, registers_to_save % 2);
112 __ jmp(&entry_label_); // We'll write the entry code later.
113 __ bind(&start_label_); // And then continue from here.
114}
115
116
117RegExpMacroAssemblerARM::~RegExpMacroAssemblerARM() {
118 delete masm_;
119 // Unuse labels in case we throw away the assembler without calling GetCode.
120 entry_label_.Unuse();
121 start_label_.Unuse();
122 success_label_.Unuse();
123 backtrack_label_.Unuse();
124 exit_label_.Unuse();
125 check_preempt_label_.Unuse();
126 stack_overflow_label_.Unuse();
127}
128
129
130int RegExpMacroAssemblerARM::stack_limit_slack() {
131 return RegExpStack::kStackLimitSlack;
132}
133
134
135void RegExpMacroAssemblerARM::AdvanceCurrentPosition(int by) {
136 if (by != 0) {
137 __ add(current_input_offset(),
138 current_input_offset(), Operand(by * char_size()));
139 }
140}
141
142
143void RegExpMacroAssemblerARM::AdvanceRegister(int reg, int by) {
144 DCHECK(reg >= 0);
145 DCHECK(reg < num_registers_);
146 if (by != 0) {
147 __ ldr(r0, register_location(reg));
148 __ add(r0, r0, Operand(by));
149 __ str(r0, register_location(reg));
150 }
151}
152
153
154void RegExpMacroAssemblerARM::Backtrack() {
155 CheckPreemption();
156 // Pop Code* offset from backtrack stack, add Code* and jump to location.
157 Pop(r0);
158 __ add(pc, r0, Operand(code_pointer()));
159}
160
161
162void RegExpMacroAssemblerARM::Bind(Label* label) {
163 __ bind(label);
164}
165
166
167void RegExpMacroAssemblerARM::CheckCharacter(uint32_t c, Label* on_equal) {
168 __ cmp(current_character(), Operand(c));
169 BranchOrBacktrack(eq, on_equal);
170}
171
172
173void RegExpMacroAssemblerARM::CheckCharacterGT(uc16 limit, Label* on_greater) {
174 __ cmp(current_character(), Operand(limit));
175 BranchOrBacktrack(gt, on_greater);
176}
177
178
179void RegExpMacroAssemblerARM::CheckAtStart(Label* on_at_start) {
180 __ ldr(r1, MemOperand(frame_pointer(), kStringStartMinusOne));
181 __ add(r0, current_input_offset(), Operand(-char_size()));
182 __ cmp(r0, r1);
183 BranchOrBacktrack(eq, on_at_start);
184}
185
186
187void RegExpMacroAssemblerARM::CheckNotAtStart(int cp_offset,
188 Label* on_not_at_start) {
189 __ ldr(r1, MemOperand(frame_pointer(), kStringStartMinusOne));
190 __ add(r0, current_input_offset(),
191 Operand(-char_size() + cp_offset * char_size()));
192 __ cmp(r0, r1);
193 BranchOrBacktrack(ne, on_not_at_start);
194}
195
196
197void RegExpMacroAssemblerARM::CheckCharacterLT(uc16 limit, Label* on_less) {
198 __ cmp(current_character(), Operand(limit));
199 BranchOrBacktrack(lt, on_less);
200}
201
202
203void RegExpMacroAssemblerARM::CheckGreedyLoop(Label* on_equal) {
204 __ ldr(r0, MemOperand(backtrack_stackpointer(), 0));
205 __ cmp(current_input_offset(), r0);
206 __ add(backtrack_stackpointer(),
207 backtrack_stackpointer(), Operand(kPointerSize), LeaveCC, eq);
208 BranchOrBacktrack(eq, on_equal);
209}
210
211
212void RegExpMacroAssemblerARM::CheckNotBackReferenceIgnoreCase(
213 int start_reg, bool read_backward, Label* on_no_match) {
214 Label fallthrough;
215 __ ldr(r0, register_location(start_reg)); // Index of start of capture
216 __ ldr(r1, register_location(start_reg + 1)); // Index of end of capture
217 __ sub(r1, r1, r0, SetCC); // Length of capture.
218
219 // At this point, the capture registers are either both set or both cleared.
220 // If the capture length is zero, then the capture is either empty or cleared.
221 // Fall through in both cases.
222 __ b(eq, &fallthrough);
223
224 // Check that there are enough characters left in the input.
225 if (read_backward) {
226 __ ldr(r3, MemOperand(frame_pointer(), kStringStartMinusOne));
227 __ add(r3, r3, r1);
228 __ cmp(current_input_offset(), r3);
229 BranchOrBacktrack(le, on_no_match);
230 } else {
231 __ cmn(r1, Operand(current_input_offset()));
232 BranchOrBacktrack(gt, on_no_match);
233 }
234
235 if (mode_ == LATIN1) {
236 Label success;
237 Label fail;
238 Label loop_check;
239
240 // r0 - offset of start of capture
241 // r1 - length of capture
242 __ add(r0, r0, end_of_input_address());
243 __ add(r2, end_of_input_address(), current_input_offset());
244 if (read_backward) {
245 __ sub(r2, r2, r1); // Offset by length when matching backwards.
246 }
247 __ add(r1, r0, r1);
248
249 // r0 - Address of start of capture.
250 // r1 - Address of end of capture
251 // r2 - Address of current input position.
252
253 Label loop;
254 __ bind(&loop);
255 __ ldrb(r3, MemOperand(r0, char_size(), PostIndex));
256 __ ldrb(r4, MemOperand(r2, char_size(), PostIndex));
257 __ cmp(r4, r3);
258 __ b(eq, &loop_check);
259
260 // Mismatch, try case-insensitive match (converting letters to lower-case).
261 __ orr(r3, r3, Operand(0x20)); // Convert capture character to lower-case.
262 __ orr(r4, r4, Operand(0x20)); // Also convert input character.
263 __ cmp(r4, r3);
264 __ b(ne, &fail);
265 __ sub(r3, r3, Operand('a'));
266 __ cmp(r3, Operand('z' - 'a')); // Is r3 a lowercase letter?
267 __ b(ls, &loop_check); // In range 'a'-'z'.
268 // Latin-1: Check for values in range [224,254] but not 247.
269 __ sub(r3, r3, Operand(224 - 'a'));
270 __ cmp(r3, Operand(254 - 224));
271 __ b(hi, &fail); // Weren't Latin-1 letters.
272 __ cmp(r3, Operand(247 - 224)); // Check for 247.
273 __ b(eq, &fail);
274
275 __ bind(&loop_check);
276 __ cmp(r0, r1);
277 __ b(lt, &loop);
278 __ jmp(&success);
279
280 __ bind(&fail);
281 BranchOrBacktrack(al, on_no_match);
282
283 __ bind(&success);
284 // Compute new value of character position after the matched part.
285 __ sub(current_input_offset(), r2, end_of_input_address());
286 if (read_backward) {
287 __ ldr(r0, register_location(start_reg)); // Index of start of capture
288 __ ldr(r1, register_location(start_reg + 1)); // Index of end of capture
289 __ add(current_input_offset(), current_input_offset(), r0);
290 __ sub(current_input_offset(), current_input_offset(), r1);
291 }
292 } else {
293 DCHECK(mode_ == UC16);
294 int argument_count = 4;
295 __ PrepareCallCFunction(argument_count, r2);
296
297 // r0 - offset of start of capture
298 // r1 - length of capture
299
300 // Put arguments into arguments registers.
301 // Parameters are
302 // r0: Address byte_offset1 - Address captured substring's start.
303 // r1: Address byte_offset2 - Address of current character position.
304 // r2: size_t byte_length - length of capture in bytes(!)
305 // r3: Isolate* isolate
306
307 // Address of start of capture.
308 __ add(r0, r0, Operand(end_of_input_address()));
309 // Length of capture.
310 __ mov(r2, Operand(r1));
311 // Save length in callee-save register for use on return.
312 __ mov(r4, Operand(r1));
313 // Address of current input position.
314 __ add(r1, current_input_offset(), end_of_input_address());
315 if (read_backward) {
316 __ sub(r1, r1, r4);
317 }
318 // Isolate.
319 __ mov(r3, Operand(ExternalReference::isolate_address(isolate())));
320
321 {
322 AllowExternalCallThatCantCauseGC scope(masm_);
323 ExternalReference function =
324 ExternalReference::re_case_insensitive_compare_uc16(isolate());
325 __ CallCFunction(function, argument_count);
326 }
327
328 // Check if function returned non-zero for success or zero for failure.
329 __ cmp(r0, Operand::Zero());
330 BranchOrBacktrack(eq, on_no_match);
331
332 // On success, advance position by length of capture.
333 if (read_backward) {
334 __ sub(current_input_offset(), current_input_offset(), r4);
335 } else {
336 __ add(current_input_offset(), current_input_offset(), r4);
337 }
338 }
339
340 __ bind(&fallthrough);
341}
342
343
344void RegExpMacroAssemblerARM::CheckNotBackReference(int start_reg,
345 bool read_backward,
346 Label* on_no_match) {
347 Label fallthrough;
348 Label success;
349
350 // Find length of back-referenced capture.
351 __ ldr(r0, register_location(start_reg));
352 __ ldr(r1, register_location(start_reg + 1));
353 __ sub(r1, r1, r0, SetCC); // Length to check.
354
355 // At this point, the capture registers are either both set or both cleared.
356 // If the capture length is zero, then the capture is either empty or cleared.
357 // Fall through in both cases.
358 __ b(eq, &fallthrough);
359
360 // Check that there are enough characters left in the input.
361 if (read_backward) {
362 __ ldr(r3, MemOperand(frame_pointer(), kStringStartMinusOne));
363 __ add(r3, r3, r1);
364 __ cmp(current_input_offset(), r3);
365 BranchOrBacktrack(lt, on_no_match);
366 } else {
367 __ cmn(r1, Operand(current_input_offset()));
368 BranchOrBacktrack(gt, on_no_match);
369 }
370
371 // r0 - offset of start of capture
372 // r1 - length of capture
373 __ add(r0, r0, end_of_input_address());
374 __ add(r2, end_of_input_address(), current_input_offset());
375 if (read_backward) {
376 __ sub(r2, r2, r1); // Offset by length when matching backwards.
377 }
378 __ add(r1, r0, r1);
379
380 Label loop;
381 __ bind(&loop);
382 if (mode_ == LATIN1) {
383 __ ldrb(r3, MemOperand(r0, char_size(), PostIndex));
384 __ ldrb(r4, MemOperand(r2, char_size(), PostIndex));
385 } else {
386 DCHECK(mode_ == UC16);
387 __ ldrh(r3, MemOperand(r0, char_size(), PostIndex));
388 __ ldrh(r4, MemOperand(r2, char_size(), PostIndex));
389 }
390 __ cmp(r3, r4);
391 BranchOrBacktrack(ne, on_no_match);
392 __ cmp(r0, r1);
393 __ b(lt, &loop);
394
395 // Move current character position to position after match.
396 __ sub(current_input_offset(), r2, end_of_input_address());
397 if (read_backward) {
398 __ ldr(r0, register_location(start_reg)); // Index of start of capture
399 __ ldr(r1, register_location(start_reg + 1)); // Index of end of capture
400 __ add(current_input_offset(), current_input_offset(), r0);
401 __ sub(current_input_offset(), current_input_offset(), r1);
402 }
403
404 __ bind(&fallthrough);
405}
406
407
408void RegExpMacroAssemblerARM::CheckNotCharacter(unsigned c,
409 Label* on_not_equal) {
410 __ cmp(current_character(), Operand(c));
411 BranchOrBacktrack(ne, on_not_equal);
412}
413
414
415void RegExpMacroAssemblerARM::CheckCharacterAfterAnd(uint32_t c,
416 uint32_t mask,
417 Label* on_equal) {
418 if (c == 0) {
419 __ tst(current_character(), Operand(mask));
420 } else {
421 __ and_(r0, current_character(), Operand(mask));
422 __ cmp(r0, Operand(c));
423 }
424 BranchOrBacktrack(eq, on_equal);
425}
426
427
428void RegExpMacroAssemblerARM::CheckNotCharacterAfterAnd(unsigned c,
429 unsigned mask,
430 Label* on_not_equal) {
431 if (c == 0) {
432 __ tst(current_character(), Operand(mask));
433 } else {
434 __ and_(r0, current_character(), Operand(mask));
435 __ cmp(r0, Operand(c));
436 }
437 BranchOrBacktrack(ne, on_not_equal);
438}
439
440
441void RegExpMacroAssemblerARM::CheckNotCharacterAfterMinusAnd(
442 uc16 c,
443 uc16 minus,
444 uc16 mask,
445 Label* on_not_equal) {
446 DCHECK(minus < String::kMaxUtf16CodeUnit);
447 __ sub(r0, current_character(), Operand(minus));
448 __ and_(r0, r0, Operand(mask));
449 __ cmp(r0, Operand(c));
450 BranchOrBacktrack(ne, on_not_equal);
451}
452
453
454void RegExpMacroAssemblerARM::CheckCharacterInRange(
455 uc16 from,
456 uc16 to,
457 Label* on_in_range) {
458 __ sub(r0, current_character(), Operand(from));
459 __ cmp(r0, Operand(to - from));
460 BranchOrBacktrack(ls, on_in_range); // Unsigned lower-or-same condition.
461}
462
463
464void RegExpMacroAssemblerARM::CheckCharacterNotInRange(
465 uc16 from,
466 uc16 to,
467 Label* on_not_in_range) {
468 __ sub(r0, current_character(), Operand(from));
469 __ cmp(r0, Operand(to - from));
470 BranchOrBacktrack(hi, on_not_in_range); // Unsigned higher condition.
471}
472
473
474void RegExpMacroAssemblerARM::CheckBitInTable(
475 Handle<ByteArray> table,
476 Label* on_bit_set) {
477 __ mov(r0, Operand(table));
478 if (mode_ != LATIN1 || kTableMask != String::kMaxOneByteCharCode) {
479 __ and_(r1, current_character(), Operand(kTableSize - 1));
480 __ add(r1, r1, Operand(ByteArray::kHeaderSize - kHeapObjectTag));
481 } else {
482 __ add(r1,
483 current_character(),
484 Operand(ByteArray::kHeaderSize - kHeapObjectTag));
485 }
486 __ ldrb(r0, MemOperand(r0, r1));
487 __ cmp(r0, Operand::Zero());
488 BranchOrBacktrack(ne, on_bit_set);
489}
490
491
492bool RegExpMacroAssemblerARM::CheckSpecialCharacterClass(uc16 type,
493 Label* on_no_match) {
494 // Range checks (c in min..max) are generally implemented by an unsigned
495 // (c - min) <= (max - min) check
496 switch (type) {
497 case 's':
498 // Match space-characters
499 if (mode_ == LATIN1) {
500 // One byte space characters are '\t'..'\r', ' ' and \u00a0.
501 Label success;
502 __ cmp(current_character(), Operand(' '));
503 __ b(eq, &success);
504 // Check range 0x09..0x0d
505 __ sub(r0, current_character(), Operand('\t'));
506 __ cmp(r0, Operand('\r' - '\t'));
507 __ b(ls, &success);
508 // \u00a0 (NBSP).
509 __ cmp(r0, Operand(0x00a0 - '\t'));
510 BranchOrBacktrack(ne, on_no_match);
511 __ bind(&success);
512 return true;
513 }
514 return false;
515 case 'S':
516 // The emitted code for generic character classes is good enough.
517 return false;
518 case 'd':
519 // Match ASCII digits ('0'..'9')
520 __ sub(r0, current_character(), Operand('0'));
521 __ cmp(r0, Operand('9' - '0'));
522 BranchOrBacktrack(hi, on_no_match);
523 return true;
524 case 'D':
525 // Match non ASCII-digits
526 __ sub(r0, current_character(), Operand('0'));
527 __ cmp(r0, Operand('9' - '0'));
528 BranchOrBacktrack(ls, on_no_match);
529 return true;
530 case '.': {
531 // Match non-newlines (not 0x0a('\n'), 0x0d('\r'), 0x2028 and 0x2029)
532 __ eor(r0, current_character(), Operand(0x01));
533 // See if current character is '\n'^1 or '\r'^1, i.e., 0x0b or 0x0c
534 __ sub(r0, r0, Operand(0x0b));
535 __ cmp(r0, Operand(0x0c - 0x0b));
536 BranchOrBacktrack(ls, on_no_match);
537 if (mode_ == UC16) {
538 // Compare original value to 0x2028 and 0x2029, using the already
539 // computed (current_char ^ 0x01 - 0x0b). I.e., check for
540 // 0x201d (0x2028 - 0x0b) or 0x201e.
541 __ sub(r0, r0, Operand(0x2028 - 0x0b));
542 __ cmp(r0, Operand(1));
543 BranchOrBacktrack(ls, on_no_match);
544 }
545 return true;
546 }
547 case 'n': {
548 // Match newlines (0x0a('\n'), 0x0d('\r'), 0x2028 and 0x2029)
549 __ eor(r0, current_character(), Operand(0x01));
550 // See if current character is '\n'^1 or '\r'^1, i.e., 0x0b or 0x0c
551 __ sub(r0, r0, Operand(0x0b));
552 __ cmp(r0, Operand(0x0c - 0x0b));
553 if (mode_ == LATIN1) {
554 BranchOrBacktrack(hi, on_no_match);
555 } else {
556 Label done;
557 __ b(ls, &done);
558 // Compare original value to 0x2028 and 0x2029, using the already
559 // computed (current_char ^ 0x01 - 0x0b). I.e., check for
560 // 0x201d (0x2028 - 0x0b) or 0x201e.
561 __ sub(r0, r0, Operand(0x2028 - 0x0b));
562 __ cmp(r0, Operand(1));
563 BranchOrBacktrack(hi, on_no_match);
564 __ bind(&done);
565 }
566 return true;
567 }
568 case 'w': {
569 if (mode_ != LATIN1) {
570 // Table is 256 entries, so all Latin1 characters can be tested.
571 __ cmp(current_character(), Operand('z'));
572 BranchOrBacktrack(hi, on_no_match);
573 }
574 ExternalReference map = ExternalReference::re_word_character_map();
575 __ mov(r0, Operand(map));
576 __ ldrb(r0, MemOperand(r0, current_character()));
577 __ cmp(r0, Operand::Zero());
578 BranchOrBacktrack(eq, on_no_match);
579 return true;
580 }
581 case 'W': {
582 Label done;
583 if (mode_ != LATIN1) {
584 // Table is 256 entries, so all Latin1 characters can be tested.
585 __ cmp(current_character(), Operand('z'));
586 __ b(hi, &done);
587 }
588 ExternalReference map = ExternalReference::re_word_character_map();
589 __ mov(r0, Operand(map));
590 __ ldrb(r0, MemOperand(r0, current_character()));
591 __ cmp(r0, Operand::Zero());
592 BranchOrBacktrack(ne, on_no_match);
593 if (mode_ != LATIN1) {
594 __ bind(&done);
595 }
596 return true;
597 }
598 case '*':
599 // Match any character.
600 return true;
601 // No custom implementation (yet): s(UC16), S(UC16).
602 default:
603 return false;
604 }
605}
606
607
608void RegExpMacroAssemblerARM::Fail() {
609 __ mov(r0, Operand(FAILURE));
610 __ jmp(&exit_label_);
611}
612
613
614Handle<HeapObject> RegExpMacroAssemblerARM::GetCode(Handle<String> source) {
615 Label return_r0;
616 // Finalize code - write the entry point code now we know how many
617 // registers we need.
618
619 // Entry code:
620 __ bind(&entry_label_);
621
622 // Tell the system that we have a stack frame. Because the type is MANUAL, no
623 // is generated.
624 FrameScope scope(masm_, StackFrame::MANUAL);
625
626 // Actually emit code to start a new stack frame.
627 // Push arguments
628 // Save callee-save registers.
629 // Start new stack frame.
630 // Store link register in existing stack-cell.
631 // Order here should correspond to order of offset constants in header file.
632 RegList registers_to_retain = r4.bit() | r5.bit() | r6.bit() |
633 r7.bit() | r8.bit() | r9.bit() | r10.bit() | fp.bit();
634 RegList argument_registers = r0.bit() | r1.bit() | r2.bit() | r3.bit();
635 __ stm(db_w, sp, argument_registers | registers_to_retain | lr.bit());
636 // Set frame pointer in space for it if this is not a direct call
637 // from generated code.
638 __ add(frame_pointer(), sp, Operand(4 * kPointerSize));
639 __ mov(r0, Operand::Zero());
640 __ push(r0); // Make room for success counter and initialize it to 0.
641 __ push(r0); // Make room for "string start - 1" constant.
642 // Check if we have space on the stack for registers.
643 Label stack_limit_hit;
644 Label stack_ok;
645
646 ExternalReference stack_limit =
647 ExternalReference::address_of_stack_limit(isolate());
648 __ mov(r0, Operand(stack_limit));
649 __ ldr(r0, MemOperand(r0));
650 __ sub(r0, sp, r0, SetCC);
651 // Handle it if the stack pointer is already below the stack limit.
652 __ b(ls, &stack_limit_hit);
653 // Check if there is room for the variable number of registers above
654 // the stack limit.
655 __ cmp(r0, Operand(num_registers_ * kPointerSize));
656 __ b(hs, &stack_ok);
657 // Exit with OutOfMemory exception. There is not enough space on the stack
658 // for our working registers.
659 __ mov(r0, Operand(EXCEPTION));
660 __ jmp(&return_r0);
661
662 __ bind(&stack_limit_hit);
663 CallCheckStackGuardState(r0);
664 __ cmp(r0, Operand::Zero());
665 // If returned value is non-zero, we exit with the returned value as result.
666 __ b(ne, &return_r0);
667
668 __ bind(&stack_ok);
669
670 // Allocate space on stack for registers.
671 __ sub(sp, sp, Operand(num_registers_ * kPointerSize));
672 // Load string end.
673 __ ldr(end_of_input_address(), MemOperand(frame_pointer(), kInputEnd));
674 // Load input start.
675 __ ldr(r0, MemOperand(frame_pointer(), kInputStart));
676 // Find negative length (offset of start relative to end).
677 __ sub(current_input_offset(), r0, end_of_input_address());
678 // Set r0 to address of char before start of the input string
679 // (effectively string position -1).
680 __ ldr(r1, MemOperand(frame_pointer(), kStartIndex));
681 __ sub(r0, current_input_offset(), Operand(char_size()));
682 __ sub(r0, r0, Operand(r1, LSL, (mode_ == UC16) ? 1 : 0));
683 // Store this value in a local variable, for use when clearing
684 // position registers.
685 __ str(r0, MemOperand(frame_pointer(), kStringStartMinusOne));
686
687 // Initialize code pointer register
688 __ mov(code_pointer(), Operand(masm_->CodeObject()));
689
690 Label load_char_start_regexp, start_regexp;
691 // Load newline if index is at start, previous character otherwise.
692 __ cmp(r1, Operand::Zero());
693 __ b(ne, &load_char_start_regexp);
694 __ mov(current_character(), Operand('\n'), LeaveCC, eq);
695 __ jmp(&start_regexp);
696
697 // Global regexp restarts matching here.
698 __ bind(&load_char_start_regexp);
699 // Load previous char as initial value of current character register.
700 LoadCurrentCharacterUnchecked(-1, 1);
701 __ bind(&start_regexp);
702
703 // Initialize on-stack registers.
704 if (num_saved_registers_ > 0) { // Always is, if generated from a regexp.
705 // Fill saved registers with initial value = start offset - 1
706 if (num_saved_registers_ > 8) {
707 // Address of register 0.
708 __ add(r1, frame_pointer(), Operand(kRegisterZero));
709 __ mov(r2, Operand(num_saved_registers_));
710 Label init_loop;
711 __ bind(&init_loop);
712 __ str(r0, MemOperand(r1, kPointerSize, NegPostIndex));
713 __ sub(r2, r2, Operand(1), SetCC);
714 __ b(ne, &init_loop);
715 } else {
716 for (int i = 0; i < num_saved_registers_; i++) {
717 __ str(r0, register_location(i));
718 }
719 }
720 }
721
722 // Initialize backtrack stack pointer.
723 __ ldr(backtrack_stackpointer(), MemOperand(frame_pointer(), kStackHighEnd));
724
725 __ jmp(&start_label_);
726
727 // Exit code:
728 if (success_label_.is_linked()) {
729 // Save captures when successful.
730 __ bind(&success_label_);
731 if (num_saved_registers_ > 0) {
732 // copy captures to output
733 __ ldr(r1, MemOperand(frame_pointer(), kInputStart));
734 __ ldr(r0, MemOperand(frame_pointer(), kRegisterOutput));
735 __ ldr(r2, MemOperand(frame_pointer(), kStartIndex));
736 __ sub(r1, end_of_input_address(), r1);
737 // r1 is length of input in bytes.
738 if (mode_ == UC16) {
739 __ mov(r1, Operand(r1, LSR, 1));
740 }
741 // r1 is length of input in characters.
742 __ add(r1, r1, Operand(r2));
743 // r1 is length of string in characters.
744
745 DCHECK_EQ(0, num_saved_registers_ % 2);
746 // Always an even number of capture registers. This allows us to
747 // unroll the loop once to add an operation between a load of a register
748 // and the following use of that register.
749 for (int i = 0; i < num_saved_registers_; i += 2) {
750 __ ldr(r2, register_location(i));
751 __ ldr(r3, register_location(i + 1));
752 if (i == 0 && global_with_zero_length_check()) {
753 // Keep capture start in r4 for the zero-length check later.
754 __ mov(r4, r2);
755 }
756 if (mode_ == UC16) {
757 __ add(r2, r1, Operand(r2, ASR, 1));
758 __ add(r3, r1, Operand(r3, ASR, 1));
759 } else {
760 __ add(r2, r1, Operand(r2));
761 __ add(r3, r1, Operand(r3));
762 }
763 __ str(r2, MemOperand(r0, kPointerSize, PostIndex));
764 __ str(r3, MemOperand(r0, kPointerSize, PostIndex));
765 }
766 }
767
768 if (global()) {
769 // Restart matching if the regular expression is flagged as global.
770 __ ldr(r0, MemOperand(frame_pointer(), kSuccessfulCaptures));
771 __ ldr(r1, MemOperand(frame_pointer(), kNumOutputRegisters));
772 __ ldr(r2, MemOperand(frame_pointer(), kRegisterOutput));
773 // Increment success counter.
774 __ add(r0, r0, Operand(1));
775 __ str(r0, MemOperand(frame_pointer(), kSuccessfulCaptures));
776 // Capture results have been stored, so the number of remaining global
777 // output registers is reduced by the number of stored captures.
778 __ sub(r1, r1, Operand(num_saved_registers_));
779 // Check whether we have enough room for another set of capture results.
780 __ cmp(r1, Operand(num_saved_registers_));
781 __ b(lt, &return_r0);
782
783 __ str(r1, MemOperand(frame_pointer(), kNumOutputRegisters));
784 // Advance the location for output.
785 __ add(r2, r2, Operand(num_saved_registers_ * kPointerSize));
786 __ str(r2, MemOperand(frame_pointer(), kRegisterOutput));
787
788 // Prepare r0 to initialize registers with its value in the next run.
789 __ ldr(r0, MemOperand(frame_pointer(), kStringStartMinusOne));
790
791 if (global_with_zero_length_check()) {
792 // Special case for zero-length matches.
793 // r4: capture start index
794 __ cmp(current_input_offset(), r4);
795 // Not a zero-length match, restart.
796 __ b(ne, &load_char_start_regexp);
797 // Offset from the end is zero if we already reached the end.
798 __ cmp(current_input_offset(), Operand::Zero());
799 __ b(eq, &exit_label_);
800 // Advance current position after a zero-length match.
801 __ add(current_input_offset(),
802 current_input_offset(),
803 Operand((mode_ == UC16) ? 2 : 1));
804 }
805
806 __ b(&load_char_start_regexp);
807 } else {
808 __ mov(r0, Operand(SUCCESS));
809 }
810 }
811
812 // Exit and return r0
813 __ bind(&exit_label_);
814 if (global()) {
815 __ ldr(r0, MemOperand(frame_pointer(), kSuccessfulCaptures));
816 }
817
818 __ bind(&return_r0);
819 // Skip sp past regexp registers and local variables..
820 __ mov(sp, frame_pointer());
821 // Restore registers r4..r11 and return (restoring lr to pc).
822 __ ldm(ia_w, sp, registers_to_retain | pc.bit());
823
824 // Backtrack code (branch target for conditional backtracks).
825 if (backtrack_label_.is_linked()) {
826 __ bind(&backtrack_label_);
827 Backtrack();
828 }
829
830 Label exit_with_exception;
831
832 // Preempt-code
833 if (check_preempt_label_.is_linked()) {
834 SafeCallTarget(&check_preempt_label_);
835
836 CallCheckStackGuardState(r0);
837 __ cmp(r0, Operand::Zero());
838 // If returning non-zero, we should end execution with the given
839 // result as return value.
840 __ b(ne, &return_r0);
841
842 // String might have moved: Reload end of string from frame.
843 __ ldr(end_of_input_address(), MemOperand(frame_pointer(), kInputEnd));
844 SafeReturn();
845 }
846
847 // Backtrack stack overflow code.
848 if (stack_overflow_label_.is_linked()) {
849 SafeCallTarget(&stack_overflow_label_);
850 // Reached if the backtrack-stack limit has been hit.
851 Label grow_failed;
852
853 // Call GrowStack(backtrack_stackpointer(), &stack_base)
854 static const int num_arguments = 3;
855 __ PrepareCallCFunction(num_arguments, r0);
856 __ mov(r0, backtrack_stackpointer());
857 __ add(r1, frame_pointer(), Operand(kStackHighEnd));
858 __ mov(r2, Operand(ExternalReference::isolate_address(isolate())));
859 ExternalReference grow_stack =
860 ExternalReference::re_grow_stack(isolate());
861 __ CallCFunction(grow_stack, num_arguments);
862 // If return NULL, we have failed to grow the stack, and
863 // must exit with a stack-overflow exception.
864 __ cmp(r0, Operand::Zero());
865 __ b(eq, &exit_with_exception);
866 // Otherwise use return value as new stack pointer.
867 __ mov(backtrack_stackpointer(), r0);
868 // Restore saved registers and continue.
869 SafeReturn();
870 }
871
872 if (exit_with_exception.is_linked()) {
873 // If any of the code above needed to exit with an exception.
874 __ bind(&exit_with_exception);
875 // Exit with Result EXCEPTION(-1) to signal thrown exception.
876 __ mov(r0, Operand(EXCEPTION));
877 __ jmp(&return_r0);
878 }
879
880 CodeDesc code_desc;
881 masm_->GetCode(&code_desc);
882 Handle<Code> code = isolate()->factory()->NewCode(
883 code_desc, Code::ComputeFlags(Code::REGEXP), masm_->CodeObject());
884 PROFILE(masm_->isolate(), RegExpCodeCreateEvent(*code, *source));
885 return Handle<HeapObject>::cast(code);
886}
887
888
889void RegExpMacroAssemblerARM::GoTo(Label* to) {
890 BranchOrBacktrack(al, to);
891}
892
893
894void RegExpMacroAssemblerARM::IfRegisterGE(int reg,
895 int comparand,
896 Label* if_ge) {
897 __ ldr(r0, register_location(reg));
898 __ cmp(r0, Operand(comparand));
899 BranchOrBacktrack(ge, if_ge);
900}
901
902
903void RegExpMacroAssemblerARM::IfRegisterLT(int reg,
904 int comparand,
905 Label* if_lt) {
906 __ ldr(r0, register_location(reg));
907 __ cmp(r0, Operand(comparand));
908 BranchOrBacktrack(lt, if_lt);
909}
910
911
912void RegExpMacroAssemblerARM::IfRegisterEqPos(int reg,
913 Label* if_eq) {
914 __ ldr(r0, register_location(reg));
915 __ cmp(r0, Operand(current_input_offset()));
916 BranchOrBacktrack(eq, if_eq);
917}
918
919
920RegExpMacroAssembler::IrregexpImplementation
921 RegExpMacroAssemblerARM::Implementation() {
922 return kARMImplementation;
923}
924
925
926void RegExpMacroAssemblerARM::LoadCurrentCharacter(int cp_offset,
927 Label* on_end_of_input,
928 bool check_bounds,
929 int characters) {
930 DCHECK(cp_offset < (1<<30)); // Be sane! (And ensure negation works)
931 if (check_bounds) {
932 if (cp_offset >= 0) {
933 CheckPosition(cp_offset + characters - 1, on_end_of_input);
934 } else {
935 CheckPosition(cp_offset, on_end_of_input);
936 }
937 }
938 LoadCurrentCharacterUnchecked(cp_offset, characters);
939}
940
941
942void RegExpMacroAssemblerARM::PopCurrentPosition() {
943 Pop(current_input_offset());
944}
945
946
947void RegExpMacroAssemblerARM::PopRegister(int register_index) {
948 Pop(r0);
949 __ str(r0, register_location(register_index));
950}
951
952
953void RegExpMacroAssemblerARM::PushBacktrack(Label* label) {
954 __ mov_label_offset(r0, label);
955 Push(r0);
956 CheckStackLimit();
957}
958
959
960void RegExpMacroAssemblerARM::PushCurrentPosition() {
961 Push(current_input_offset());
962}
963
964
965void RegExpMacroAssemblerARM::PushRegister(int register_index,
966 StackCheckFlag check_stack_limit) {
967 __ ldr(r0, register_location(register_index));
968 Push(r0);
969 if (check_stack_limit) CheckStackLimit();
970}
971
972
973void RegExpMacroAssemblerARM::ReadCurrentPositionFromRegister(int reg) {
974 __ ldr(current_input_offset(), register_location(reg));
975}
976
977
978void RegExpMacroAssemblerARM::ReadStackPointerFromRegister(int reg) {
979 __ ldr(backtrack_stackpointer(), register_location(reg));
980 __ ldr(r0, MemOperand(frame_pointer(), kStackHighEnd));
981 __ add(backtrack_stackpointer(), backtrack_stackpointer(), Operand(r0));
982}
983
984
985void RegExpMacroAssemblerARM::SetCurrentPositionFromEnd(int by) {
986 Label after_position;
987 __ cmp(current_input_offset(), Operand(-by * char_size()));
988 __ b(ge, &after_position);
989 __ mov(current_input_offset(), Operand(-by * char_size()));
990 // On RegExp code entry (where this operation is used), the character before
991 // the current position is expected to be already loaded.
992 // We have advanced the position, so it's safe to read backwards.
993 LoadCurrentCharacterUnchecked(-1, 1);
994 __ bind(&after_position);
995}
996
997
998void RegExpMacroAssemblerARM::SetRegister(int register_index, int to) {
999 DCHECK(register_index >= num_saved_registers_); // Reserved for positions!
1000 __ mov(r0, Operand(to));
1001 __ str(r0, register_location(register_index));
1002}
1003
1004
1005bool RegExpMacroAssemblerARM::Succeed() {
1006 __ jmp(&success_label_);
1007 return global();
1008}
1009
1010
1011void RegExpMacroAssemblerARM::WriteCurrentPositionToRegister(int reg,
1012 int cp_offset) {
1013 if (cp_offset == 0) {
1014 __ str(current_input_offset(), register_location(reg));
1015 } else {
1016 __ add(r0, current_input_offset(), Operand(cp_offset * char_size()));
1017 __ str(r0, register_location(reg));
1018 }
1019}
1020
1021
1022void RegExpMacroAssemblerARM::ClearRegisters(int reg_from, int reg_to) {
1023 DCHECK(reg_from <= reg_to);
1024 __ ldr(r0, MemOperand(frame_pointer(), kStringStartMinusOne));
1025 for (int reg = reg_from; reg <= reg_to; reg++) {
1026 __ str(r0, register_location(reg));
1027 }
1028}
1029
1030
1031void RegExpMacroAssemblerARM::WriteStackPointerToRegister(int reg) {
1032 __ ldr(r1, MemOperand(frame_pointer(), kStackHighEnd));
1033 __ sub(r0, backtrack_stackpointer(), r1);
1034 __ str(r0, register_location(reg));
1035}
1036
1037
1038// Private methods:
1039
1040void RegExpMacroAssemblerARM::CallCheckStackGuardState(Register scratch) {
1041 __ PrepareCallCFunction(3, scratch);
1042
1043 // RegExp code frame pointer.
1044 __ mov(r2, frame_pointer());
1045 // Code* of self.
1046 __ mov(r1, Operand(masm_->CodeObject()));
1047
1048 // We need to make room for the return address on the stack.
1049 int stack_alignment = base::OS::ActivationFrameAlignment();
1050 DCHECK(IsAligned(stack_alignment, kPointerSize));
1051 __ sub(sp, sp, Operand(stack_alignment));
1052
1053 // r0 will point to the return address, placed by DirectCEntry.
1054 __ mov(r0, sp);
1055
1056 ExternalReference stack_guard_check =
1057 ExternalReference::re_check_stack_guard_state(isolate());
1058 __ mov(ip, Operand(stack_guard_check));
1059 DirectCEntryStub stub(isolate());
1060 stub.GenerateCall(masm_, ip);
1061
1062 // Drop the return address from the stack.
1063 __ add(sp, sp, Operand(stack_alignment));
1064
1065 DCHECK(stack_alignment != 0);
1066 __ ldr(sp, MemOperand(sp, 0));
1067
1068 __ mov(code_pointer(), Operand(masm_->CodeObject()));
1069}
1070
1071
1072// Helper function for reading a value out of a stack frame.
1073template <typename T>
1074static T& frame_entry(Address re_frame, int frame_offset) {
1075 return reinterpret_cast<T&>(Memory::int32_at(re_frame + frame_offset));
1076}
1077
1078
1079template <typename T>
1080static T* frame_entry_address(Address re_frame, int frame_offset) {
1081 return reinterpret_cast<T*>(re_frame + frame_offset);
1082}
1083
1084
1085int RegExpMacroAssemblerARM::CheckStackGuardState(Address* return_address,
1086 Code* re_code,
1087 Address re_frame) {
1088 return NativeRegExpMacroAssembler::CheckStackGuardState(
1089 frame_entry<Isolate*>(re_frame, kIsolate),
1090 frame_entry<int>(re_frame, kStartIndex),
1091 frame_entry<int>(re_frame, kDirectCall) == 1, return_address, re_code,
1092 frame_entry_address<String*>(re_frame, kInputString),
1093 frame_entry_address<const byte*>(re_frame, kInputStart),
1094 frame_entry_address<const byte*>(re_frame, kInputEnd));
1095}
1096
1097
1098MemOperand RegExpMacroAssemblerARM::register_location(int register_index) {
1099 DCHECK(register_index < (1<<30));
1100 if (num_registers_ <= register_index) {
1101 num_registers_ = register_index + 1;
1102 }
1103 return MemOperand(frame_pointer(),
1104 kRegisterZero - register_index * kPointerSize);
1105}
1106
1107
1108void RegExpMacroAssemblerARM::CheckPosition(int cp_offset,
1109 Label* on_outside_input) {
1110 if (cp_offset >= 0) {
1111 __ cmp(current_input_offset(), Operand(-cp_offset * char_size()));
1112 BranchOrBacktrack(ge, on_outside_input);
1113 } else {
1114 __ ldr(r1, MemOperand(frame_pointer(), kStringStartMinusOne));
1115 __ add(r0, current_input_offset(), Operand(cp_offset * char_size()));
1116 __ cmp(r0, r1);
1117 BranchOrBacktrack(le, on_outside_input);
1118 }
1119}
1120
1121
1122void RegExpMacroAssemblerARM::BranchOrBacktrack(Condition condition,
1123 Label* to) {
1124 if (condition == al) { // Unconditional.
1125 if (to == NULL) {
1126 Backtrack();
1127 return;
1128 }
1129 __ jmp(to);
1130 return;
1131 }
1132 if (to == NULL) {
1133 __ b(condition, &backtrack_label_);
1134 return;
1135 }
1136 __ b(condition, to);
1137}
1138
1139
1140void RegExpMacroAssemblerARM::SafeCall(Label* to, Condition cond) {
1141 __ bl(to, cond);
1142}
1143
1144
1145void RegExpMacroAssemblerARM::SafeReturn() {
1146 __ pop(lr);
1147 __ add(pc, lr, Operand(masm_->CodeObject()));
1148}
1149
1150
1151void RegExpMacroAssemblerARM::SafeCallTarget(Label* name) {
1152 __ bind(name);
1153 __ sub(lr, lr, Operand(masm_->CodeObject()));
1154 __ push(lr);
1155}
1156
1157
1158void RegExpMacroAssemblerARM::Push(Register source) {
1159 DCHECK(!source.is(backtrack_stackpointer()));
1160 __ str(source,
1161 MemOperand(backtrack_stackpointer(), kPointerSize, NegPreIndex));
1162}
1163
1164
1165void RegExpMacroAssemblerARM::Pop(Register target) {
1166 DCHECK(!target.is(backtrack_stackpointer()));
1167 __ ldr(target,
1168 MemOperand(backtrack_stackpointer(), kPointerSize, PostIndex));
1169}
1170
1171
1172void RegExpMacroAssemblerARM::CheckPreemption() {
1173 // Check for preemption.
1174 ExternalReference stack_limit =
1175 ExternalReference::address_of_stack_limit(isolate());
1176 __ mov(r0, Operand(stack_limit));
1177 __ ldr(r0, MemOperand(r0));
1178 __ cmp(sp, r0);
1179 SafeCall(&check_preempt_label_, ls);
1180}
1181
1182
1183void RegExpMacroAssemblerARM::CheckStackLimit() {
1184 ExternalReference stack_limit =
1185 ExternalReference::address_of_regexp_stack_limit(isolate());
1186 __ mov(r0, Operand(stack_limit));
1187 __ ldr(r0, MemOperand(r0));
1188 __ cmp(backtrack_stackpointer(), Operand(r0));
1189 SafeCall(&stack_overflow_label_, ls);
1190}
1191
1192
1193bool RegExpMacroAssemblerARM::CanReadUnaligned() {
1194 return CpuFeatures::IsSupported(UNALIGNED_ACCESSES) && !slow_safe();
1195}
1196
1197
1198void RegExpMacroAssemblerARM::LoadCurrentCharacterUnchecked(int cp_offset,
1199 int characters) {
1200 Register offset = current_input_offset();
1201 if (cp_offset != 0) {
1202 // r4 is not being used to store the capture start index at this point.
1203 __ add(r4, current_input_offset(), Operand(cp_offset * char_size()));
1204 offset = r4;
1205 }
1206 // The ldr, str, ldrh, strh instructions can do unaligned accesses, if the CPU
1207 // and the operating system running on the target allow it.
1208 // If unaligned load/stores are not supported then this function must only
1209 // be used to load a single character at a time.
1210 if (!CanReadUnaligned()) {
1211 DCHECK(characters == 1);
1212 }
1213
1214 if (mode_ == LATIN1) {
1215 if (characters == 4) {
1216 __ ldr(current_character(), MemOperand(end_of_input_address(), offset));
1217 } else if (characters == 2) {
1218 __ ldrh(current_character(), MemOperand(end_of_input_address(), offset));
1219 } else {
1220 DCHECK(characters == 1);
1221 __ ldrb(current_character(), MemOperand(end_of_input_address(), offset));
1222 }
1223 } else {
1224 DCHECK(mode_ == UC16);
1225 if (characters == 2) {
1226 __ ldr(current_character(), MemOperand(end_of_input_address(), offset));
1227 } else {
1228 DCHECK(characters == 1);
1229 __ ldrh(current_character(), MemOperand(end_of_input_address(), offset));
1230 }
1231 }
1232}
1233
1234
1235#undef __
1236
1237#endif // V8_INTERPRETED_REGEXP
1238
1239} // namespace internal
1240} // namespace v8
1241
1242#endif // V8_TARGET_ARCH_ARM