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