regexp-macro-assembler-mips.cc revision 85b71799222b55eb5dd74ea26efe0c64ab655c8c
1// Copyright 2006-2010 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6//     * Redistributions of source code must retain the above copyright
7//       notice, this list of conditions and the following disclaimer.
8//     * Redistributions in binary form must reproduce the above
9//       copyright notice, this list of conditions and the following
10//       disclaimer in the documentation and/or other materials provided
11//       with the distribution.
12//     * Neither the name of Google Inc. nor the names of its
13//       contributors may be used to endorse or promote products derived
14//       from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
30#if defined(V8_TARGET_ARCH_MIPS)
31
32#include "unicode.h"
33#include "log.h"
34#include "code-stubs.h"
35#include "regexp-stack.h"
36#include "macro-assembler.h"
37#include "regexp-macro-assembler.h"
38#include "mips/regexp-macro-assembler-mips.h"
39
40namespace v8 {
41namespace internal {
42
43#ifndef V8_INTERPRETED_REGEXP
44/*
45 * This assembler uses the following register assignment convention
46 * - t1 : Pointer to current code object (Code*) including heap object tag.
47 * - t2 : Current position in input, as negative offset from end of string.
48 *        Please notice that this is the byte offset, not the character offset!
49 * - t3 : Currently loaded character. Must be loaded using
50 *        LoadCurrentCharacter before using any of the dispatch methods.
51 * - t4 : points to tip of backtrack stack
52 * - t5 : Unused.
53 * - t6 : End of input (points to byte after last character in input).
54 * - fp : Frame pointer. Used to access arguments, local variables and
55 *         RegExp registers.
56 * - sp : points to tip of C stack.
57 *
58 * The remaining registers are free for computations.
59 * Each call to a public method should retain this convention.
60 *
61 * The stack will have the following structure:
62 *
63 *  - fp[56]  direct_call  (if 1, direct call from JavaScript code,
64 *                          if 0, call through the runtime system).
65 *  - fp[52]  stack_area_base (High end of the memory area to use as
66 *                             backtracking stack).
67 *  - fp[48]  int* capture_array (int[num_saved_registers_], for output).
68 *  - fp[44]  secondary link/return address used by native call.
69 *  --- sp when called ---
70 *  - fp[40]  return address (lr).
71 *  - fp[36]  old frame pointer (r11).
72 *  - fp[0..32]  backup of registers s0..s7.
73 *  --- frame pointer ----
74 *  - fp[-4]  end of input       (Address of end of string).
75 *  - fp[-8]  start of input     (Address of first character in string).
76 *  - fp[-12] start index        (character index of start).
77 *  - fp[-16] void* input_string (location of a handle containing the string).
78 *  - fp[-20] Offset of location before start of input (effectively character
79 *            position -1). Used to initialize capture registers to a
80 *            non-position.
81 *  - fp[-24] At start (if 1, we are starting at the start of the
82 *    string, otherwise 0)
83 *  - fp[-28] register 0         (Only positions must be stored in the first
84 *  -         register 1          num_saved_registers_ registers)
85 *  -         ...
86 *  -         register num_registers-1
87 *  --- sp ---
88 *
89 * The first num_saved_registers_ registers are initialized to point to
90 * "character -1" in the string (i.e., char_size() bytes before the first
91 * character of the string). The remaining registers start out as garbage.
92 *
93 * The data up to the return address must be placed there by the calling
94 * code and the remaining arguments are passed in registers, e.g. by calling the
95 * code entry as cast to a function with the signature:
96 * int (*match)(String* input_string,
97 *              int start_index,
98 *              Address start,
99 *              Address end,
100 *              Address secondary_return_address,  // Only used by native call.
101 *              int* capture_output_array,
102 *              byte* stack_area_base,
103 *              bool direct_call = false)
104 * The call is performed by NativeRegExpMacroAssembler::Execute()
105 * (in regexp-macro-assembler.cc) via the CALL_GENERATED_REGEXP_CODE macro
106 * in mips/simulator-mips.h.
107 * When calling as a non-direct call (i.e., from C++ code), the return address
108 * area is overwritten with the ra register by the RegExp code. When doing a
109 * direct call from generated code, the return address is placed there by
110 * the calling code, as in a normal exit frame.
111 */
112
113#define __ ACCESS_MASM(masm_)
114
115RegExpMacroAssemblerMIPS::RegExpMacroAssemblerMIPS(
116    Mode mode,
117    int registers_to_save)
118    : masm_(new MacroAssembler(Isolate::Current(), NULL, kRegExpCodeSize)),
119      mode_(mode),
120      num_registers_(registers_to_save),
121      num_saved_registers_(registers_to_save),
122      entry_label_(),
123      start_label_(),
124      success_label_(),
125      backtrack_label_(),
126      exit_label_(),
127      internal_failure_label_() {
128  ASSERT_EQ(0, registers_to_save % 2);
129  __ jmp(&entry_label_);   // We'll write the entry code later.
130  // If the code gets too big or corrupted, an internal exception will be
131  // raised, and we will exit right away.
132  __ bind(&internal_failure_label_);
133  __ li(v0, Operand(FAILURE));
134  __ Ret();
135  __ bind(&start_label_);  // And then continue from here.
136}
137
138
139RegExpMacroAssemblerMIPS::~RegExpMacroAssemblerMIPS() {
140  delete masm_;
141  // Unuse labels in case we throw away the assembler without calling GetCode.
142  entry_label_.Unuse();
143  start_label_.Unuse();
144  success_label_.Unuse();
145  backtrack_label_.Unuse();
146  exit_label_.Unuse();
147  check_preempt_label_.Unuse();
148  stack_overflow_label_.Unuse();
149  internal_failure_label_.Unuse();
150}
151
152
153int RegExpMacroAssemblerMIPS::stack_limit_slack()  {
154  return RegExpStack::kStackLimitSlack;
155}
156
157
158void RegExpMacroAssemblerMIPS::AdvanceCurrentPosition(int by) {
159  if (by != 0) {
160    __ Addu(current_input_offset(),
161           current_input_offset(), Operand(by * char_size()));
162  }
163}
164
165
166void RegExpMacroAssemblerMIPS::AdvanceRegister(int reg, int by) {
167  ASSERT(reg >= 0);
168  ASSERT(reg < num_registers_);
169  if (by != 0) {
170    __ lw(a0, register_location(reg));
171    __ Addu(a0, a0, Operand(by));
172    __ sw(a0, register_location(reg));
173  }
174}
175
176
177void RegExpMacroAssemblerMIPS::Backtrack() {
178  CheckPreemption();
179  // Pop Code* offset from backtrack stack, add Code* and jump to location.
180  Pop(a0);
181  __ Addu(a0, a0, code_pointer());
182  __ Jump(a0);
183}
184
185
186void RegExpMacroAssemblerMIPS::Bind(Label* label) {
187  __ bind(label);
188}
189
190
191void RegExpMacroAssemblerMIPS::CheckCharacter(uint32_t c, Label* on_equal) {
192  BranchOrBacktrack(on_equal, eq, current_character(), Operand(c));
193}
194
195
196void RegExpMacroAssemblerMIPS::CheckCharacterGT(uc16 limit, Label* on_greater) {
197  BranchOrBacktrack(on_greater, gt, current_character(), Operand(limit));
198}
199
200
201void RegExpMacroAssemblerMIPS::CheckAtStart(Label* on_at_start) {
202  Label not_at_start;
203  // Did we start the match at the start of the string at all?
204  __ lw(a0, MemOperand(frame_pointer(), kAtStart));
205  BranchOrBacktrack(&not_at_start, eq, a0, Operand(zero_reg));
206
207  // If we did, are we still at the start of the input?
208  __ lw(a1, MemOperand(frame_pointer(), kInputStart));
209  __ Addu(a0, end_of_input_address(), Operand(current_input_offset()));
210  BranchOrBacktrack(on_at_start, eq, a0, Operand(a1));
211  __ bind(&not_at_start);
212}
213
214
215void RegExpMacroAssemblerMIPS::CheckNotAtStart(Label* on_not_at_start) {
216  // Did we start the match at the start of the string at all?
217  __ lw(a0, MemOperand(frame_pointer(), kAtStart));
218  BranchOrBacktrack(on_not_at_start, eq, a0, Operand(zero_reg));
219  // If we did, are we still at the start of the input?
220  __ lw(a1, MemOperand(frame_pointer(), kInputStart));
221  __ Addu(a0, end_of_input_address(), Operand(current_input_offset()));
222  BranchOrBacktrack(on_not_at_start, ne, a0, Operand(a1));
223}
224
225
226void RegExpMacroAssemblerMIPS::CheckCharacterLT(uc16 limit, Label* on_less) {
227  BranchOrBacktrack(on_less, lt, current_character(), Operand(limit));
228}
229
230
231void RegExpMacroAssemblerMIPS::CheckCharacters(Vector<const uc16> str,
232                                              int cp_offset,
233                                              Label* on_failure,
234                                              bool check_end_of_string) {
235  if (on_failure == NULL) {
236    // Instead of inlining a backtrack for each test, (re)use the global
237    // backtrack target.
238    on_failure = &backtrack_label_;
239  }
240
241  if (check_end_of_string) {
242    // Is last character of required match inside string.
243    CheckPosition(cp_offset + str.length() - 1, on_failure);
244  }
245
246  __ Addu(a0, end_of_input_address(), Operand(current_input_offset()));
247  if (cp_offset != 0) {
248    int byte_offset = cp_offset * char_size();
249    __ Addu(a0, a0, Operand(byte_offset));
250  }
251
252  // a0 : Address of characters to match against str.
253  int stored_high_byte = 0;
254  for (int i = 0; i < str.length(); i++) {
255    if (mode_ == ASCII) {
256      __ lbu(a1, MemOperand(a0, 0));
257      __ addiu(a0, a0, char_size());
258      ASSERT(str[i] <= String::kMaxAsciiCharCode);
259      BranchOrBacktrack(on_failure, ne, a1, Operand(str[i]));
260    } else {
261      __ lhu(a1, MemOperand(a0, 0));
262      __ addiu(a0, a0, char_size());
263      uc16 match_char = str[i];
264      int match_high_byte = (match_char >> 8);
265      if (match_high_byte == 0) {
266        BranchOrBacktrack(on_failure, ne, a1, Operand(str[i]));
267      } else {
268        if (match_high_byte != stored_high_byte) {
269          __ li(a2, Operand(match_high_byte));
270          stored_high_byte = match_high_byte;
271        }
272        __ Addu(a3, a2, Operand(match_char & 0xff));
273        BranchOrBacktrack(on_failure, ne, a1, Operand(a3));
274      }
275    }
276  }
277}
278
279
280void RegExpMacroAssemblerMIPS::CheckGreedyLoop(Label* on_equal) {
281  Label backtrack_non_equal;
282  __ lw(a0, MemOperand(backtrack_stackpointer(), 0));
283  __ Branch(&backtrack_non_equal, ne, current_input_offset(), Operand(a0));
284  __ Addu(backtrack_stackpointer(),
285          backtrack_stackpointer(),
286          Operand(kPointerSize));
287  __ bind(&backtrack_non_equal);
288  BranchOrBacktrack(on_equal, eq, current_input_offset(), Operand(a0));
289}
290
291
292void RegExpMacroAssemblerMIPS::CheckNotBackReferenceIgnoreCase(
293    int start_reg,
294    Label* on_no_match) {
295  Label fallthrough;
296  __ lw(a0, register_location(start_reg));  // Index of start of capture.
297  __ lw(a1, register_location(start_reg + 1));  // Index of end of capture.
298  __ Subu(a1, a1, a0);  // Length of capture.
299
300  // If length is zero, either the capture is empty or it is not participating.
301  // In either case succeed immediately.
302  __ Branch(&fallthrough, eq, a1, Operand(zero_reg));
303
304  __ Addu(t5, a1, current_input_offset());
305  // Check that there are enough characters left in the input.
306  BranchOrBacktrack(on_no_match, gt, t5, Operand(zero_reg));
307
308  if (mode_ == ASCII) {
309    Label success;
310    Label fail;
311    Label loop_check;
312
313    // a0 - offset of start of capture.
314    // a1 - length of capture.
315    __ Addu(a0, a0, Operand(end_of_input_address()));
316    __ Addu(a2, end_of_input_address(), Operand(current_input_offset()));
317    __ Addu(a1, a0, Operand(a1));
318
319    // a0 - Address of start of capture.
320    // a1 - Address of end of capture.
321    // a2 - Address of current input position.
322
323    Label loop;
324    __ bind(&loop);
325    __ lbu(a3, MemOperand(a0, 0));
326    __ addiu(a0, a0, char_size());
327    __ lbu(t0, MemOperand(a2, 0));
328    __ addiu(a2, a2, char_size());
329
330    __ Branch(&loop_check, eq, t0, Operand(a3));
331
332    // Mismatch, try case-insensitive match (converting letters to lower-case).
333    __ Or(a3, a3, Operand(0x20));  // Convert capture character to lower-case.
334    __ Or(t0, t0, Operand(0x20));  // Also convert input character.
335    __ Branch(&fail, ne, t0, Operand(a3));
336    __ Subu(a3, a3, Operand('a'));
337    __ Branch(&fail, hi, a3, Operand('z' - 'a'));  // Is a3 a lowercase letter?
338
339    __ bind(&loop_check);
340    __ Branch(&loop, lt, a0, Operand(a1));
341    __ jmp(&success);
342
343    __ bind(&fail);
344    GoTo(on_no_match);
345
346    __ bind(&success);
347    // Compute new value of character position after the matched part.
348    __ Subu(current_input_offset(), a2, end_of_input_address());
349  } else {
350    ASSERT(mode_ == UC16);
351    // Put regexp engine registers on stack.
352    RegList regexp_registers_to_retain = current_input_offset().bit() |
353        current_character().bit() | backtrack_stackpointer().bit();
354    __ MultiPush(regexp_registers_to_retain);
355
356    int argument_count = 4;
357    __ PrepareCallCFunction(argument_count, a2);
358
359    // a0 - offset of start of capture.
360    // a1 - length of capture.
361
362    // Put arguments into arguments registers.
363    // Parameters are
364    //   a0: Address byte_offset1 - Address captured substring's start.
365    //   a1: Address byte_offset2 - Address of current character position.
366    //   a2: size_t byte_length - length of capture in bytes(!).
367    //   a3: Isolate* isolate.
368
369    // Address of start of capture.
370    __ Addu(a0, a0, Operand(end_of_input_address()));
371    // Length of capture.
372    __ mov(a2, a1);
373    // Save length in callee-save register for use on return.
374    __ mov(s3, a1);
375    // Address of current input position.
376    __ Addu(a1, current_input_offset(), Operand(end_of_input_address()));
377    // Isolate.
378    __ li(a3, Operand(ExternalReference::isolate_address()));
379
380    ExternalReference function =
381        ExternalReference::re_case_insensitive_compare_uc16(masm_->isolate());
382    __ CallCFunction(function, argument_count);
383
384    // Restore regexp engine registers.
385    __ MultiPop(regexp_registers_to_retain);
386    __ li(code_pointer(), Operand(masm_->CodeObject()));
387    __ lw(end_of_input_address(), MemOperand(frame_pointer(), kInputEnd));
388
389    // Check if function returned non-zero for success or zero for failure.
390    BranchOrBacktrack(on_no_match, eq, v0, Operand(zero_reg));
391    // On success, increment position by length of capture.
392    __ Addu(current_input_offset(), current_input_offset(), Operand(s3));
393  }
394
395  __ bind(&fallthrough);
396}
397
398
399void RegExpMacroAssemblerMIPS::CheckNotBackReference(
400    int start_reg,
401    Label* on_no_match) {
402  Label fallthrough;
403  Label success;
404
405  // Find length of back-referenced capture.
406  __ lw(a0, register_location(start_reg));
407  __ lw(a1, register_location(start_reg + 1));
408  __ Subu(a1, a1, a0);  // Length to check.
409  // Succeed on empty capture (including no capture).
410  __ Branch(&fallthrough, eq, a1, Operand(zero_reg));
411
412  __ Addu(t5, a1, current_input_offset());
413  // Check that there are enough characters left in the input.
414  BranchOrBacktrack(on_no_match, gt, t5, Operand(zero_reg));
415
416  // Compute pointers to match string and capture string.
417  __ Addu(a0, a0, Operand(end_of_input_address()));
418  __ Addu(a2, end_of_input_address(), Operand(current_input_offset()));
419  __ Addu(a1, a1, Operand(a0));
420
421  Label loop;
422  __ bind(&loop);
423  if (mode_ == ASCII) {
424    __ lbu(a3, MemOperand(a0, 0));
425    __ addiu(a0, a0, char_size());
426    __ lbu(t0, MemOperand(a2, 0));
427    __ addiu(a2, a2, char_size());
428  } else {
429    ASSERT(mode_ == UC16);
430    __ lhu(a3, MemOperand(a0, 0));
431    __ addiu(a0, a0, char_size());
432    __ lhu(t0, MemOperand(a2, 0));
433    __ addiu(a2, a2, char_size());
434  }
435  BranchOrBacktrack(on_no_match, ne, a3, Operand(t0));
436  __ Branch(&loop, lt, a0, Operand(a1));
437
438  // Move current character position to position after match.
439  __ Subu(current_input_offset(), a2, end_of_input_address());
440  __ bind(&fallthrough);
441}
442
443
444void RegExpMacroAssemblerMIPS::CheckNotRegistersEqual(int reg1,
445                                                      int reg2,
446                                                      Label* on_not_equal) {
447  UNIMPLEMENTED_MIPS();
448}
449
450
451void RegExpMacroAssemblerMIPS::CheckNotCharacter(uint32_t c,
452                                                Label* on_not_equal) {
453  BranchOrBacktrack(on_not_equal, ne, current_character(), Operand(c));
454}
455
456
457void RegExpMacroAssemblerMIPS::CheckCharacterAfterAnd(uint32_t c,
458                                                     uint32_t mask,
459                                                     Label* on_equal) {
460  __ And(a0, current_character(), Operand(mask));
461  BranchOrBacktrack(on_equal, eq, a0, Operand(c));
462}
463
464
465void RegExpMacroAssemblerMIPS::CheckNotCharacterAfterAnd(uint32_t c,
466                                                        uint32_t mask,
467                                                        Label* on_not_equal) {
468  __ And(a0, current_character(), Operand(mask));
469  BranchOrBacktrack(on_not_equal, ne, a0, Operand(c));
470}
471
472
473void RegExpMacroAssemblerMIPS::CheckNotCharacterAfterMinusAnd(
474    uc16 c,
475    uc16 minus,
476    uc16 mask,
477    Label* on_not_equal) {
478  UNIMPLEMENTED_MIPS();
479}
480
481
482bool RegExpMacroAssemblerMIPS::CheckSpecialCharacterClass(uc16 type,
483                                                         Label* on_no_match) {
484  // Range checks (c in min..max) are generally implemented by an unsigned
485  // (c - min) <= (max - min) check.
486  switch (type) {
487  case 's':
488    // Match space-characters.
489    if (mode_ == ASCII) {
490      // ASCII space characters are '\t'..'\r' and ' '.
491      Label success;
492      __ Branch(&success, eq, current_character(), Operand(' '));
493      // Check range 0x09..0x0d.
494      __ Subu(a0, current_character(), Operand('\t'));
495      BranchOrBacktrack(on_no_match, hi, a0, Operand('\r' - '\t'));
496      __ bind(&success);
497      return true;
498    }
499    return false;
500  case 'S':
501    // Match non-space characters.
502    if (mode_ == ASCII) {
503      // ASCII space characters are '\t'..'\r' and ' '.
504      BranchOrBacktrack(on_no_match, eq, current_character(), Operand(' '));
505      __ Subu(a0, current_character(), Operand('\t'));
506      BranchOrBacktrack(on_no_match, ls, a0, Operand('\r' - '\t'));
507      return true;
508    }
509    return false;
510  case 'd':
511    // Match ASCII digits ('0'..'9').
512    __ Subu(a0, current_character(), Operand('0'));
513    BranchOrBacktrack(on_no_match, hi, a0, Operand('9' - '0'));
514    return true;
515  case 'D':
516    // Match non ASCII-digits.
517    __ Subu(a0, current_character(), Operand('0'));
518    BranchOrBacktrack(on_no_match, ls, a0, Operand('9' - '0'));
519    return true;
520  case '.': {
521    // Match non-newlines (not 0x0a('\n'), 0x0d('\r'), 0x2028 and 0x2029).
522    __ Xor(a0, current_character(), Operand(0x01));
523    // See if current character is '\n'^1 or '\r'^1, i.e., 0x0b or 0x0c.
524    __ Subu(a0, a0, Operand(0x0b));
525    BranchOrBacktrack(on_no_match, ls, a0, Operand(0x0c - 0x0b));
526    if (mode_ == UC16) {
527      // Compare original value to 0x2028 and 0x2029, using the already
528      // computed (current_char ^ 0x01 - 0x0b). I.e., check for
529      // 0x201d (0x2028 - 0x0b) or 0x201e.
530      __ Subu(a0, a0, Operand(0x2028 - 0x0b));
531      BranchOrBacktrack(on_no_match, ls, a0, Operand(1));
532    }
533    return true;
534  }
535  case 'n': {
536    // Match newlines (0x0a('\n'), 0x0d('\r'), 0x2028 and 0x2029).
537    __ Xor(a0, current_character(), Operand(0x01));
538    // See if current character is '\n'^1 or '\r'^1, i.e., 0x0b or 0x0c.
539    __ Subu(a0, a0, Operand(0x0b));
540    if (mode_ == ASCII) {
541      BranchOrBacktrack(on_no_match, hi, a0, Operand(0x0c - 0x0b));
542    } else {
543      Label done;
544      BranchOrBacktrack(&done, ls, a0, Operand(0x0c - 0x0b));
545      // Compare original value to 0x2028 and 0x2029, using the already
546      // computed (current_char ^ 0x01 - 0x0b). I.e., check for
547      // 0x201d (0x2028 - 0x0b) or 0x201e.
548      __ Subu(a0, a0, Operand(0x2028 - 0x0b));
549      BranchOrBacktrack(on_no_match, hi, a0, Operand(1));
550      __ bind(&done);
551    }
552    return true;
553  }
554  case 'w': {
555    if (mode_ != ASCII) {
556      // Table is 128 entries, so all ASCII characters can be tested.
557      BranchOrBacktrack(on_no_match, hi, current_character(), Operand('z'));
558    }
559    ExternalReference map = ExternalReference::re_word_character_map();
560    __ li(a0, Operand(map));
561    __ Addu(a0, a0, current_character());
562    __ lbu(a0, MemOperand(a0, 0));
563    BranchOrBacktrack(on_no_match, eq, a0, Operand(zero_reg));
564    return true;
565  }
566  case 'W': {
567    Label done;
568    if (mode_ != ASCII) {
569      // Table is 128 entries, so all ASCII characters can be tested.
570      __ Branch(&done, hi, current_character(), Operand('z'));
571    }
572    ExternalReference map = ExternalReference::re_word_character_map();
573    __ li(a0, Operand(map));
574    __ Addu(a0, a0, current_character());
575    __ lbu(a0, MemOperand(a0, 0));
576    BranchOrBacktrack(on_no_match, ne, a0, Operand(zero_reg));
577    if (mode_ != ASCII) {
578      __ bind(&done);
579    }
580    return true;
581  }
582  case '*':
583    // Match any character.
584    return true;
585  // No custom implementation (yet): s(UC16), S(UC16).
586  default:
587    return false;
588  }
589}
590
591
592void RegExpMacroAssemblerMIPS::Fail() {
593  __ li(v0, Operand(FAILURE));
594  __ jmp(&exit_label_);
595}
596
597
598Handle<HeapObject> RegExpMacroAssemblerMIPS::GetCode(Handle<String> source) {
599  if (masm_->has_exception()) {
600    // If the code gets corrupted due to long regular expressions and lack of
601    // space on trampolines, an internal exception flag is set. If this case
602    // is detected, we will jump into exit sequence right away.
603    __ bind_to(&entry_label_, internal_failure_label_.pos());
604  } else {
605    // Finalize code - write the entry point code now we know how many
606    // registers we need.
607
608    // Entry code:
609    __ bind(&entry_label_);
610    // Push arguments
611    // Save callee-save registers.
612    // Start new stack frame.
613    // Store link register in existing stack-cell.
614    // Order here should correspond to order of offset constants in header file.
615    RegList registers_to_retain = s0.bit() | s1.bit() | s2.bit() |
616        s3.bit() | s4.bit() | s5.bit() | s6.bit() | s7.bit() | fp.bit();
617    RegList argument_registers = a0.bit() | a1.bit() | a2.bit() | a3.bit();
618    __ MultiPush(argument_registers | registers_to_retain | ra.bit());
619    // Set frame pointer in space for it if this is not a direct call
620    // from generated code.
621    __ Addu(frame_pointer(), sp, Operand(4 * kPointerSize));
622    __ push(a0);  // Make room for "position - 1" constant (value irrelevant).
623    __ push(a0);  // Make room for "at start" constant (value irrelevant).
624
625    // Check if we have space on the stack for registers.
626    Label stack_limit_hit;
627    Label stack_ok;
628
629    ExternalReference stack_limit =
630        ExternalReference::address_of_stack_limit(masm_->isolate());
631    __ li(a0, Operand(stack_limit));
632    __ lw(a0, MemOperand(a0));
633    __ Subu(a0, sp, a0);
634    // Handle it if the stack pointer is already below the stack limit.
635    __ Branch(&stack_limit_hit, le, a0, Operand(zero_reg));
636    // Check if there is room for the variable number of registers above
637    // the stack limit.
638    __ Branch(&stack_ok, hs, a0, Operand(num_registers_ * kPointerSize));
639    // Exit with OutOfMemory exception. There is not enough space on the stack
640    // for our working registers.
641    __ li(v0, Operand(EXCEPTION));
642    __ jmp(&exit_label_);
643
644    __ bind(&stack_limit_hit);
645    CallCheckStackGuardState(a0);
646    // If returned value is non-zero, we exit with the returned value as result.
647    __ Branch(&exit_label_, ne, v0, Operand(zero_reg));
648
649    __ bind(&stack_ok);
650    // Allocate space on stack for registers.
651    __ Subu(sp, sp, Operand(num_registers_ * kPointerSize));
652    // Load string end.
653    __ lw(end_of_input_address(), MemOperand(frame_pointer(), kInputEnd));
654    // Load input start.
655    __ lw(a0, MemOperand(frame_pointer(), kInputStart));
656    // Find negative length (offset of start relative to end).
657    __ Subu(current_input_offset(), a0, end_of_input_address());
658    // Set a0 to address of char before start of the input string
659    // (effectively string position -1).
660    __ lw(a1, MemOperand(frame_pointer(), kStartIndex));
661    __ Subu(a0, current_input_offset(), Operand(char_size()));
662    __ sll(t5, a1, (mode_ == UC16) ? 1 : 0);
663    __ Subu(a0, a0, t5);
664    // Store this value in a local variable, for use when clearing
665    // position registers.
666    __ sw(a0, MemOperand(frame_pointer(), kInputStartMinusOne));
667
668    // Determine whether the start index is zero, that is at the start of the
669    // string, and store that value in a local variable.
670    __ mov(t5, a1);
671    __ li(a1, Operand(1));
672    __ movn(a1, zero_reg, t5);
673    __ sw(a1, MemOperand(frame_pointer(), kAtStart));
674
675    if (num_saved_registers_ > 0) {  // Always is, if generated from a regexp.
676      // Fill saved registers with initial value = start offset - 1.
677
678      // Address of register 0.
679      __ Addu(a1, frame_pointer(), Operand(kRegisterZero));
680      __ li(a2, Operand(num_saved_registers_));
681      Label init_loop;
682      __ bind(&init_loop);
683      __ sw(a0, MemOperand(a1));
684      __ Addu(a1, a1, Operand(-kPointerSize));
685      __ Subu(a2, a2, Operand(1));
686      __ Branch(&init_loop, ne, a2, Operand(zero_reg));
687    }
688
689    // Initialize backtrack stack pointer.
690    __ lw(backtrack_stackpointer(), MemOperand(frame_pointer(), kStackHighEnd));
691    // Initialize code pointer register
692    __ li(code_pointer(), Operand(masm_->CodeObject()));
693    // Load previous char as initial value of current character register.
694    Label at_start;
695    __ lw(a0, MemOperand(frame_pointer(), kAtStart));
696    __ Branch(&at_start, ne, a0, Operand(zero_reg));
697    LoadCurrentCharacterUnchecked(-1, 1);  // Load previous char.
698    __ jmp(&start_label_);
699    __ bind(&at_start);
700    __ li(current_character(), Operand('\n'));
701    __ jmp(&start_label_);
702
703
704    // Exit code:
705    if (success_label_.is_linked()) {
706      // Save captures when successful.
707      __ bind(&success_label_);
708      if (num_saved_registers_ > 0) {
709        // Copy captures to output.
710        __ lw(a1, MemOperand(frame_pointer(), kInputStart));
711        __ lw(a0, MemOperand(frame_pointer(), kRegisterOutput));
712        __ lw(a2, MemOperand(frame_pointer(), kStartIndex));
713        __ Subu(a1, end_of_input_address(), a1);
714        // a1 is length of input in bytes.
715        if (mode_ == UC16) {
716          __ srl(a1, a1, 1);
717        }
718        // a1 is length of input in characters.
719        __ Addu(a1, a1, Operand(a2));
720        // a1 is length of string in characters.
721
722        ASSERT_EQ(0, num_saved_registers_ % 2);
723        // Always an even number of capture registers. This allows us to
724        // unroll the loop once to add an operation between a load of a register
725        // and the following use of that register.
726        for (int i = 0; i < num_saved_registers_; i += 2) {
727          __ lw(a2, register_location(i));
728          __ lw(a3, register_location(i + 1));
729          if (mode_ == UC16) {
730            __ sra(a2, a2, 1);
731            __ Addu(a2, a2, a1);
732            __ sra(a3, a3, 1);
733            __ Addu(a3, a3, a1);
734          } else {
735            __ Addu(a2, a1, Operand(a2));
736            __ Addu(a3, a1, Operand(a3));
737          }
738          __ sw(a2, MemOperand(a0));
739          __ Addu(a0, a0, kPointerSize);
740          __ sw(a3, MemOperand(a0));
741          __ Addu(a0, a0, kPointerSize);
742        }
743      }
744      __ li(v0, Operand(SUCCESS));
745    }
746    // Exit and return v0.
747    __ bind(&exit_label_);
748    // Skip sp past regexp registers and local variables..
749    __ mov(sp, frame_pointer());
750    // Restore registers s0..s7 and return (restoring ra to pc).
751    __ MultiPop(registers_to_retain | ra.bit());
752    __ Ret();
753
754    // Backtrack code (branch target for conditional backtracks).
755    if (backtrack_label_.is_linked()) {
756      __ bind(&backtrack_label_);
757      Backtrack();
758    }
759
760    Label exit_with_exception;
761
762    // Preempt-code.
763    if (check_preempt_label_.is_linked()) {
764      SafeCallTarget(&check_preempt_label_);
765      // Put regexp engine registers on stack.
766      RegList regexp_registers_to_retain = current_input_offset().bit() |
767          current_character().bit() | backtrack_stackpointer().bit();
768      __ MultiPush(regexp_registers_to_retain);
769      CallCheckStackGuardState(a0);
770      __ MultiPop(regexp_registers_to_retain);
771      // If returning non-zero, we should end execution with the given
772      // result as return value.
773      __ Branch(&exit_label_, ne, v0, Operand(zero_reg));
774
775      // String might have moved: Reload end of string from frame.
776      __ lw(end_of_input_address(), MemOperand(frame_pointer(), kInputEnd));
777      __ li(code_pointer(), Operand(masm_->CodeObject()));
778      SafeReturn();
779    }
780
781    // Backtrack stack overflow code.
782    if (stack_overflow_label_.is_linked()) {
783      SafeCallTarget(&stack_overflow_label_);
784      // Reached if the backtrack-stack limit has been hit.
785      // Put regexp engine registers on stack first.
786      RegList regexp_registers = current_input_offset().bit() |
787          current_character().bit();
788      __ MultiPush(regexp_registers);
789      Label grow_failed;
790      // Call GrowStack(backtrack_stackpointer(), &stack_base)
791      static const int num_arguments = 3;
792      __ PrepareCallCFunction(num_arguments, a0);
793      __ mov(a0, backtrack_stackpointer());
794      __ Addu(a1, frame_pointer(), Operand(kStackHighEnd));
795      __ li(a2, Operand(ExternalReference::isolate_address()));
796      ExternalReference grow_stack =
797          ExternalReference::re_grow_stack(masm_->isolate());
798      __ CallCFunction(grow_stack, num_arguments);
799      // Restore regexp registers.
800      __ MultiPop(regexp_registers);
801      // If return NULL, we have failed to grow the stack, and
802      // must exit with a stack-overflow exception.
803      __ Branch(&exit_with_exception, eq, v0, Operand(zero_reg));
804      // Otherwise use return value as new stack pointer.
805      __ mov(backtrack_stackpointer(), v0);
806      // Restore saved registers and continue.
807      __ li(code_pointer(), Operand(masm_->CodeObject()));
808      __ lw(end_of_input_address(), MemOperand(frame_pointer(), kInputEnd));
809      SafeReturn();
810    }
811
812    if (exit_with_exception.is_linked()) {
813      // If any of the code above needed to exit with an exception.
814      __ bind(&exit_with_exception);
815      // Exit with Result EXCEPTION(-1) to signal thrown exception.
816      __ li(v0, Operand(EXCEPTION));
817      __ jmp(&exit_label_);
818    }
819  }
820
821  CodeDesc code_desc;
822  masm_->GetCode(&code_desc);
823  Handle<Code> code = FACTORY->NewCode(code_desc,
824                                       Code::ComputeFlags(Code::REGEXP),
825                                       masm_->CodeObject());
826  LOG(Isolate::Current(), RegExpCodeCreateEvent(*code, *source));
827  return Handle<HeapObject>::cast(code);
828}
829
830
831void RegExpMacroAssemblerMIPS::GoTo(Label* to) {
832  if (to == NULL) {
833    Backtrack();
834    return;
835  }
836  __ jmp(to);
837  return;
838}
839
840
841void RegExpMacroAssemblerMIPS::IfRegisterGE(int reg,
842                                           int comparand,
843                                           Label* if_ge) {
844  __ lw(a0, register_location(reg));
845    BranchOrBacktrack(if_ge, ge, a0, Operand(comparand));
846}
847
848
849void RegExpMacroAssemblerMIPS::IfRegisterLT(int reg,
850                                           int comparand,
851                                           Label* if_lt) {
852  __ lw(a0, register_location(reg));
853  BranchOrBacktrack(if_lt, lt, a0, Operand(comparand));
854}
855
856
857void RegExpMacroAssemblerMIPS::IfRegisterEqPos(int reg,
858                                              Label* if_eq) {
859  __ lw(a0, register_location(reg));
860  BranchOrBacktrack(if_eq, eq, a0, Operand(current_input_offset()));
861}
862
863
864RegExpMacroAssembler::IrregexpImplementation
865    RegExpMacroAssemblerMIPS::Implementation() {
866  return kMIPSImplementation;
867}
868
869
870void RegExpMacroAssemblerMIPS::LoadCurrentCharacter(int cp_offset,
871                                                   Label* on_end_of_input,
872                                                   bool check_bounds,
873                                                   int characters) {
874  ASSERT(cp_offset >= -1);      // ^ and \b can look behind one character.
875  ASSERT(cp_offset < (1<<30));  // Be sane! (And ensure negation works).
876  if (check_bounds) {
877    CheckPosition(cp_offset + characters - 1, on_end_of_input);
878  }
879  LoadCurrentCharacterUnchecked(cp_offset, characters);
880}
881
882
883void RegExpMacroAssemblerMIPS::PopCurrentPosition() {
884  Pop(current_input_offset());
885}
886
887
888void RegExpMacroAssemblerMIPS::PopRegister(int register_index) {
889  Pop(a0);
890  __ sw(a0, register_location(register_index));
891}
892
893
894void RegExpMacroAssemblerMIPS::PushBacktrack(Label* label) {
895  if (label->is_bound()) {
896    int target = label->pos();
897    __ li(a0, Operand(target + Code::kHeaderSize - kHeapObjectTag));
898  } else {
899    Label after_constant;
900    __ Branch(&after_constant);
901    int offset = masm_->pc_offset();
902    int cp_offset = offset + Code::kHeaderSize - kHeapObjectTag;
903    __ emit(0);
904    masm_->label_at_put(label, offset);
905    __ bind(&after_constant);
906    if (is_int16(cp_offset)) {
907      __ lw(a0, MemOperand(code_pointer(), cp_offset));
908    } else {
909      __ Addu(a0, code_pointer(), cp_offset);
910      __ lw(a0, MemOperand(a0, 0));
911    }
912  }
913  Push(a0);
914  CheckStackLimit();
915}
916
917
918void RegExpMacroAssemblerMIPS::PushCurrentPosition() {
919  Push(current_input_offset());
920}
921
922
923void RegExpMacroAssemblerMIPS::PushRegister(int register_index,
924                                           StackCheckFlag check_stack_limit) {
925  __ lw(a0, register_location(register_index));
926  Push(a0);
927  if (check_stack_limit) CheckStackLimit();
928}
929
930
931void RegExpMacroAssemblerMIPS::ReadCurrentPositionFromRegister(int reg) {
932  __ lw(current_input_offset(), register_location(reg));
933}
934
935
936void RegExpMacroAssemblerMIPS::ReadStackPointerFromRegister(int reg) {
937  __ lw(backtrack_stackpointer(), register_location(reg));
938  __ lw(a0, MemOperand(frame_pointer(), kStackHighEnd));
939  __ Addu(backtrack_stackpointer(), backtrack_stackpointer(), Operand(a0));
940}
941
942
943void RegExpMacroAssemblerMIPS::SetCurrentPositionFromEnd(int by) {
944  Label after_position;
945  __ Branch(&after_position,
946            ge,
947            current_input_offset(),
948            Operand(-by * char_size()));
949  __ li(current_input_offset(), -by * char_size());
950  // On RegExp code entry (where this operation is used), the character before
951  // the current position is expected to be already loaded.
952  // We have advanced the position, so it's safe to read backwards.
953  LoadCurrentCharacterUnchecked(-1, 1);
954  __ bind(&after_position);
955}
956
957
958void RegExpMacroAssemblerMIPS::SetRegister(int register_index, int to) {
959  ASSERT(register_index >= num_saved_registers_);  // Reserved for positions!
960  __ li(a0, Operand(to));
961  __ sw(a0, register_location(register_index));
962}
963
964
965void RegExpMacroAssemblerMIPS::Succeed() {
966  __ jmp(&success_label_);
967}
968
969
970void RegExpMacroAssemblerMIPS::WriteCurrentPositionToRegister(int reg,
971                                                             int cp_offset) {
972  if (cp_offset == 0) {
973    __ sw(current_input_offset(), register_location(reg));
974  } else {
975    __ Addu(a0, current_input_offset(), Operand(cp_offset * char_size()));
976    __ sw(a0, register_location(reg));
977  }
978}
979
980
981void RegExpMacroAssemblerMIPS::ClearRegisters(int reg_from, int reg_to) {
982  ASSERT(reg_from <= reg_to);
983  __ lw(a0, MemOperand(frame_pointer(), kInputStartMinusOne));
984  for (int reg = reg_from; reg <= reg_to; reg++) {
985    __ sw(a0, register_location(reg));
986  }
987}
988
989
990void RegExpMacroAssemblerMIPS::WriteStackPointerToRegister(int reg) {
991  __ lw(a1, MemOperand(frame_pointer(), kStackHighEnd));
992  __ Subu(a0, backtrack_stackpointer(), a1);
993  __ sw(a0, register_location(reg));
994}
995
996
997// Private methods:
998
999void RegExpMacroAssemblerMIPS::CallCheckStackGuardState(Register scratch) {
1000  static const int num_arguments = 3;
1001  __ PrepareCallCFunction(num_arguments, scratch);
1002  __ mov(a2, frame_pointer());
1003  // Code* of self.
1004  __ li(a1, Operand(masm_->CodeObject()));
1005  // a0 becomes return address pointer.
1006  ExternalReference stack_guard_check =
1007      ExternalReference::re_check_stack_guard_state(masm_->isolate());
1008  CallCFunctionUsingStub(stack_guard_check, num_arguments);
1009}
1010
1011
1012// Helper function for reading a value out of a stack frame.
1013template <typename T>
1014static T& frame_entry(Address re_frame, int frame_offset) {
1015  return reinterpret_cast<T&>(Memory::int32_at(re_frame + frame_offset));
1016}
1017
1018
1019int RegExpMacroAssemblerMIPS::CheckStackGuardState(Address* return_address,
1020                                                   Code* re_code,
1021                                                   Address re_frame) {
1022  Isolate* isolate = frame_entry<Isolate*>(re_frame, kIsolate);
1023  ASSERT(isolate == Isolate::Current());
1024  if (isolate->stack_guard()->IsStackOverflow()) {
1025    isolate->StackOverflow();
1026    return EXCEPTION;
1027  }
1028
1029  // If not real stack overflow the stack guard was used to interrupt
1030  // execution for another purpose.
1031
1032  // If this is a direct call from JavaScript retry the RegExp forcing the call
1033  // through the runtime system. Currently the direct call cannot handle a GC.
1034  if (frame_entry<int>(re_frame, kDirectCall) == 1) {
1035    return RETRY;
1036  }
1037
1038  // Prepare for possible GC.
1039  HandleScope handles(isolate);
1040  Handle<Code> code_handle(re_code);
1041
1042  Handle<String> subject(frame_entry<String*>(re_frame, kInputString));
1043  // Current string.
1044  bool is_ascii = subject->IsAsciiRepresentationUnderneath();
1045
1046  ASSERT(re_code->instruction_start() <= *return_address);
1047  ASSERT(*return_address <=
1048      re_code->instruction_start() + re_code->instruction_size());
1049
1050  MaybeObject* result = Execution::HandleStackGuardInterrupt();
1051
1052  if (*code_handle != re_code) {  // Return address no longer valid.
1053    int delta = code_handle->address() - re_code->address();
1054    // Overwrite the return address on the stack.
1055    *return_address += delta;
1056  }
1057
1058  if (result->IsException()) {
1059    return EXCEPTION;
1060  }
1061
1062  Handle<String> subject_tmp = subject;
1063  int slice_offset = 0;
1064
1065  // Extract the underlying string and the slice offset.
1066  if (StringShape(*subject_tmp).IsCons()) {
1067    subject_tmp = Handle<String>(ConsString::cast(*subject_tmp)->first());
1068  } else if (StringShape(*subject_tmp).IsSliced()) {
1069    SlicedString* slice = SlicedString::cast(*subject_tmp);
1070    subject_tmp = Handle<String>(slice->parent());
1071    slice_offset = slice->offset();
1072  }
1073
1074  // String might have changed.
1075  if (subject_tmp->IsAsciiRepresentation() != is_ascii) {
1076    // If we changed between an ASCII and an UC16 string, the specialized
1077    // code cannot be used, and we need to restart regexp matching from
1078    // scratch (including, potentially, compiling a new version of the code).
1079    return RETRY;
1080  }
1081
1082  // Otherwise, the content of the string might have moved. It must still
1083  // be a sequential or external string with the same content.
1084  // Update the start and end pointers in the stack frame to the current
1085  // location (whether it has actually moved or not).
1086  ASSERT(StringShape(*subject_tmp).IsSequential() ||
1087      StringShape(*subject_tmp).IsExternal());
1088
1089  // The original start address of the characters to match.
1090  const byte* start_address = frame_entry<const byte*>(re_frame, kInputStart);
1091
1092  // Find the current start address of the same character at the current string
1093  // position.
1094  int start_index = frame_entry<int>(re_frame, kStartIndex);
1095  const byte* new_address = StringCharacterPosition(*subject_tmp,
1096                                                    start_index + slice_offset);
1097
1098  if (start_address != new_address) {
1099    // If there is a difference, update the object pointer and start and end
1100    // addresses in the RegExp stack frame to match the new value.
1101    const byte* end_address = frame_entry<const byte* >(re_frame, kInputEnd);
1102    int byte_length = static_cast<int>(end_address - start_address);
1103    frame_entry<const String*>(re_frame, kInputString) = *subject;
1104    frame_entry<const byte*>(re_frame, kInputStart) = new_address;
1105    frame_entry<const byte*>(re_frame, kInputEnd) = new_address + byte_length;
1106  }
1107
1108  return 0;
1109}
1110
1111
1112MemOperand RegExpMacroAssemblerMIPS::register_location(int register_index) {
1113  ASSERT(register_index < (1<<30));
1114  if (num_registers_ <= register_index) {
1115    num_registers_ = register_index + 1;
1116  }
1117  return MemOperand(frame_pointer(),
1118                    kRegisterZero - register_index * kPointerSize);
1119}
1120
1121
1122void RegExpMacroAssemblerMIPS::CheckPosition(int cp_offset,
1123                                            Label* on_outside_input) {
1124  BranchOrBacktrack(on_outside_input,
1125                    ge,
1126                    current_input_offset(),
1127                    Operand(-cp_offset * char_size()));
1128}
1129
1130
1131void RegExpMacroAssemblerMIPS::BranchOrBacktrack(Label* to,
1132                                                 Condition condition,
1133                                                 Register rs,
1134                                                 const Operand& rt) {
1135  if (condition == al) {  // Unconditional.
1136    if (to == NULL) {
1137      Backtrack();
1138      return;
1139    }
1140    __ jmp(to);
1141    return;
1142  }
1143  if (to == NULL) {
1144    __ Branch(&backtrack_label_, condition, rs, rt);
1145    return;
1146  }
1147  __ Branch(to, condition, rs, rt);
1148}
1149
1150
1151void RegExpMacroAssemblerMIPS::SafeCall(Label* to, Condition cond, Register rs,
1152                                           const Operand& rt) {
1153  __ BranchAndLink(to, cond, rs, rt);
1154}
1155
1156
1157void RegExpMacroAssemblerMIPS::SafeReturn() {
1158  __ pop(ra);
1159  __ Addu(t5, ra, Operand(masm_->CodeObject()));
1160  __ Jump(t5);
1161}
1162
1163
1164void RegExpMacroAssemblerMIPS::SafeCallTarget(Label* name) {
1165  __ bind(name);
1166  __ Subu(ra, ra, Operand(masm_->CodeObject()));
1167  __ push(ra);
1168}
1169
1170
1171void RegExpMacroAssemblerMIPS::Push(Register source) {
1172  ASSERT(!source.is(backtrack_stackpointer()));
1173  __ Addu(backtrack_stackpointer(),
1174          backtrack_stackpointer(),
1175          Operand(-kPointerSize));
1176  __ sw(source, MemOperand(backtrack_stackpointer()));
1177}
1178
1179
1180void RegExpMacroAssemblerMIPS::Pop(Register target) {
1181  ASSERT(!target.is(backtrack_stackpointer()));
1182  __ lw(target, MemOperand(backtrack_stackpointer()));
1183  __ Addu(backtrack_stackpointer(), backtrack_stackpointer(), kPointerSize);
1184}
1185
1186
1187void RegExpMacroAssemblerMIPS::CheckPreemption() {
1188  // Check for preemption.
1189  ExternalReference stack_limit =
1190      ExternalReference::address_of_stack_limit(masm_->isolate());
1191  __ li(a0, Operand(stack_limit));
1192  __ lw(a0, MemOperand(a0));
1193  SafeCall(&check_preempt_label_, ls, sp, Operand(a0));
1194}
1195
1196
1197void RegExpMacroAssemblerMIPS::CheckStackLimit() {
1198  ExternalReference stack_limit =
1199      ExternalReference::address_of_regexp_stack_limit(masm_->isolate());
1200
1201  __ li(a0, Operand(stack_limit));
1202  __ lw(a0, MemOperand(a0));
1203  SafeCall(&stack_overflow_label_, ls, backtrack_stackpointer(), Operand(a0));
1204}
1205
1206
1207void RegExpMacroAssemblerMIPS::CallCFunctionUsingStub(
1208    ExternalReference function,
1209    int num_arguments) {
1210  // Must pass all arguments in registers. The stub pushes on the stack.
1211  ASSERT(num_arguments <= 4);
1212  __ li(code_pointer(), Operand(function));
1213  RegExpCEntryStub stub;
1214  __ CallStub(&stub);
1215  if (OS::ActivationFrameAlignment() != 0) {
1216    __ lw(sp, MemOperand(sp, 16));
1217  }
1218  __ li(code_pointer(), Operand(masm_->CodeObject()));
1219}
1220
1221
1222void RegExpMacroAssemblerMIPS::LoadCurrentCharacterUnchecked(int cp_offset,
1223                                                            int characters) {
1224  Register offset = current_input_offset();
1225  if (cp_offset != 0) {
1226    __ Addu(a0, current_input_offset(), Operand(cp_offset * char_size()));
1227    offset = a0;
1228  }
1229  // We assume that we cannot do unaligned loads on MIPS, so this function
1230  // must only be used to load a single character at a time.
1231  ASSERT(characters == 1);
1232  __ Addu(t5, end_of_input_address(), Operand(offset));
1233  if (mode_ == ASCII) {
1234    __ lbu(current_character(), MemOperand(t5, 0));
1235  } else {
1236    ASSERT(mode_ == UC16);
1237    __ lhu(current_character(), MemOperand(t5, 0));
1238  }
1239}
1240
1241
1242void RegExpCEntryStub::Generate(MacroAssembler* masm_) {
1243  int stack_alignment = OS::ActivationFrameAlignment();
1244  if (stack_alignment < kPointerSize) stack_alignment = kPointerSize;
1245  // Stack is already aligned for call, so decrement by alignment
1246  // to make room for storing the return address.
1247  __ Subu(sp, sp, Operand(stack_alignment));
1248  __ sw(ra, MemOperand(sp, 0));
1249  __ mov(a0, sp);
1250  __ mov(t9, t1);
1251  __ Call(t9);
1252  __ lw(ra, MemOperand(sp, 0));
1253  __ Addu(sp, sp, Operand(stack_alignment));
1254  __ Jump(ra);
1255}
1256
1257
1258#undef __
1259
1260#endif  // V8_INTERPRETED_REGEXP
1261
1262}}  // namespace v8::internal
1263
1264#endif  // V8_TARGET_ARCH_MIPS
1265