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