11cb0ef41Sopenharmony_ci// Copyright 2012 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci// found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci#if V8_TARGET_ARCH_MIPS64
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci#include "src/regexp/mips64/regexp-macro-assembler-mips64.h"
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci#include "src/codegen/macro-assembler.h"
101cb0ef41Sopenharmony_ci#include "src/codegen/mips64/assembler-mips64-inl.h"
111cb0ef41Sopenharmony_ci#include "src/heap/factory.h"
121cb0ef41Sopenharmony_ci#include "src/logging/log.h"
131cb0ef41Sopenharmony_ci#include "src/objects/code-inl.h"
141cb0ef41Sopenharmony_ci#include "src/regexp/regexp-stack.h"
151cb0ef41Sopenharmony_ci#include "src/snapshot/embedded/embedded-data-inl.h"
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_cinamespace v8 {
181cb0ef41Sopenharmony_cinamespace internal {
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci/* clang-format off
211cb0ef41Sopenharmony_ci *
221cb0ef41Sopenharmony_ci * This assembler uses the following register assignment convention
231cb0ef41Sopenharmony_ci * - s0 : Unused.
241cb0ef41Sopenharmony_ci * - s1 : Pointer to current Code object including heap object tag.
251cb0ef41Sopenharmony_ci * - s2 : Current position in input, as negative offset from end of string.
261cb0ef41Sopenharmony_ci *        Please notice that this is the byte offset, not the character offset!
271cb0ef41Sopenharmony_ci * - s5 : Currently loaded character. Must be loaded using
281cb0ef41Sopenharmony_ci *        LoadCurrentCharacter before using any of the dispatch methods.
291cb0ef41Sopenharmony_ci * - s6 : Points to tip of backtrack stack
301cb0ef41Sopenharmony_ci * - s7 : End of input (points to byte after last character in input).
311cb0ef41Sopenharmony_ci * - fp : Frame pointer. Used to access arguments, local variables and
321cb0ef41Sopenharmony_ci *        RegExp registers.
331cb0ef41Sopenharmony_ci * - sp : Points to tip of C stack.
341cb0ef41Sopenharmony_ci *
351cb0ef41Sopenharmony_ci * The remaining registers are free for computations.
361cb0ef41Sopenharmony_ci * Each call to a public method should retain this convention.
371cb0ef41Sopenharmony_ci *
381cb0ef41Sopenharmony_ci * TODO(plind): O32 documented here with intent of having single 32/64 codebase
391cb0ef41Sopenharmony_ci *              in the future.
401cb0ef41Sopenharmony_ci *
411cb0ef41Sopenharmony_ci * The O32 stack will have the following structure:
421cb0ef41Sopenharmony_ci *
431cb0ef41Sopenharmony_ci *  - fp[72]  Isolate* isolate   (address of the current isolate)
441cb0ef41Sopenharmony_ci *  - fp[68]  direct_call  (if 1, direct call from JavaScript code,
451cb0ef41Sopenharmony_ci *                          if 0, call through the runtime system).
461cb0ef41Sopenharmony_ci *  - fp[64]  stack_area_base (High end of the memory area to use as
471cb0ef41Sopenharmony_ci *                             backtracking stack).
481cb0ef41Sopenharmony_ci *  - fp[60]  capture array size (may fit multiple sets of matches)
491cb0ef41Sopenharmony_ci *  - fp[44..59]  MIPS O32 four argument slots
501cb0ef41Sopenharmony_ci *  - fp[40]  int* capture_array (int[num_saved_registers_], for output).
511cb0ef41Sopenharmony_ci *  --- sp when called ---
521cb0ef41Sopenharmony_ci *  - fp[36]  return address      (lr).
531cb0ef41Sopenharmony_ci *  - fp[32]  old frame pointer   (r11).
541cb0ef41Sopenharmony_ci *  - fp[0..31]  backup of registers s0..s7.
551cb0ef41Sopenharmony_ci *  --- frame pointer ----
561cb0ef41Sopenharmony_ci *  - fp[-4]  end of input       (address of end of string).
571cb0ef41Sopenharmony_ci *  - fp[-8]  start of input     (address of first character in string).
581cb0ef41Sopenharmony_ci *  - fp[-12] start index        (character index of start).
591cb0ef41Sopenharmony_ci *  - fp[-16] void* input_string (location of a handle containing the string).
601cb0ef41Sopenharmony_ci *  - fp[-20] success counter    (only for global regexps to count matches).
611cb0ef41Sopenharmony_ci *  - fp[-24] Offset of location before start of input (effectively character
621cb0ef41Sopenharmony_ci *            string start - 1). Used to initialize capture registers to a
631cb0ef41Sopenharmony_ci *            non-position.
641cb0ef41Sopenharmony_ci *  - fp[-28] At start (if 1, we are starting at the start of the
651cb0ef41Sopenharmony_ci *    string, otherwise 0)
661cb0ef41Sopenharmony_ci *  - fp[-32] register 0         (Only positions must be stored in the first
671cb0ef41Sopenharmony_ci *  -         register 1          num_saved_registers_ registers)
681cb0ef41Sopenharmony_ci *  -         ...
691cb0ef41Sopenharmony_ci *  -         register num_registers-1
701cb0ef41Sopenharmony_ci *  --- sp ---
711cb0ef41Sopenharmony_ci *
721cb0ef41Sopenharmony_ci *
731cb0ef41Sopenharmony_ci * The N64 stack will have the following structure:
741cb0ef41Sopenharmony_ci *
751cb0ef41Sopenharmony_ci *  - fp[80]  Isolate* isolate   (address of the current isolate)               kIsolate
761cb0ef41Sopenharmony_ci *                                                                              kStackFrameHeader
771cb0ef41Sopenharmony_ci *  --- sp when called ---
781cb0ef41Sopenharmony_ci *  - fp[72]  ra                 Return from RegExp code (ra).                  kReturnAddress
791cb0ef41Sopenharmony_ci *  - fp[64]  s9, old-fp         Old fp, callee saved(s9).
801cb0ef41Sopenharmony_ci *  - fp[0..63]  s0..s7          Callee-saved registers s0..s7.
811cb0ef41Sopenharmony_ci *  --- frame pointer ----
821cb0ef41Sopenharmony_ci *  - fp[-8]  direct_call        (1 = direct call from JS, 0 = from runtime)    kDirectCall
831cb0ef41Sopenharmony_ci *  - fp[-16] capture array size (may fit multiple sets of matches)             kNumOutputRegisters
841cb0ef41Sopenharmony_ci *  - fp[-24] int* capture_array (int[num_saved_registers_], for output).       kRegisterOutput
851cb0ef41Sopenharmony_ci *  - fp[-32] end of input       (address of end of string).                    kInputEnd
861cb0ef41Sopenharmony_ci *  - fp[-40] start of input     (address of first character in string).        kInputStart
871cb0ef41Sopenharmony_ci *  - fp[-48] start index        (character index of start).                    kStartIndex
881cb0ef41Sopenharmony_ci *  - fp[-56] void* input_string (location of a handle containing the string).  kInputString
891cb0ef41Sopenharmony_ci *  - fp[-64] success counter    (only for global regexps to count matches).    kSuccessfulCaptures
901cb0ef41Sopenharmony_ci *  - fp[-72] Offset of location before start of input (effectively character   kStringStartMinusOne
911cb0ef41Sopenharmony_ci *            position -1). Used to initialize capture registers to a
921cb0ef41Sopenharmony_ci *            non-position.
931cb0ef41Sopenharmony_ci *  --------- The following output registers are 32-bit values. ---------
941cb0ef41Sopenharmony_ci *  - fp[-80] register 0         (Only positions must be stored in the first    kRegisterZero
951cb0ef41Sopenharmony_ci *  -         register 1          num_saved_registers_ registers)
961cb0ef41Sopenharmony_ci *  -         ...
971cb0ef41Sopenharmony_ci *  -         register num_registers-1
981cb0ef41Sopenharmony_ci *  --- sp ---
991cb0ef41Sopenharmony_ci *
1001cb0ef41Sopenharmony_ci * The first num_saved_registers_ registers are initialized to point to
1011cb0ef41Sopenharmony_ci * "character -1" in the string (i.e., char_size() bytes before the first
1021cb0ef41Sopenharmony_ci * character of the string). The remaining registers start out as garbage.
1031cb0ef41Sopenharmony_ci *
1041cb0ef41Sopenharmony_ci * The data up to the return address must be placed there by the calling
1051cb0ef41Sopenharmony_ci * code and the remaining arguments are passed in registers, e.g. by calling the
1061cb0ef41Sopenharmony_ci * code entry as cast to a function with the signature:
1071cb0ef41Sopenharmony_ci * int (*match)(String input_string,
1081cb0ef41Sopenharmony_ci *              int start_index,
1091cb0ef41Sopenharmony_ci *              Address start,
1101cb0ef41Sopenharmony_ci *              Address end,
1111cb0ef41Sopenharmony_ci *              int* capture_output_array,
1121cb0ef41Sopenharmony_ci *              int num_capture_registers,
1131cb0ef41Sopenharmony_ci *              bool direct_call = false,
1141cb0ef41Sopenharmony_ci *              Isolate* isolate);
1151cb0ef41Sopenharmony_ci * The call is performed by NativeRegExpMacroAssembler::Execute()
1161cb0ef41Sopenharmony_ci * (in regexp-macro-assembler.cc) via the GeneratedCode wrapper.
1171cb0ef41Sopenharmony_ci *
1181cb0ef41Sopenharmony_ci * clang-format on
1191cb0ef41Sopenharmony_ci */
1201cb0ef41Sopenharmony_ci
1211cb0ef41Sopenharmony_ci#define __ ACCESS_MASM(masm_)
1221cb0ef41Sopenharmony_ci
1231cb0ef41Sopenharmony_ciconst int RegExpMacroAssemblerMIPS::kRegExpCodeSize;
1241cb0ef41Sopenharmony_ci
1251cb0ef41Sopenharmony_ciRegExpMacroAssemblerMIPS::RegExpMacroAssemblerMIPS(Isolate* isolate, Zone* zone,
1261cb0ef41Sopenharmony_ci                                                   Mode mode,
1271cb0ef41Sopenharmony_ci                                                   int registers_to_save)
1281cb0ef41Sopenharmony_ci    : NativeRegExpMacroAssembler(isolate, zone),
1291cb0ef41Sopenharmony_ci      masm_(std::make_unique<MacroAssembler>(
1301cb0ef41Sopenharmony_ci          isolate, CodeObjectRequired::kYes,
1311cb0ef41Sopenharmony_ci          NewAssemblerBuffer(kRegExpCodeSize))),
1321cb0ef41Sopenharmony_ci      no_root_array_scope_(masm_.get()),
1331cb0ef41Sopenharmony_ci      mode_(mode),
1341cb0ef41Sopenharmony_ci      num_registers_(registers_to_save),
1351cb0ef41Sopenharmony_ci      num_saved_registers_(registers_to_save),
1361cb0ef41Sopenharmony_ci      entry_label_(),
1371cb0ef41Sopenharmony_ci      start_label_(),
1381cb0ef41Sopenharmony_ci      success_label_(),
1391cb0ef41Sopenharmony_ci      backtrack_label_(),
1401cb0ef41Sopenharmony_ci      exit_label_(),
1411cb0ef41Sopenharmony_ci      internal_failure_label_() {
1421cb0ef41Sopenharmony_ci  DCHECK_EQ(0, registers_to_save % 2);
1431cb0ef41Sopenharmony_ci  __ jmp(&entry_label_);   // We'll write the entry code later.
1441cb0ef41Sopenharmony_ci  // If the code gets too big or corrupted, an internal exception will be
1451cb0ef41Sopenharmony_ci  // raised, and we will exit right away.
1461cb0ef41Sopenharmony_ci  __ bind(&internal_failure_label_);
1471cb0ef41Sopenharmony_ci  __ li(v0, Operand(FAILURE));
1481cb0ef41Sopenharmony_ci  __ Ret();
1491cb0ef41Sopenharmony_ci  __ bind(&start_label_);  // And then continue from here.
1501cb0ef41Sopenharmony_ci}
1511cb0ef41Sopenharmony_ci
1521cb0ef41Sopenharmony_ciRegExpMacroAssemblerMIPS::~RegExpMacroAssemblerMIPS() {
1531cb0ef41Sopenharmony_ci  // Unuse labels in case we throw away the assembler without calling GetCode.
1541cb0ef41Sopenharmony_ci  entry_label_.Unuse();
1551cb0ef41Sopenharmony_ci  start_label_.Unuse();
1561cb0ef41Sopenharmony_ci  success_label_.Unuse();
1571cb0ef41Sopenharmony_ci  backtrack_label_.Unuse();
1581cb0ef41Sopenharmony_ci  exit_label_.Unuse();
1591cb0ef41Sopenharmony_ci  check_preempt_label_.Unuse();
1601cb0ef41Sopenharmony_ci  stack_overflow_label_.Unuse();
1611cb0ef41Sopenharmony_ci  internal_failure_label_.Unuse();
1621cb0ef41Sopenharmony_ci  fallback_label_.Unuse();
1631cb0ef41Sopenharmony_ci}
1641cb0ef41Sopenharmony_ci
1651cb0ef41Sopenharmony_ci
1661cb0ef41Sopenharmony_ciint RegExpMacroAssemblerMIPS::stack_limit_slack()  {
1671cb0ef41Sopenharmony_ci  return RegExpStack::kStackLimitSlack;
1681cb0ef41Sopenharmony_ci}
1691cb0ef41Sopenharmony_ci
1701cb0ef41Sopenharmony_ci
1711cb0ef41Sopenharmony_civoid RegExpMacroAssemblerMIPS::AdvanceCurrentPosition(int by) {
1721cb0ef41Sopenharmony_ci  if (by != 0) {
1731cb0ef41Sopenharmony_ci    __ Daddu(current_input_offset(),
1741cb0ef41Sopenharmony_ci            current_input_offset(), Operand(by * char_size()));
1751cb0ef41Sopenharmony_ci  }
1761cb0ef41Sopenharmony_ci}
1771cb0ef41Sopenharmony_ci
1781cb0ef41Sopenharmony_ci
1791cb0ef41Sopenharmony_civoid RegExpMacroAssemblerMIPS::AdvanceRegister(int reg, int by) {
1801cb0ef41Sopenharmony_ci  DCHECK_LE(0, reg);
1811cb0ef41Sopenharmony_ci  DCHECK_GT(num_registers_, reg);
1821cb0ef41Sopenharmony_ci  if (by != 0) {
1831cb0ef41Sopenharmony_ci    __ Ld(a0, register_location(reg));
1841cb0ef41Sopenharmony_ci    __ Daddu(a0, a0, Operand(by));
1851cb0ef41Sopenharmony_ci    __ Sd(a0, register_location(reg));
1861cb0ef41Sopenharmony_ci  }
1871cb0ef41Sopenharmony_ci}
1881cb0ef41Sopenharmony_ci
1891cb0ef41Sopenharmony_ci
1901cb0ef41Sopenharmony_civoid RegExpMacroAssemblerMIPS::Backtrack() {
1911cb0ef41Sopenharmony_ci  CheckPreemption();
1921cb0ef41Sopenharmony_ci  if (has_backtrack_limit()) {
1931cb0ef41Sopenharmony_ci    Label next;
1941cb0ef41Sopenharmony_ci    __ Ld(a0, MemOperand(frame_pointer(), kBacktrackCount));
1951cb0ef41Sopenharmony_ci    __ Daddu(a0, a0, Operand(1));
1961cb0ef41Sopenharmony_ci    __ Sd(a0, MemOperand(frame_pointer(), kBacktrackCount));
1971cb0ef41Sopenharmony_ci    __ Branch(&next, ne, a0, Operand(backtrack_limit()));
1981cb0ef41Sopenharmony_ci
1991cb0ef41Sopenharmony_ci    // Backtrack limit exceeded.
2001cb0ef41Sopenharmony_ci    if (can_fallback()) {
2011cb0ef41Sopenharmony_ci      __ jmp(&fallback_label_);
2021cb0ef41Sopenharmony_ci    } else {
2031cb0ef41Sopenharmony_ci      // Can't fallback, so we treat it as a failed match.
2041cb0ef41Sopenharmony_ci      Fail();
2051cb0ef41Sopenharmony_ci    }
2061cb0ef41Sopenharmony_ci
2071cb0ef41Sopenharmony_ci    __ bind(&next);
2081cb0ef41Sopenharmony_ci  }
2091cb0ef41Sopenharmony_ci  // Pop Code offset from backtrack stack, add Code and jump to location.
2101cb0ef41Sopenharmony_ci  Pop(a0);
2111cb0ef41Sopenharmony_ci  __ Daddu(a0, a0, code_pointer());
2121cb0ef41Sopenharmony_ci  __ Jump(a0);
2131cb0ef41Sopenharmony_ci}
2141cb0ef41Sopenharmony_ci
2151cb0ef41Sopenharmony_ci
2161cb0ef41Sopenharmony_civoid RegExpMacroAssemblerMIPS::Bind(Label* label) {
2171cb0ef41Sopenharmony_ci  __ bind(label);
2181cb0ef41Sopenharmony_ci}
2191cb0ef41Sopenharmony_ci
2201cb0ef41Sopenharmony_ci
2211cb0ef41Sopenharmony_civoid RegExpMacroAssemblerMIPS::CheckCharacter(uint32_t c, Label* on_equal) {
2221cb0ef41Sopenharmony_ci  BranchOrBacktrack(on_equal, eq, current_character(), Operand(c));
2231cb0ef41Sopenharmony_ci}
2241cb0ef41Sopenharmony_ci
2251cb0ef41Sopenharmony_civoid RegExpMacroAssemblerMIPS::CheckCharacterGT(base::uc16 limit,
2261cb0ef41Sopenharmony_ci                                                Label* on_greater) {
2271cb0ef41Sopenharmony_ci  BranchOrBacktrack(on_greater, gt, current_character(), Operand(limit));
2281cb0ef41Sopenharmony_ci}
2291cb0ef41Sopenharmony_ci
2301cb0ef41Sopenharmony_civoid RegExpMacroAssemblerMIPS::CheckAtStart(int cp_offset, Label* on_at_start) {
2311cb0ef41Sopenharmony_ci  __ Ld(a1, MemOperand(frame_pointer(), kStringStartMinusOne));
2321cb0ef41Sopenharmony_ci  __ Daddu(a0, current_input_offset(),
2331cb0ef41Sopenharmony_ci           Operand(-char_size() + cp_offset * char_size()));
2341cb0ef41Sopenharmony_ci  BranchOrBacktrack(on_at_start, eq, a0, Operand(a1));
2351cb0ef41Sopenharmony_ci}
2361cb0ef41Sopenharmony_ci
2371cb0ef41Sopenharmony_ci
2381cb0ef41Sopenharmony_civoid RegExpMacroAssemblerMIPS::CheckNotAtStart(int cp_offset,
2391cb0ef41Sopenharmony_ci                                               Label* on_not_at_start) {
2401cb0ef41Sopenharmony_ci  __ Ld(a1, MemOperand(frame_pointer(), kStringStartMinusOne));
2411cb0ef41Sopenharmony_ci  __ Daddu(a0, current_input_offset(),
2421cb0ef41Sopenharmony_ci           Operand(-char_size() + cp_offset * char_size()));
2431cb0ef41Sopenharmony_ci  BranchOrBacktrack(on_not_at_start, ne, a0, Operand(a1));
2441cb0ef41Sopenharmony_ci}
2451cb0ef41Sopenharmony_ci
2461cb0ef41Sopenharmony_civoid RegExpMacroAssemblerMIPS::CheckCharacterLT(base::uc16 limit,
2471cb0ef41Sopenharmony_ci                                                Label* on_less) {
2481cb0ef41Sopenharmony_ci  BranchOrBacktrack(on_less, lt, current_character(), Operand(limit));
2491cb0ef41Sopenharmony_ci}
2501cb0ef41Sopenharmony_ci
2511cb0ef41Sopenharmony_civoid RegExpMacroAssemblerMIPS::CheckGreedyLoop(Label* on_equal) {
2521cb0ef41Sopenharmony_ci  Label backtrack_non_equal;
2531cb0ef41Sopenharmony_ci  __ Lw(a0, MemOperand(backtrack_stackpointer(), 0));
2541cb0ef41Sopenharmony_ci  __ Branch(&backtrack_non_equal, ne, current_input_offset(), Operand(a0));
2551cb0ef41Sopenharmony_ci  __ Daddu(backtrack_stackpointer(),
2561cb0ef41Sopenharmony_ci          backtrack_stackpointer(),
2571cb0ef41Sopenharmony_ci          Operand(kIntSize));
2581cb0ef41Sopenharmony_ci  __ bind(&backtrack_non_equal);
2591cb0ef41Sopenharmony_ci  BranchOrBacktrack(on_equal, eq, current_input_offset(), Operand(a0));
2601cb0ef41Sopenharmony_ci}
2611cb0ef41Sopenharmony_ci
2621cb0ef41Sopenharmony_civoid RegExpMacroAssemblerMIPS::CheckNotBackReferenceIgnoreCase(
2631cb0ef41Sopenharmony_ci    int start_reg, bool read_backward, bool unicode, Label* on_no_match) {
2641cb0ef41Sopenharmony_ci  Label fallthrough;
2651cb0ef41Sopenharmony_ci  __ Ld(a0, register_location(start_reg));      // Index of start of capture.
2661cb0ef41Sopenharmony_ci  __ Ld(a1, register_location(start_reg + 1));  // Index of end of capture.
2671cb0ef41Sopenharmony_ci  __ Dsubu(a1, a1, a0);  // Length of capture.
2681cb0ef41Sopenharmony_ci
2691cb0ef41Sopenharmony_ci  // At this point, the capture registers are either both set or both cleared.
2701cb0ef41Sopenharmony_ci  // If the capture length is zero, then the capture is either empty or cleared.
2711cb0ef41Sopenharmony_ci  // Fall through in both cases.
2721cb0ef41Sopenharmony_ci  __ Branch(&fallthrough, eq, a1, Operand(zero_reg));
2731cb0ef41Sopenharmony_ci
2741cb0ef41Sopenharmony_ci  if (read_backward) {
2751cb0ef41Sopenharmony_ci    __ Ld(t1, MemOperand(frame_pointer(), kStringStartMinusOne));
2761cb0ef41Sopenharmony_ci    __ Daddu(t1, t1, a1);
2771cb0ef41Sopenharmony_ci    BranchOrBacktrack(on_no_match, le, current_input_offset(), Operand(t1));
2781cb0ef41Sopenharmony_ci  } else {
2791cb0ef41Sopenharmony_ci    __ Daddu(t1, a1, current_input_offset());
2801cb0ef41Sopenharmony_ci    // Check that there are enough characters left in the input.
2811cb0ef41Sopenharmony_ci    BranchOrBacktrack(on_no_match, gt, t1, Operand(zero_reg));
2821cb0ef41Sopenharmony_ci  }
2831cb0ef41Sopenharmony_ci
2841cb0ef41Sopenharmony_ci  if (mode_ == LATIN1) {
2851cb0ef41Sopenharmony_ci    Label success;
2861cb0ef41Sopenharmony_ci    Label fail;
2871cb0ef41Sopenharmony_ci    Label loop_check;
2881cb0ef41Sopenharmony_ci
2891cb0ef41Sopenharmony_ci    // a0 - offset of start of capture.
2901cb0ef41Sopenharmony_ci    // a1 - length of capture.
2911cb0ef41Sopenharmony_ci    __ Daddu(a0, a0, Operand(end_of_input_address()));
2921cb0ef41Sopenharmony_ci    __ Daddu(a2, end_of_input_address(), Operand(current_input_offset()));
2931cb0ef41Sopenharmony_ci    if (read_backward) {
2941cb0ef41Sopenharmony_ci      __ Dsubu(a2, a2, Operand(a1));
2951cb0ef41Sopenharmony_ci    }
2961cb0ef41Sopenharmony_ci    __ Daddu(a1, a0, Operand(a1));
2971cb0ef41Sopenharmony_ci
2981cb0ef41Sopenharmony_ci    // a0 - Address of start of capture.
2991cb0ef41Sopenharmony_ci    // a1 - Address of end of capture.
3001cb0ef41Sopenharmony_ci    // a2 - Address of current input position.
3011cb0ef41Sopenharmony_ci
3021cb0ef41Sopenharmony_ci    Label loop;
3031cb0ef41Sopenharmony_ci    __ bind(&loop);
3041cb0ef41Sopenharmony_ci    __ Lbu(a3, MemOperand(a0, 0));
3051cb0ef41Sopenharmony_ci    __ daddiu(a0, a0, char_size());
3061cb0ef41Sopenharmony_ci    __ Lbu(a4, MemOperand(a2, 0));
3071cb0ef41Sopenharmony_ci    __ daddiu(a2, a2, char_size());
3081cb0ef41Sopenharmony_ci
3091cb0ef41Sopenharmony_ci    __ Branch(&loop_check, eq, a4, Operand(a3));
3101cb0ef41Sopenharmony_ci
3111cb0ef41Sopenharmony_ci    // Mismatch, try case-insensitive match (converting letters to lower-case).
3121cb0ef41Sopenharmony_ci    __ Or(a3, a3, Operand(0x20));  // Convert capture character to lower-case.
3131cb0ef41Sopenharmony_ci    __ Or(a4, a4, Operand(0x20));  // Also convert input character.
3141cb0ef41Sopenharmony_ci    __ Branch(&fail, ne, a4, Operand(a3));
3151cb0ef41Sopenharmony_ci    __ Dsubu(a3, a3, Operand('a'));
3161cb0ef41Sopenharmony_ci    __ Branch(&loop_check, ls, a3, Operand('z' - 'a'));
3171cb0ef41Sopenharmony_ci    // Latin-1: Check for values in range [224,254] but not 247.
3181cb0ef41Sopenharmony_ci    __ Dsubu(a3, a3, Operand(224 - 'a'));
3191cb0ef41Sopenharmony_ci    // Weren't Latin-1 letters.
3201cb0ef41Sopenharmony_ci    __ Branch(&fail, hi, a3, Operand(254 - 224));
3211cb0ef41Sopenharmony_ci    // Check for 247.
3221cb0ef41Sopenharmony_ci    __ Branch(&fail, eq, a3, Operand(247 - 224));
3231cb0ef41Sopenharmony_ci
3241cb0ef41Sopenharmony_ci    __ bind(&loop_check);
3251cb0ef41Sopenharmony_ci    __ Branch(&loop, lt, a0, Operand(a1));
3261cb0ef41Sopenharmony_ci    __ jmp(&success);
3271cb0ef41Sopenharmony_ci
3281cb0ef41Sopenharmony_ci    __ bind(&fail);
3291cb0ef41Sopenharmony_ci    GoTo(on_no_match);
3301cb0ef41Sopenharmony_ci
3311cb0ef41Sopenharmony_ci    __ bind(&success);
3321cb0ef41Sopenharmony_ci    // Compute new value of character position after the matched part.
3331cb0ef41Sopenharmony_ci    __ Dsubu(current_input_offset(), a2, end_of_input_address());
3341cb0ef41Sopenharmony_ci    if (read_backward) {
3351cb0ef41Sopenharmony_ci      __ Ld(t1, register_location(start_reg));  // Index of start of capture.
3361cb0ef41Sopenharmony_ci      __ Ld(a2, register_location(start_reg + 1));  // Index of end of capture.
3371cb0ef41Sopenharmony_ci      __ Daddu(current_input_offset(), current_input_offset(), Operand(t1));
3381cb0ef41Sopenharmony_ci      __ Dsubu(current_input_offset(), current_input_offset(), Operand(a2));
3391cb0ef41Sopenharmony_ci    }
3401cb0ef41Sopenharmony_ci  } else {
3411cb0ef41Sopenharmony_ci    DCHECK(mode_ == UC16);
3421cb0ef41Sopenharmony_ci
3431cb0ef41Sopenharmony_ci    int argument_count = 4;
3441cb0ef41Sopenharmony_ci    __ PrepareCallCFunction(argument_count, a2);
3451cb0ef41Sopenharmony_ci
3461cb0ef41Sopenharmony_ci    // a0 - offset of start of capture.
3471cb0ef41Sopenharmony_ci    // a1 - length of capture.
3481cb0ef41Sopenharmony_ci
3491cb0ef41Sopenharmony_ci    // Put arguments into arguments registers.
3501cb0ef41Sopenharmony_ci    // Parameters are
3511cb0ef41Sopenharmony_ci    //   a0: Address byte_offset1 - Address captured substring's start.
3521cb0ef41Sopenharmony_ci    //   a1: Address byte_offset2 - Address of current character position.
3531cb0ef41Sopenharmony_ci    //   a2: size_t byte_length - length of capture in bytes(!).
3541cb0ef41Sopenharmony_ci    //   a3: Isolate* isolate.
3551cb0ef41Sopenharmony_ci
3561cb0ef41Sopenharmony_ci    // Address of start of capture.
3571cb0ef41Sopenharmony_ci    __ Daddu(a0, a0, Operand(end_of_input_address()));
3581cb0ef41Sopenharmony_ci    // Length of capture.
3591cb0ef41Sopenharmony_ci    __ mov(a2, a1);
3601cb0ef41Sopenharmony_ci    // Save length in callee-save register for use on return.
3611cb0ef41Sopenharmony_ci    __ mov(s3, a1);
3621cb0ef41Sopenharmony_ci    // Address of current input position.
3631cb0ef41Sopenharmony_ci    __ Daddu(a1, current_input_offset(), Operand(end_of_input_address()));
3641cb0ef41Sopenharmony_ci    if (read_backward) {
3651cb0ef41Sopenharmony_ci      __ Dsubu(a1, a1, Operand(s3));
3661cb0ef41Sopenharmony_ci    }
3671cb0ef41Sopenharmony_ci    // Isolate.
3681cb0ef41Sopenharmony_ci    __ li(a3, Operand(ExternalReference::isolate_address(masm_->isolate())));
3691cb0ef41Sopenharmony_ci
3701cb0ef41Sopenharmony_ci    {
3711cb0ef41Sopenharmony_ci      AllowExternalCallThatCantCauseGC scope(masm_.get());
3721cb0ef41Sopenharmony_ci      ExternalReference function =
3731cb0ef41Sopenharmony_ci          unicode
3741cb0ef41Sopenharmony_ci              ? ExternalReference::re_case_insensitive_compare_unicode()
3751cb0ef41Sopenharmony_ci              : ExternalReference::re_case_insensitive_compare_non_unicode();
3761cb0ef41Sopenharmony_ci      __ CallCFunction(function, argument_count);
3771cb0ef41Sopenharmony_ci    }
3781cb0ef41Sopenharmony_ci
3791cb0ef41Sopenharmony_ci    // Check if function returned non-zero for success or zero for failure.
3801cb0ef41Sopenharmony_ci    BranchOrBacktrack(on_no_match, eq, v0, Operand(zero_reg));
3811cb0ef41Sopenharmony_ci    // On success, increment position by length of capture.
3821cb0ef41Sopenharmony_ci    if (read_backward) {
3831cb0ef41Sopenharmony_ci      __ Dsubu(current_input_offset(), current_input_offset(), Operand(s3));
3841cb0ef41Sopenharmony_ci    } else {
3851cb0ef41Sopenharmony_ci      __ Daddu(current_input_offset(), current_input_offset(), Operand(s3));
3861cb0ef41Sopenharmony_ci    }
3871cb0ef41Sopenharmony_ci  }
3881cb0ef41Sopenharmony_ci
3891cb0ef41Sopenharmony_ci  __ bind(&fallthrough);
3901cb0ef41Sopenharmony_ci}
3911cb0ef41Sopenharmony_ci
3921cb0ef41Sopenharmony_civoid RegExpMacroAssemblerMIPS::CheckNotBackReference(int start_reg,
3931cb0ef41Sopenharmony_ci                                                     bool read_backward,
3941cb0ef41Sopenharmony_ci                                                     Label* on_no_match) {
3951cb0ef41Sopenharmony_ci  Label fallthrough;
3961cb0ef41Sopenharmony_ci
3971cb0ef41Sopenharmony_ci  // Find length of back-referenced capture.
3981cb0ef41Sopenharmony_ci  __ Ld(a0, register_location(start_reg));
3991cb0ef41Sopenharmony_ci  __ Ld(a1, register_location(start_reg + 1));
4001cb0ef41Sopenharmony_ci  __ Dsubu(a1, a1, a0);  // Length to check.
4011cb0ef41Sopenharmony_ci
4021cb0ef41Sopenharmony_ci  // At this point, the capture registers are either both set or both cleared.
4031cb0ef41Sopenharmony_ci  // If the capture length is zero, then the capture is either empty or cleared.
4041cb0ef41Sopenharmony_ci  // Fall through in both cases.
4051cb0ef41Sopenharmony_ci  __ Branch(&fallthrough, eq, a1, Operand(zero_reg));
4061cb0ef41Sopenharmony_ci
4071cb0ef41Sopenharmony_ci  if (read_backward) {
4081cb0ef41Sopenharmony_ci    __ Ld(t1, MemOperand(frame_pointer(), kStringStartMinusOne));
4091cb0ef41Sopenharmony_ci    __ Daddu(t1, t1, a1);
4101cb0ef41Sopenharmony_ci    BranchOrBacktrack(on_no_match, le, current_input_offset(), Operand(t1));
4111cb0ef41Sopenharmony_ci  } else {
4121cb0ef41Sopenharmony_ci    __ Daddu(t1, a1, current_input_offset());
4131cb0ef41Sopenharmony_ci    // Check that there are enough characters left in the input.
4141cb0ef41Sopenharmony_ci    BranchOrBacktrack(on_no_match, gt, t1, Operand(zero_reg));
4151cb0ef41Sopenharmony_ci  }
4161cb0ef41Sopenharmony_ci
4171cb0ef41Sopenharmony_ci  // Compute pointers to match string and capture string.
4181cb0ef41Sopenharmony_ci  __ Daddu(a0, a0, Operand(end_of_input_address()));
4191cb0ef41Sopenharmony_ci  __ Daddu(a2, end_of_input_address(), Operand(current_input_offset()));
4201cb0ef41Sopenharmony_ci  if (read_backward) {
4211cb0ef41Sopenharmony_ci    __ Dsubu(a2, a2, Operand(a1));
4221cb0ef41Sopenharmony_ci  }
4231cb0ef41Sopenharmony_ci  __ Daddu(a1, a1, Operand(a0));
4241cb0ef41Sopenharmony_ci
4251cb0ef41Sopenharmony_ci  Label loop;
4261cb0ef41Sopenharmony_ci  __ bind(&loop);
4271cb0ef41Sopenharmony_ci  if (mode_ == LATIN1) {
4281cb0ef41Sopenharmony_ci    __ Lbu(a3, MemOperand(a0, 0));
4291cb0ef41Sopenharmony_ci    __ daddiu(a0, a0, char_size());
4301cb0ef41Sopenharmony_ci    __ Lbu(a4, MemOperand(a2, 0));
4311cb0ef41Sopenharmony_ci    __ daddiu(a2, a2, char_size());
4321cb0ef41Sopenharmony_ci  } else {
4331cb0ef41Sopenharmony_ci    DCHECK(mode_ == UC16);
4341cb0ef41Sopenharmony_ci    __ Lhu(a3, MemOperand(a0, 0));
4351cb0ef41Sopenharmony_ci    __ daddiu(a0, a0, char_size());
4361cb0ef41Sopenharmony_ci    __ Lhu(a4, MemOperand(a2, 0));
4371cb0ef41Sopenharmony_ci    __ daddiu(a2, a2, char_size());
4381cb0ef41Sopenharmony_ci  }
4391cb0ef41Sopenharmony_ci  BranchOrBacktrack(on_no_match, ne, a3, Operand(a4));
4401cb0ef41Sopenharmony_ci  __ Branch(&loop, lt, a0, Operand(a1));
4411cb0ef41Sopenharmony_ci
4421cb0ef41Sopenharmony_ci  // Move current character position to position after match.
4431cb0ef41Sopenharmony_ci  __ Dsubu(current_input_offset(), a2, end_of_input_address());
4441cb0ef41Sopenharmony_ci  if (read_backward) {
4451cb0ef41Sopenharmony_ci    __ Ld(t1, register_location(start_reg));      // Index of start of capture.
4461cb0ef41Sopenharmony_ci    __ Ld(a2, register_location(start_reg + 1));  // Index of end of capture.
4471cb0ef41Sopenharmony_ci    __ Daddu(current_input_offset(), current_input_offset(), Operand(t1));
4481cb0ef41Sopenharmony_ci    __ Dsubu(current_input_offset(), current_input_offset(), Operand(a2));
4491cb0ef41Sopenharmony_ci  }
4501cb0ef41Sopenharmony_ci  __ bind(&fallthrough);
4511cb0ef41Sopenharmony_ci}
4521cb0ef41Sopenharmony_ci
4531cb0ef41Sopenharmony_ci
4541cb0ef41Sopenharmony_civoid RegExpMacroAssemblerMIPS::CheckNotCharacter(uint32_t c,
4551cb0ef41Sopenharmony_ci                                                 Label* on_not_equal) {
4561cb0ef41Sopenharmony_ci  BranchOrBacktrack(on_not_equal, ne, current_character(), Operand(c));
4571cb0ef41Sopenharmony_ci}
4581cb0ef41Sopenharmony_ci
4591cb0ef41Sopenharmony_ci
4601cb0ef41Sopenharmony_civoid RegExpMacroAssemblerMIPS::CheckCharacterAfterAnd(uint32_t c,
4611cb0ef41Sopenharmony_ci                                                      uint32_t mask,
4621cb0ef41Sopenharmony_ci                                                      Label* on_equal) {
4631cb0ef41Sopenharmony_ci  __ And(a0, current_character(), Operand(mask));
4641cb0ef41Sopenharmony_ci  Operand rhs = (c == 0) ? Operand(zero_reg) : Operand(c);
4651cb0ef41Sopenharmony_ci  BranchOrBacktrack(on_equal, eq, a0, rhs);
4661cb0ef41Sopenharmony_ci}
4671cb0ef41Sopenharmony_ci
4681cb0ef41Sopenharmony_ci
4691cb0ef41Sopenharmony_civoid RegExpMacroAssemblerMIPS::CheckNotCharacterAfterAnd(uint32_t c,
4701cb0ef41Sopenharmony_ci                                                         uint32_t mask,
4711cb0ef41Sopenharmony_ci                                                         Label* on_not_equal) {
4721cb0ef41Sopenharmony_ci  __ And(a0, current_character(), Operand(mask));
4731cb0ef41Sopenharmony_ci  Operand rhs = (c == 0) ? Operand(zero_reg) : Operand(c);
4741cb0ef41Sopenharmony_ci  BranchOrBacktrack(on_not_equal, ne, a0, rhs);
4751cb0ef41Sopenharmony_ci}
4761cb0ef41Sopenharmony_ci
4771cb0ef41Sopenharmony_civoid RegExpMacroAssemblerMIPS::CheckNotCharacterAfterMinusAnd(
4781cb0ef41Sopenharmony_ci    base::uc16 c, base::uc16 minus, base::uc16 mask, Label* on_not_equal) {
4791cb0ef41Sopenharmony_ci  DCHECK_GT(String::kMaxUtf16CodeUnit, minus);
4801cb0ef41Sopenharmony_ci  __ Dsubu(a0, current_character(), Operand(minus));
4811cb0ef41Sopenharmony_ci  __ And(a0, a0, Operand(mask));
4821cb0ef41Sopenharmony_ci  BranchOrBacktrack(on_not_equal, ne, a0, Operand(c));
4831cb0ef41Sopenharmony_ci}
4841cb0ef41Sopenharmony_ci
4851cb0ef41Sopenharmony_civoid RegExpMacroAssemblerMIPS::CheckCharacterInRange(base::uc16 from,
4861cb0ef41Sopenharmony_ci                                                     base::uc16 to,
4871cb0ef41Sopenharmony_ci                                                     Label* on_in_range) {
4881cb0ef41Sopenharmony_ci  __ Dsubu(a0, current_character(), Operand(from));
4891cb0ef41Sopenharmony_ci  // Unsigned lower-or-same condition.
4901cb0ef41Sopenharmony_ci  BranchOrBacktrack(on_in_range, ls, a0, Operand(to - from));
4911cb0ef41Sopenharmony_ci}
4921cb0ef41Sopenharmony_ci
4931cb0ef41Sopenharmony_civoid RegExpMacroAssemblerMIPS::CheckCharacterNotInRange(
4941cb0ef41Sopenharmony_ci    base::uc16 from, base::uc16 to, Label* on_not_in_range) {
4951cb0ef41Sopenharmony_ci  __ Dsubu(a0, current_character(), Operand(from));
4961cb0ef41Sopenharmony_ci  // Unsigned higher condition.
4971cb0ef41Sopenharmony_ci  BranchOrBacktrack(on_not_in_range, hi, a0, Operand(to - from));
4981cb0ef41Sopenharmony_ci}
4991cb0ef41Sopenharmony_ci
5001cb0ef41Sopenharmony_civoid RegExpMacroAssemblerMIPS::CallIsCharacterInRangeArray(
5011cb0ef41Sopenharmony_ci    const ZoneList<CharacterRange>* ranges) {
5021cb0ef41Sopenharmony_ci  static const int kNumArguments = 3;
5031cb0ef41Sopenharmony_ci  __ PrepareCallCFunction(kNumArguments, a0);
5041cb0ef41Sopenharmony_ci
5051cb0ef41Sopenharmony_ci  __ mov(a0, current_character());
5061cb0ef41Sopenharmony_ci  __ li(a1, Operand(GetOrAddRangeArray(ranges)));
5071cb0ef41Sopenharmony_ci  __ li(a2, Operand(ExternalReference::isolate_address(isolate())));
5081cb0ef41Sopenharmony_ci
5091cb0ef41Sopenharmony_ci  {
5101cb0ef41Sopenharmony_ci    // We have a frame (set up in GetCode), but the assembler doesn't know.
5111cb0ef41Sopenharmony_ci    FrameScope scope(masm_.get(), StackFrame::MANUAL);
5121cb0ef41Sopenharmony_ci    __ CallCFunction(ExternalReference::re_is_character_in_range_array(),
5131cb0ef41Sopenharmony_ci                     kNumArguments);
5141cb0ef41Sopenharmony_ci  }
5151cb0ef41Sopenharmony_ci
5161cb0ef41Sopenharmony_ci  __ li(code_pointer(), Operand(masm_->CodeObject()));
5171cb0ef41Sopenharmony_ci}
5181cb0ef41Sopenharmony_ci
5191cb0ef41Sopenharmony_cibool RegExpMacroAssemblerMIPS::CheckCharacterInRangeArray(
5201cb0ef41Sopenharmony_ci    const ZoneList<CharacterRange>* ranges, Label* on_in_range) {
5211cb0ef41Sopenharmony_ci  CallIsCharacterInRangeArray(ranges);
5221cb0ef41Sopenharmony_ci  BranchOrBacktrack(on_in_range, ne, v0, Operand(zero_reg));
5231cb0ef41Sopenharmony_ci  return true;
5241cb0ef41Sopenharmony_ci}
5251cb0ef41Sopenharmony_ci
5261cb0ef41Sopenharmony_cibool RegExpMacroAssemblerMIPS::CheckCharacterNotInRangeArray(
5271cb0ef41Sopenharmony_ci    const ZoneList<CharacterRange>* ranges, Label* on_not_in_range) {
5281cb0ef41Sopenharmony_ci  CallIsCharacterInRangeArray(ranges);
5291cb0ef41Sopenharmony_ci  BranchOrBacktrack(on_not_in_range, eq, v0, Operand(zero_reg));
5301cb0ef41Sopenharmony_ci  return true;
5311cb0ef41Sopenharmony_ci}
5321cb0ef41Sopenharmony_ci
5331cb0ef41Sopenharmony_civoid RegExpMacroAssemblerMIPS::CheckBitInTable(
5341cb0ef41Sopenharmony_ci    Handle<ByteArray> table,
5351cb0ef41Sopenharmony_ci    Label* on_bit_set) {
5361cb0ef41Sopenharmony_ci  __ li(a0, Operand(table));
5371cb0ef41Sopenharmony_ci  if (mode_ != LATIN1 || kTableMask != String::kMaxOneByteCharCode) {
5381cb0ef41Sopenharmony_ci    __ And(a1, current_character(), Operand(kTableSize - 1));
5391cb0ef41Sopenharmony_ci    __ Daddu(a0, a0, a1);
5401cb0ef41Sopenharmony_ci  } else {
5411cb0ef41Sopenharmony_ci    __ Daddu(a0, a0, current_character());
5421cb0ef41Sopenharmony_ci  }
5431cb0ef41Sopenharmony_ci
5441cb0ef41Sopenharmony_ci  __ Lbu(a0, FieldMemOperand(a0, ByteArray::kHeaderSize));
5451cb0ef41Sopenharmony_ci  BranchOrBacktrack(on_bit_set, ne, a0, Operand(zero_reg));
5461cb0ef41Sopenharmony_ci}
5471cb0ef41Sopenharmony_ci
5481cb0ef41Sopenharmony_cibool RegExpMacroAssemblerMIPS::CheckSpecialCharacterClass(
5491cb0ef41Sopenharmony_ci    StandardCharacterSet type, Label* on_no_match) {
5501cb0ef41Sopenharmony_ci  // Range checks (c in min..max) are generally implemented by an unsigned
5511cb0ef41Sopenharmony_ci  // (c - min) <= (max - min) check.
5521cb0ef41Sopenharmony_ci  // TODO(jgruber): No custom implementation (yet): s(UC16), S(UC16).
5531cb0ef41Sopenharmony_ci  switch (type) {
5541cb0ef41Sopenharmony_ci    case StandardCharacterSet::kWhitespace:
5551cb0ef41Sopenharmony_ci      // Match space-characters.
5561cb0ef41Sopenharmony_ci      if (mode_ == LATIN1) {
5571cb0ef41Sopenharmony_ci        // One byte space characters are '\t'..'\r', ' ' and \u00a0.
5581cb0ef41Sopenharmony_ci        Label success;
5591cb0ef41Sopenharmony_ci        __ Branch(&success, eq, current_character(), Operand(' '));
5601cb0ef41Sopenharmony_ci        // Check range 0x09..0x0D.
5611cb0ef41Sopenharmony_ci        __ Dsubu(a0, current_character(), Operand('\t'));
5621cb0ef41Sopenharmony_ci        __ Branch(&success, ls, a0, Operand('\r' - '\t'));
5631cb0ef41Sopenharmony_ci        // \u00a0 (NBSP).
5641cb0ef41Sopenharmony_ci        BranchOrBacktrack(on_no_match, ne, a0, Operand(0x00A0 - '\t'));
5651cb0ef41Sopenharmony_ci        __ bind(&success);
5661cb0ef41Sopenharmony_ci        return true;
5671cb0ef41Sopenharmony_ci      }
5681cb0ef41Sopenharmony_ci      return false;
5691cb0ef41Sopenharmony_ci    case StandardCharacterSet::kNotWhitespace:
5701cb0ef41Sopenharmony_ci      // The emitted code for generic character classes is good enough.
5711cb0ef41Sopenharmony_ci      return false;
5721cb0ef41Sopenharmony_ci    case StandardCharacterSet::kDigit:
5731cb0ef41Sopenharmony_ci      // Match Latin1 digits ('0'..'9').
5741cb0ef41Sopenharmony_ci      __ Dsubu(a0, current_character(), Operand('0'));
5751cb0ef41Sopenharmony_ci      BranchOrBacktrack(on_no_match, hi, a0, Operand('9' - '0'));
5761cb0ef41Sopenharmony_ci      return true;
5771cb0ef41Sopenharmony_ci    case StandardCharacterSet::kNotDigit:
5781cb0ef41Sopenharmony_ci      // Match non Latin1-digits.
5791cb0ef41Sopenharmony_ci      __ Dsubu(a0, current_character(), Operand('0'));
5801cb0ef41Sopenharmony_ci      BranchOrBacktrack(on_no_match, ls, a0, Operand('9' - '0'));
5811cb0ef41Sopenharmony_ci      return true;
5821cb0ef41Sopenharmony_ci    case StandardCharacterSet::kNotLineTerminator: {
5831cb0ef41Sopenharmony_ci      // Match non-newlines (not 0x0A('\n'), 0x0D('\r'), 0x2028 and 0x2029).
5841cb0ef41Sopenharmony_ci      __ Xor(a0, current_character(), Operand(0x01));
5851cb0ef41Sopenharmony_ci      // See if current character is '\n'^1 or '\r'^1, i.e., 0x0B or 0x0C.
5861cb0ef41Sopenharmony_ci      __ Dsubu(a0, a0, Operand(0x0B));
5871cb0ef41Sopenharmony_ci      BranchOrBacktrack(on_no_match, ls, a0, Operand(0x0C - 0x0B));
5881cb0ef41Sopenharmony_ci      if (mode_ == UC16) {
5891cb0ef41Sopenharmony_ci        // Compare original value to 0x2028 and 0x2029, using the already
5901cb0ef41Sopenharmony_ci        // computed (current_char ^ 0x01 - 0x0B). I.e., check for
5911cb0ef41Sopenharmony_ci        // 0x201D (0x2028 - 0x0B) or 0x201E.
5921cb0ef41Sopenharmony_ci        __ Dsubu(a0, a0, Operand(0x2028 - 0x0B));
5931cb0ef41Sopenharmony_ci        BranchOrBacktrack(on_no_match, ls, a0, Operand(1));
5941cb0ef41Sopenharmony_ci      }
5951cb0ef41Sopenharmony_ci      return true;
5961cb0ef41Sopenharmony_ci    }
5971cb0ef41Sopenharmony_ci    case StandardCharacterSet::kLineTerminator: {
5981cb0ef41Sopenharmony_ci      // Match newlines (0x0A('\n'), 0x0D('\r'), 0x2028 and 0x2029).
5991cb0ef41Sopenharmony_ci      __ Xor(a0, current_character(), Operand(0x01));
6001cb0ef41Sopenharmony_ci      // See if current character is '\n'^1 or '\r'^1, i.e., 0x0B or 0x0C.
6011cb0ef41Sopenharmony_ci      __ Dsubu(a0, a0, Operand(0x0B));
6021cb0ef41Sopenharmony_ci      if (mode_ == LATIN1) {
6031cb0ef41Sopenharmony_ci        BranchOrBacktrack(on_no_match, hi, a0, Operand(0x0C - 0x0B));
6041cb0ef41Sopenharmony_ci      } else {
6051cb0ef41Sopenharmony_ci        Label done;
6061cb0ef41Sopenharmony_ci        BranchOrBacktrack(&done, ls, a0, Operand(0x0C - 0x0B));
6071cb0ef41Sopenharmony_ci        // Compare original value to 0x2028 and 0x2029, using the already
6081cb0ef41Sopenharmony_ci        // computed (current_char ^ 0x01 - 0x0B). I.e., check for
6091cb0ef41Sopenharmony_ci        // 0x201D (0x2028 - 0x0B) or 0x201E.
6101cb0ef41Sopenharmony_ci        __ Dsubu(a0, a0, Operand(0x2028 - 0x0B));
6111cb0ef41Sopenharmony_ci        BranchOrBacktrack(on_no_match, hi, a0, Operand(1));
6121cb0ef41Sopenharmony_ci        __ bind(&done);
6131cb0ef41Sopenharmony_ci      }
6141cb0ef41Sopenharmony_ci      return true;
6151cb0ef41Sopenharmony_ci    }
6161cb0ef41Sopenharmony_ci    case StandardCharacterSet::kWord: {
6171cb0ef41Sopenharmony_ci      if (mode_ != LATIN1) {
6181cb0ef41Sopenharmony_ci        // Table is 256 entries, so all Latin1 characters can be tested.
6191cb0ef41Sopenharmony_ci        BranchOrBacktrack(on_no_match, hi, current_character(), Operand('z'));
6201cb0ef41Sopenharmony_ci      }
6211cb0ef41Sopenharmony_ci      ExternalReference map = ExternalReference::re_word_character_map();
6221cb0ef41Sopenharmony_ci      __ li(a0, Operand(map));
6231cb0ef41Sopenharmony_ci      __ Daddu(a0, a0, current_character());
6241cb0ef41Sopenharmony_ci      __ Lbu(a0, MemOperand(a0, 0));
6251cb0ef41Sopenharmony_ci      BranchOrBacktrack(on_no_match, eq, a0, Operand(zero_reg));
6261cb0ef41Sopenharmony_ci      return true;
6271cb0ef41Sopenharmony_ci    }
6281cb0ef41Sopenharmony_ci    case StandardCharacterSet::kNotWord: {
6291cb0ef41Sopenharmony_ci      Label done;
6301cb0ef41Sopenharmony_ci      if (mode_ != LATIN1) {
6311cb0ef41Sopenharmony_ci        // Table is 256 entries, so all Latin1 characters can be tested.
6321cb0ef41Sopenharmony_ci        __ Branch(&done, hi, current_character(), Operand('z'));
6331cb0ef41Sopenharmony_ci      }
6341cb0ef41Sopenharmony_ci      ExternalReference map = ExternalReference::re_word_character_map();
6351cb0ef41Sopenharmony_ci      __ li(a0, Operand(map));
6361cb0ef41Sopenharmony_ci      __ Daddu(a0, a0, current_character());
6371cb0ef41Sopenharmony_ci      __ Lbu(a0, MemOperand(a0, 0));
6381cb0ef41Sopenharmony_ci      BranchOrBacktrack(on_no_match, ne, a0, Operand(zero_reg));
6391cb0ef41Sopenharmony_ci      if (mode_ != LATIN1) {
6401cb0ef41Sopenharmony_ci        __ bind(&done);
6411cb0ef41Sopenharmony_ci      }
6421cb0ef41Sopenharmony_ci      return true;
6431cb0ef41Sopenharmony_ci    }
6441cb0ef41Sopenharmony_ci    case StandardCharacterSet::kEverything:
6451cb0ef41Sopenharmony_ci      // Match any character.
6461cb0ef41Sopenharmony_ci      return true;
6471cb0ef41Sopenharmony_ci  }
6481cb0ef41Sopenharmony_ci}
6491cb0ef41Sopenharmony_ci
6501cb0ef41Sopenharmony_civoid RegExpMacroAssemblerMIPS::Fail() {
6511cb0ef41Sopenharmony_ci  __ li(v0, Operand(FAILURE));
6521cb0ef41Sopenharmony_ci  __ jmp(&exit_label_);
6531cb0ef41Sopenharmony_ci}
6541cb0ef41Sopenharmony_ci
6551cb0ef41Sopenharmony_civoid RegExpMacroAssemblerMIPS::LoadRegExpStackPointerFromMemory(Register dst) {
6561cb0ef41Sopenharmony_ci  ExternalReference ref =
6571cb0ef41Sopenharmony_ci      ExternalReference::address_of_regexp_stack_stack_pointer(isolate());
6581cb0ef41Sopenharmony_ci  __ li(dst, Operand(ref));
6591cb0ef41Sopenharmony_ci  __ Ld(dst, MemOperand(dst));
6601cb0ef41Sopenharmony_ci}
6611cb0ef41Sopenharmony_ci
6621cb0ef41Sopenharmony_civoid RegExpMacroAssemblerMIPS::StoreRegExpStackPointerToMemory(
6631cb0ef41Sopenharmony_ci    Register src, Register scratch) {
6641cb0ef41Sopenharmony_ci  ExternalReference ref =
6651cb0ef41Sopenharmony_ci      ExternalReference::address_of_regexp_stack_stack_pointer(isolate());
6661cb0ef41Sopenharmony_ci  __ li(scratch, Operand(ref));
6671cb0ef41Sopenharmony_ci  __ Sd(src, MemOperand(scratch));
6681cb0ef41Sopenharmony_ci}
6691cb0ef41Sopenharmony_ci
6701cb0ef41Sopenharmony_civoid RegExpMacroAssemblerMIPS::PushRegExpBasePointer(Register stack_pointer,
6711cb0ef41Sopenharmony_ci                                                     Register scratch) {
6721cb0ef41Sopenharmony_ci  ExternalReference ref =
6731cb0ef41Sopenharmony_ci      ExternalReference::address_of_regexp_stack_memory_top_address(isolate());
6741cb0ef41Sopenharmony_ci  __ li(scratch, Operand(ref));
6751cb0ef41Sopenharmony_ci  __ Ld(scratch, MemOperand(scratch));
6761cb0ef41Sopenharmony_ci  __ Dsubu(scratch, stack_pointer, scratch);
6771cb0ef41Sopenharmony_ci  __ Sd(scratch, MemOperand(frame_pointer(), kRegExpStackBasePointer));
6781cb0ef41Sopenharmony_ci}
6791cb0ef41Sopenharmony_ci
6801cb0ef41Sopenharmony_civoid RegExpMacroAssemblerMIPS::PopRegExpBasePointer(Register stack_pointer_out,
6811cb0ef41Sopenharmony_ci                                                    Register scratch) {
6821cb0ef41Sopenharmony_ci  ExternalReference ref =
6831cb0ef41Sopenharmony_ci      ExternalReference::address_of_regexp_stack_memory_top_address(isolate());
6841cb0ef41Sopenharmony_ci  __ Ld(stack_pointer_out,
6851cb0ef41Sopenharmony_ci        MemOperand(frame_pointer(), kRegExpStackBasePointer));
6861cb0ef41Sopenharmony_ci  __ li(scratch, Operand(ref));
6871cb0ef41Sopenharmony_ci  __ Ld(scratch, MemOperand(scratch));
6881cb0ef41Sopenharmony_ci  __ Daddu(stack_pointer_out, stack_pointer_out, scratch);
6891cb0ef41Sopenharmony_ci  StoreRegExpStackPointerToMemory(stack_pointer_out, scratch);
6901cb0ef41Sopenharmony_ci}
6911cb0ef41Sopenharmony_ci
6921cb0ef41Sopenharmony_ciHandle<HeapObject> RegExpMacroAssemblerMIPS::GetCode(Handle<String> source) {
6931cb0ef41Sopenharmony_ci  Label return_v0;
6941cb0ef41Sopenharmony_ci  if (masm_->has_exception()) {
6951cb0ef41Sopenharmony_ci    // If the code gets corrupted due to long regular expressions and lack of
6961cb0ef41Sopenharmony_ci    // space on trampolines, an internal exception flag is set. If this case
6971cb0ef41Sopenharmony_ci    // is detected, we will jump into exit sequence right away.
6981cb0ef41Sopenharmony_ci    __ bind_to(&entry_label_, internal_failure_label_.pos());
6991cb0ef41Sopenharmony_ci  } else {
7001cb0ef41Sopenharmony_ci    // Finalize code - write the entry point code now we know how many
7011cb0ef41Sopenharmony_ci    // registers we need.
7021cb0ef41Sopenharmony_ci
7031cb0ef41Sopenharmony_ci    // Entry code:
7041cb0ef41Sopenharmony_ci    __ bind(&entry_label_);
7051cb0ef41Sopenharmony_ci
7061cb0ef41Sopenharmony_ci    // Tell the system that we have a stack frame.  Because the type is MANUAL,
7071cb0ef41Sopenharmony_ci    // no is generated.
7081cb0ef41Sopenharmony_ci    FrameScope scope(masm_.get(), StackFrame::MANUAL);
7091cb0ef41Sopenharmony_ci
7101cb0ef41Sopenharmony_ci    // Actually emit code to start a new stack frame.
7111cb0ef41Sopenharmony_ci    // Push arguments
7121cb0ef41Sopenharmony_ci    // Save callee-save registers.
7131cb0ef41Sopenharmony_ci    // Start new stack frame.
7141cb0ef41Sopenharmony_ci    // Store link register in existing stack-cell.
7151cb0ef41Sopenharmony_ci    // Order here should correspond to order of offset constants in header file.
7161cb0ef41Sopenharmony_ci    // TODO(plind): we save s0..s7, but ONLY use s3 here - use the regs
7171cb0ef41Sopenharmony_ci    // or dont save.
7181cb0ef41Sopenharmony_ci    RegList registers_to_retain = {s0, s1, s2, s3, s4, s5, s6, s7, fp};
7191cb0ef41Sopenharmony_ci    RegList argument_registers = {a0, a1, a2, a3};
7201cb0ef41Sopenharmony_ci
7211cb0ef41Sopenharmony_ci    argument_registers |= {a4, a5, a6, a7};
7221cb0ef41Sopenharmony_ci
7231cb0ef41Sopenharmony_ci    __ MultiPush(argument_registers | registers_to_retain | ra);
7241cb0ef41Sopenharmony_ci    // Set frame pointer in space for it if this is not a direct call
7251cb0ef41Sopenharmony_ci    // from generated code.
7261cb0ef41Sopenharmony_ci    // TODO(plind): this 8 is the # of argument regs, should have definition.
7271cb0ef41Sopenharmony_ci    __ Daddu(frame_pointer(), sp, Operand(8 * kPointerSize));
7281cb0ef41Sopenharmony_ci    STATIC_ASSERT(kSuccessfulCaptures == kInputString - kSystemPointerSize);
7291cb0ef41Sopenharmony_ci    __ mov(a0, zero_reg);
7301cb0ef41Sopenharmony_ci    __ push(a0);  // Make room for success counter and initialize it to 0.
7311cb0ef41Sopenharmony_ci    STATIC_ASSERT(kStringStartMinusOne ==
7321cb0ef41Sopenharmony_ci                  kSuccessfulCaptures - kSystemPointerSize);
7331cb0ef41Sopenharmony_ci    __ push(a0);  // Make room for "string start - 1" constant.
7341cb0ef41Sopenharmony_ci    STATIC_ASSERT(kBacktrackCount == kStringStartMinusOne - kSystemPointerSize);
7351cb0ef41Sopenharmony_ci    __ push(a0);  // The backtrack counter
7361cb0ef41Sopenharmony_ci    STATIC_ASSERT(kRegExpStackBasePointer ==
7371cb0ef41Sopenharmony_ci                  kBacktrackCount - kSystemPointerSize);
7381cb0ef41Sopenharmony_ci    __ push(a0);  // The regexp stack base ptr.
7391cb0ef41Sopenharmony_ci
7401cb0ef41Sopenharmony_ci    // Initialize backtrack stack pointer. It must not be clobbered from here
7411cb0ef41Sopenharmony_ci    // on. Note the backtrack_stackpointer is callee-saved.
7421cb0ef41Sopenharmony_ci    STATIC_ASSERT(backtrack_stackpointer() == s7);
7431cb0ef41Sopenharmony_ci    LoadRegExpStackPointerFromMemory(backtrack_stackpointer());
7441cb0ef41Sopenharmony_ci
7451cb0ef41Sopenharmony_ci    // Store the regexp base pointer - we'll later restore it / write it to
7461cb0ef41Sopenharmony_ci    // memory when returning from this irregexp code object.
7471cb0ef41Sopenharmony_ci    PushRegExpBasePointer(backtrack_stackpointer(), a1);
7481cb0ef41Sopenharmony_ci
7491cb0ef41Sopenharmony_ci    {
7501cb0ef41Sopenharmony_ci      // Check if we have space on the stack for registers.
7511cb0ef41Sopenharmony_ci      Label stack_limit_hit, stack_ok;
7521cb0ef41Sopenharmony_ci
7531cb0ef41Sopenharmony_ci      ExternalReference stack_limit =
7541cb0ef41Sopenharmony_ci          ExternalReference::address_of_jslimit(masm_->isolate());
7551cb0ef41Sopenharmony_ci      __ li(a0, Operand(stack_limit));
7561cb0ef41Sopenharmony_ci      __ Ld(a0, MemOperand(a0));
7571cb0ef41Sopenharmony_ci      __ Dsubu(a0, sp, a0);
7581cb0ef41Sopenharmony_ci      // Handle it if the stack pointer is already below the stack limit.
7591cb0ef41Sopenharmony_ci      __ Branch(&stack_limit_hit, le, a0, Operand(zero_reg));
7601cb0ef41Sopenharmony_ci      // Check if there is room for the variable number of registers above
7611cb0ef41Sopenharmony_ci      // the stack limit.
7621cb0ef41Sopenharmony_ci      __ Branch(&stack_ok, hs, a0, Operand(num_registers_ * kPointerSize));
7631cb0ef41Sopenharmony_ci      // Exit with OutOfMemory exception. There is not enough space on the stack
7641cb0ef41Sopenharmony_ci      // for our working registers.
7651cb0ef41Sopenharmony_ci      __ li(v0, Operand(EXCEPTION));
7661cb0ef41Sopenharmony_ci      __ jmp(&return_v0);
7671cb0ef41Sopenharmony_ci
7681cb0ef41Sopenharmony_ci      __ bind(&stack_limit_hit);
7691cb0ef41Sopenharmony_ci      CallCheckStackGuardState(a0);
7701cb0ef41Sopenharmony_ci      // If returned value is non-zero, we exit with the returned value as
7711cb0ef41Sopenharmony_ci      // result.
7721cb0ef41Sopenharmony_ci      __ Branch(&return_v0, ne, v0, Operand(zero_reg));
7731cb0ef41Sopenharmony_ci
7741cb0ef41Sopenharmony_ci      __ bind(&stack_ok);
7751cb0ef41Sopenharmony_ci    }
7761cb0ef41Sopenharmony_ci
7771cb0ef41Sopenharmony_ci    // Allocate space on stack for registers.
7781cb0ef41Sopenharmony_ci    __ Dsubu(sp, sp, Operand(num_registers_ * kPointerSize));
7791cb0ef41Sopenharmony_ci    // Load string end.
7801cb0ef41Sopenharmony_ci    __ Ld(end_of_input_address(), MemOperand(frame_pointer(), kInputEnd));
7811cb0ef41Sopenharmony_ci    // Load input start.
7821cb0ef41Sopenharmony_ci    __ Ld(a0, MemOperand(frame_pointer(), kInputStart));
7831cb0ef41Sopenharmony_ci    // Find negative length (offset of start relative to end).
7841cb0ef41Sopenharmony_ci    __ Dsubu(current_input_offset(), a0, end_of_input_address());
7851cb0ef41Sopenharmony_ci    // Set a0 to address of char before start of the input string
7861cb0ef41Sopenharmony_ci    // (effectively string position -1).
7871cb0ef41Sopenharmony_ci    __ Ld(a1, MemOperand(frame_pointer(), kStartIndex));
7881cb0ef41Sopenharmony_ci    __ Dsubu(a0, current_input_offset(), Operand(char_size()));
7891cb0ef41Sopenharmony_ci    __ dsll(t1, a1, (mode_ == UC16) ? 1 : 0);
7901cb0ef41Sopenharmony_ci    __ Dsubu(a0, a0, t1);
7911cb0ef41Sopenharmony_ci    // Store this value in a local variable, for use when clearing
7921cb0ef41Sopenharmony_ci    // position registers.
7931cb0ef41Sopenharmony_ci    __ Sd(a0, MemOperand(frame_pointer(), kStringStartMinusOne));
7941cb0ef41Sopenharmony_ci
7951cb0ef41Sopenharmony_ci    // Initialize code pointer register
7961cb0ef41Sopenharmony_ci    __ li(code_pointer(), Operand(masm_->CodeObject()), CONSTANT_SIZE);
7971cb0ef41Sopenharmony_ci
7981cb0ef41Sopenharmony_ci    Label load_char_start_regexp;
7991cb0ef41Sopenharmony_ci    {
8001cb0ef41Sopenharmony_ci      Label start_regexp;
8011cb0ef41Sopenharmony_ci      // Load newline if index is at start, previous character otherwise.
8021cb0ef41Sopenharmony_ci      __ Branch(&load_char_start_regexp, ne, a1, Operand(zero_reg));
8031cb0ef41Sopenharmony_ci      __ li(current_character(), Operand('\n'));
8041cb0ef41Sopenharmony_ci      __ jmp(&start_regexp);
8051cb0ef41Sopenharmony_ci
8061cb0ef41Sopenharmony_ci      // Global regexp restarts matching here.
8071cb0ef41Sopenharmony_ci      __ bind(&load_char_start_regexp);
8081cb0ef41Sopenharmony_ci      // Load previous char as initial value of current character register.
8091cb0ef41Sopenharmony_ci      LoadCurrentCharacterUnchecked(-1, 1);
8101cb0ef41Sopenharmony_ci      __ bind(&start_regexp);
8111cb0ef41Sopenharmony_ci    }
8121cb0ef41Sopenharmony_ci
8131cb0ef41Sopenharmony_ci    // Initialize on-stack registers.
8141cb0ef41Sopenharmony_ci    if (num_saved_registers_ > 0) {  // Always is, if generated from a regexp.
8151cb0ef41Sopenharmony_ci      // Fill saved registers with initial value = start offset - 1.
8161cb0ef41Sopenharmony_ci      if (num_saved_registers_ > 8) {
8171cb0ef41Sopenharmony_ci        // Address of register 0.
8181cb0ef41Sopenharmony_ci        __ Daddu(a1, frame_pointer(), Operand(kRegisterZero));
8191cb0ef41Sopenharmony_ci        __ li(a2, Operand(num_saved_registers_));
8201cb0ef41Sopenharmony_ci        Label init_loop;
8211cb0ef41Sopenharmony_ci        __ bind(&init_loop);
8221cb0ef41Sopenharmony_ci        __ Sd(a0, MemOperand(a1));
8231cb0ef41Sopenharmony_ci        __ Daddu(a1, a1, Operand(-kPointerSize));
8241cb0ef41Sopenharmony_ci        __ Dsubu(a2, a2, Operand(1));
8251cb0ef41Sopenharmony_ci        __ Branch(&init_loop, ne, a2, Operand(zero_reg));
8261cb0ef41Sopenharmony_ci      } else {
8271cb0ef41Sopenharmony_ci        for (int i = 0; i < num_saved_registers_; i++) {
8281cb0ef41Sopenharmony_ci          __ Sd(a0, register_location(i));
8291cb0ef41Sopenharmony_ci        }
8301cb0ef41Sopenharmony_ci      }
8311cb0ef41Sopenharmony_ci    }
8321cb0ef41Sopenharmony_ci
8331cb0ef41Sopenharmony_ci    __ jmp(&start_label_);
8341cb0ef41Sopenharmony_ci
8351cb0ef41Sopenharmony_ci    // Exit code:
8361cb0ef41Sopenharmony_ci    if (success_label_.is_linked()) {
8371cb0ef41Sopenharmony_ci      // Save captures when successful.
8381cb0ef41Sopenharmony_ci      __ bind(&success_label_);
8391cb0ef41Sopenharmony_ci      if (num_saved_registers_ > 0) {
8401cb0ef41Sopenharmony_ci        // Copy captures to output.
8411cb0ef41Sopenharmony_ci        __ Ld(a1, MemOperand(frame_pointer(), kInputStart));
8421cb0ef41Sopenharmony_ci        __ Ld(a0, MemOperand(frame_pointer(), kRegisterOutput));
8431cb0ef41Sopenharmony_ci        __ Ld(a2, MemOperand(frame_pointer(), kStartIndex));
8441cb0ef41Sopenharmony_ci        __ Dsubu(a1, end_of_input_address(), a1);
8451cb0ef41Sopenharmony_ci        // a1 is length of input in bytes.
8461cb0ef41Sopenharmony_ci        if (mode_ == UC16) {
8471cb0ef41Sopenharmony_ci          __ dsrl(a1, a1, 1);
8481cb0ef41Sopenharmony_ci        }
8491cb0ef41Sopenharmony_ci        // a1 is length of input in characters.
8501cb0ef41Sopenharmony_ci        __ Daddu(a1, a1, Operand(a2));
8511cb0ef41Sopenharmony_ci        // a1 is length of string in characters.
8521cb0ef41Sopenharmony_ci
8531cb0ef41Sopenharmony_ci        DCHECK_EQ(0, num_saved_registers_ % 2);
8541cb0ef41Sopenharmony_ci        // Always an even number of capture registers. This allows us to
8551cb0ef41Sopenharmony_ci        // unroll the loop once to add an operation between a load of a register
8561cb0ef41Sopenharmony_ci        // and the following use of that register.
8571cb0ef41Sopenharmony_ci        for (int i = 0; i < num_saved_registers_; i += 2) {
8581cb0ef41Sopenharmony_ci          __ Ld(a2, register_location(i));
8591cb0ef41Sopenharmony_ci          __ Ld(a3, register_location(i + 1));
8601cb0ef41Sopenharmony_ci          if (i == 0 && global_with_zero_length_check()) {
8611cb0ef41Sopenharmony_ci            // Keep capture start in a4 for the zero-length check later.
8621cb0ef41Sopenharmony_ci            __ mov(t3, a2);
8631cb0ef41Sopenharmony_ci          }
8641cb0ef41Sopenharmony_ci          if (mode_ == UC16) {
8651cb0ef41Sopenharmony_ci            __ dsra(a2, a2, 1);
8661cb0ef41Sopenharmony_ci            __ Daddu(a2, a2, a1);
8671cb0ef41Sopenharmony_ci            __ dsra(a3, a3, 1);
8681cb0ef41Sopenharmony_ci            __ Daddu(a3, a3, a1);
8691cb0ef41Sopenharmony_ci          } else {
8701cb0ef41Sopenharmony_ci            __ Daddu(a2, a1, Operand(a2));
8711cb0ef41Sopenharmony_ci            __ Daddu(a3, a1, Operand(a3));
8721cb0ef41Sopenharmony_ci          }
8731cb0ef41Sopenharmony_ci          // V8 expects the output to be an int32_t array.
8741cb0ef41Sopenharmony_ci          __ Sw(a2, MemOperand(a0));
8751cb0ef41Sopenharmony_ci          __ Daddu(a0, a0, kIntSize);
8761cb0ef41Sopenharmony_ci          __ Sw(a3, MemOperand(a0));
8771cb0ef41Sopenharmony_ci          __ Daddu(a0, a0, kIntSize);
8781cb0ef41Sopenharmony_ci        }
8791cb0ef41Sopenharmony_ci      }
8801cb0ef41Sopenharmony_ci
8811cb0ef41Sopenharmony_ci      if (global()) {
8821cb0ef41Sopenharmony_ci        // Restart matching if the regular expression is flagged as global.
8831cb0ef41Sopenharmony_ci        __ Ld(a0, MemOperand(frame_pointer(), kSuccessfulCaptures));
8841cb0ef41Sopenharmony_ci        __ Ld(a1, MemOperand(frame_pointer(), kNumOutputRegisters));
8851cb0ef41Sopenharmony_ci        __ Ld(a2, MemOperand(frame_pointer(), kRegisterOutput));
8861cb0ef41Sopenharmony_ci        // Increment success counter.
8871cb0ef41Sopenharmony_ci        __ Daddu(a0, a0, 1);
8881cb0ef41Sopenharmony_ci        __ Sd(a0, MemOperand(frame_pointer(), kSuccessfulCaptures));
8891cb0ef41Sopenharmony_ci        // Capture results have been stored, so the number of remaining global
8901cb0ef41Sopenharmony_ci        // output registers is reduced by the number of stored captures.
8911cb0ef41Sopenharmony_ci        __ Dsubu(a1, a1, num_saved_registers_);
8921cb0ef41Sopenharmony_ci        // Check whether we have enough room for another set of capture results.
8931cb0ef41Sopenharmony_ci        __ mov(v0, a0);
8941cb0ef41Sopenharmony_ci        __ Branch(&return_v0, lt, a1, Operand(num_saved_registers_));
8951cb0ef41Sopenharmony_ci
8961cb0ef41Sopenharmony_ci        __ Sd(a1, MemOperand(frame_pointer(), kNumOutputRegisters));
8971cb0ef41Sopenharmony_ci        // Advance the location for output.
8981cb0ef41Sopenharmony_ci        __ Daddu(a2, a2, num_saved_registers_ * kIntSize);
8991cb0ef41Sopenharmony_ci        __ Sd(a2, MemOperand(frame_pointer(), kRegisterOutput));
9001cb0ef41Sopenharmony_ci
9011cb0ef41Sopenharmony_ci        // Prepare a0 to initialize registers with its value in the next run.
9021cb0ef41Sopenharmony_ci        __ Ld(a0, MemOperand(frame_pointer(), kStringStartMinusOne));
9031cb0ef41Sopenharmony_ci
9041cb0ef41Sopenharmony_ci        // Restore the original regexp stack pointer value (effectively, pop the
9051cb0ef41Sopenharmony_ci        // stored base pointer).
9061cb0ef41Sopenharmony_ci        PopRegExpBasePointer(backtrack_stackpointer(), a2);
9071cb0ef41Sopenharmony_ci
9081cb0ef41Sopenharmony_ci        if (global_with_zero_length_check()) {
9091cb0ef41Sopenharmony_ci          // Special case for zero-length matches.
9101cb0ef41Sopenharmony_ci          // t3: capture start index
9111cb0ef41Sopenharmony_ci          // Not a zero-length match, restart.
9121cb0ef41Sopenharmony_ci          __ Branch(
9131cb0ef41Sopenharmony_ci              &load_char_start_regexp, ne, current_input_offset(), Operand(t3));
9141cb0ef41Sopenharmony_ci          // Offset from the end is zero if we already reached the end.
9151cb0ef41Sopenharmony_ci          __ Branch(&exit_label_, eq, current_input_offset(),
9161cb0ef41Sopenharmony_ci                    Operand(zero_reg));
9171cb0ef41Sopenharmony_ci          // Advance current position after a zero-length match.
9181cb0ef41Sopenharmony_ci          Label advance;
9191cb0ef41Sopenharmony_ci          __ bind(&advance);
9201cb0ef41Sopenharmony_ci          __ Daddu(current_input_offset(), current_input_offset(),
9211cb0ef41Sopenharmony_ci                   Operand((mode_ == UC16) ? 2 : 1));
9221cb0ef41Sopenharmony_ci          if (global_unicode()) CheckNotInSurrogatePair(0, &advance);
9231cb0ef41Sopenharmony_ci        }
9241cb0ef41Sopenharmony_ci
9251cb0ef41Sopenharmony_ci        __ Branch(&load_char_start_regexp);
9261cb0ef41Sopenharmony_ci      } else {
9271cb0ef41Sopenharmony_ci        __ li(v0, Operand(SUCCESS));
9281cb0ef41Sopenharmony_ci      }
9291cb0ef41Sopenharmony_ci    }
9301cb0ef41Sopenharmony_ci    // Exit and return v0.
9311cb0ef41Sopenharmony_ci    __ bind(&exit_label_);
9321cb0ef41Sopenharmony_ci    if (global()) {
9331cb0ef41Sopenharmony_ci      __ Ld(v0, MemOperand(frame_pointer(), kSuccessfulCaptures));
9341cb0ef41Sopenharmony_ci    }
9351cb0ef41Sopenharmony_ci
9361cb0ef41Sopenharmony_ci    __ bind(&return_v0);
9371cb0ef41Sopenharmony_ci    // Restore the original regexp stack pointer value (effectively, pop the
9381cb0ef41Sopenharmony_ci    // stored base pointer).
9391cb0ef41Sopenharmony_ci    PopRegExpBasePointer(backtrack_stackpointer(), a1);
9401cb0ef41Sopenharmony_ci
9411cb0ef41Sopenharmony_ci    // Skip sp past regexp registers and local variables..
9421cb0ef41Sopenharmony_ci    __ mov(sp, frame_pointer());
9431cb0ef41Sopenharmony_ci    // Restore registers s0..s7 and return (restoring ra to pc).
9441cb0ef41Sopenharmony_ci    __ MultiPop(registers_to_retain | ra);
9451cb0ef41Sopenharmony_ci    __ Ret();
9461cb0ef41Sopenharmony_ci
9471cb0ef41Sopenharmony_ci    // Backtrack code (branch target for conditional backtracks).
9481cb0ef41Sopenharmony_ci    if (backtrack_label_.is_linked()) {
9491cb0ef41Sopenharmony_ci      __ bind(&backtrack_label_);
9501cb0ef41Sopenharmony_ci      Backtrack();
9511cb0ef41Sopenharmony_ci    }
9521cb0ef41Sopenharmony_ci
9531cb0ef41Sopenharmony_ci    Label exit_with_exception;
9541cb0ef41Sopenharmony_ci
9551cb0ef41Sopenharmony_ci    // Preempt-code.
9561cb0ef41Sopenharmony_ci    if (check_preempt_label_.is_linked()) {
9571cb0ef41Sopenharmony_ci      SafeCallTarget(&check_preempt_label_);
9581cb0ef41Sopenharmony_ci      StoreRegExpStackPointerToMemory(backtrack_stackpointer(), a0);
9591cb0ef41Sopenharmony_ci
9601cb0ef41Sopenharmony_ci      CallCheckStackGuardState(a0);
9611cb0ef41Sopenharmony_ci      // If returning non-zero, we should end execution with the given
9621cb0ef41Sopenharmony_ci      // result as return value.
9631cb0ef41Sopenharmony_ci      __ Branch(&return_v0, ne, v0, Operand(zero_reg));
9641cb0ef41Sopenharmony_ci
9651cb0ef41Sopenharmony_ci      LoadRegExpStackPointerFromMemory(backtrack_stackpointer());
9661cb0ef41Sopenharmony_ci
9671cb0ef41Sopenharmony_ci      // String might have moved: Reload end of string from frame.
9681cb0ef41Sopenharmony_ci      __ Ld(end_of_input_address(), MemOperand(frame_pointer(), kInputEnd));
9691cb0ef41Sopenharmony_ci      SafeReturn();
9701cb0ef41Sopenharmony_ci    }
9711cb0ef41Sopenharmony_ci
9721cb0ef41Sopenharmony_ci    // Backtrack stack overflow code.
9731cb0ef41Sopenharmony_ci    if (stack_overflow_label_.is_linked()) {
9741cb0ef41Sopenharmony_ci      SafeCallTarget(&stack_overflow_label_);
9751cb0ef41Sopenharmony_ci      StoreRegExpStackPointerToMemory(backtrack_stackpointer(), a0);
9761cb0ef41Sopenharmony_ci      // Reached if the backtrack-stack limit has been hit.
9771cb0ef41Sopenharmony_ci
9781cb0ef41Sopenharmony_ci      // Call GrowStack(isolate)
9791cb0ef41Sopenharmony_ci      static constexpr int kNumArguments = 1;
9801cb0ef41Sopenharmony_ci      __ PrepareCallCFunction(kNumArguments, a0);
9811cb0ef41Sopenharmony_ci      __ li(a0, Operand(ExternalReference::isolate_address(masm_->isolate())));
9821cb0ef41Sopenharmony_ci      ExternalReference grow_stack = ExternalReference::re_grow_stack();
9831cb0ef41Sopenharmony_ci      __ CallCFunction(grow_stack, kNumArguments);
9841cb0ef41Sopenharmony_ci      // If nullptr is returned, we have failed to grow the stack, and must exit
9851cb0ef41Sopenharmony_ci      // with a stack-overflow exception.
9861cb0ef41Sopenharmony_ci      __ Branch(&exit_with_exception, eq, v0, Operand(zero_reg));
9871cb0ef41Sopenharmony_ci      // Otherwise use return value as new stack pointer.
9881cb0ef41Sopenharmony_ci      __ mov(backtrack_stackpointer(), v0);
9891cb0ef41Sopenharmony_ci      SafeReturn();
9901cb0ef41Sopenharmony_ci    }
9911cb0ef41Sopenharmony_ci
9921cb0ef41Sopenharmony_ci    if (exit_with_exception.is_linked()) {
9931cb0ef41Sopenharmony_ci      // If any of the code above needed to exit with an exception.
9941cb0ef41Sopenharmony_ci      __ bind(&exit_with_exception);
9951cb0ef41Sopenharmony_ci      // Exit with Result EXCEPTION(-1) to signal thrown exception.
9961cb0ef41Sopenharmony_ci      __ li(v0, Operand(EXCEPTION));
9971cb0ef41Sopenharmony_ci      __ jmp(&return_v0);
9981cb0ef41Sopenharmony_ci    }
9991cb0ef41Sopenharmony_ci
10001cb0ef41Sopenharmony_ci    if (fallback_label_.is_linked()) {
10011cb0ef41Sopenharmony_ci      __ bind(&fallback_label_);
10021cb0ef41Sopenharmony_ci      __ li(v0, Operand(FALLBACK_TO_EXPERIMENTAL));
10031cb0ef41Sopenharmony_ci      __ jmp(&return_v0);
10041cb0ef41Sopenharmony_ci    }
10051cb0ef41Sopenharmony_ci  }
10061cb0ef41Sopenharmony_ci
10071cb0ef41Sopenharmony_ci  CodeDesc code_desc;
10081cb0ef41Sopenharmony_ci  masm_->GetCode(isolate(), &code_desc);
10091cb0ef41Sopenharmony_ci  Handle<Code> code =
10101cb0ef41Sopenharmony_ci      Factory::CodeBuilder(isolate(), code_desc, CodeKind::REGEXP)
10111cb0ef41Sopenharmony_ci          .set_self_reference(masm_->CodeObject())
10121cb0ef41Sopenharmony_ci          .Build();
10131cb0ef41Sopenharmony_ci  LOG(masm_->isolate(),
10141cb0ef41Sopenharmony_ci      RegExpCodeCreateEvent(Handle<AbstractCode>::cast(code), source));
10151cb0ef41Sopenharmony_ci  return Handle<HeapObject>::cast(code);
10161cb0ef41Sopenharmony_ci}
10171cb0ef41Sopenharmony_ci
10181cb0ef41Sopenharmony_ci
10191cb0ef41Sopenharmony_civoid RegExpMacroAssemblerMIPS::GoTo(Label* to) {
10201cb0ef41Sopenharmony_ci  if (to == nullptr) {
10211cb0ef41Sopenharmony_ci    Backtrack();
10221cb0ef41Sopenharmony_ci    return;
10231cb0ef41Sopenharmony_ci  }
10241cb0ef41Sopenharmony_ci  __ jmp(to);
10251cb0ef41Sopenharmony_ci  return;
10261cb0ef41Sopenharmony_ci}
10271cb0ef41Sopenharmony_ci
10281cb0ef41Sopenharmony_ci
10291cb0ef41Sopenharmony_civoid RegExpMacroAssemblerMIPS::IfRegisterGE(int reg,
10301cb0ef41Sopenharmony_ci                                            int comparand,
10311cb0ef41Sopenharmony_ci                                            Label* if_ge) {
10321cb0ef41Sopenharmony_ci  __ Ld(a0, register_location(reg));
10331cb0ef41Sopenharmony_ci  BranchOrBacktrack(if_ge, ge, a0, Operand(comparand));
10341cb0ef41Sopenharmony_ci}
10351cb0ef41Sopenharmony_ci
10361cb0ef41Sopenharmony_ci
10371cb0ef41Sopenharmony_civoid RegExpMacroAssemblerMIPS::IfRegisterLT(int reg,
10381cb0ef41Sopenharmony_ci                                            int comparand,
10391cb0ef41Sopenharmony_ci                                            Label* if_lt) {
10401cb0ef41Sopenharmony_ci  __ Ld(a0, register_location(reg));
10411cb0ef41Sopenharmony_ci  BranchOrBacktrack(if_lt, lt, a0, Operand(comparand));
10421cb0ef41Sopenharmony_ci}
10431cb0ef41Sopenharmony_ci
10441cb0ef41Sopenharmony_ci
10451cb0ef41Sopenharmony_civoid RegExpMacroAssemblerMIPS::IfRegisterEqPos(int reg,
10461cb0ef41Sopenharmony_ci                                               Label* if_eq) {
10471cb0ef41Sopenharmony_ci  __ Ld(a0, register_location(reg));
10481cb0ef41Sopenharmony_ci  BranchOrBacktrack(if_eq, eq, a0, Operand(current_input_offset()));
10491cb0ef41Sopenharmony_ci}
10501cb0ef41Sopenharmony_ci
10511cb0ef41Sopenharmony_ci
10521cb0ef41Sopenharmony_ciRegExpMacroAssembler::IrregexpImplementation
10531cb0ef41Sopenharmony_ci    RegExpMacroAssemblerMIPS::Implementation() {
10541cb0ef41Sopenharmony_ci  return kMIPSImplementation;
10551cb0ef41Sopenharmony_ci}
10561cb0ef41Sopenharmony_ci
10571cb0ef41Sopenharmony_ci
10581cb0ef41Sopenharmony_civoid RegExpMacroAssemblerMIPS::PopCurrentPosition() {
10591cb0ef41Sopenharmony_ci  Pop(current_input_offset());
10601cb0ef41Sopenharmony_ci}
10611cb0ef41Sopenharmony_ci
10621cb0ef41Sopenharmony_ci
10631cb0ef41Sopenharmony_civoid RegExpMacroAssemblerMIPS::PopRegister(int register_index) {
10641cb0ef41Sopenharmony_ci  Pop(a0);
10651cb0ef41Sopenharmony_ci  __ Sd(a0, register_location(register_index));
10661cb0ef41Sopenharmony_ci}
10671cb0ef41Sopenharmony_ci
10681cb0ef41Sopenharmony_ci
10691cb0ef41Sopenharmony_civoid RegExpMacroAssemblerMIPS::PushBacktrack(Label* label) {
10701cb0ef41Sopenharmony_ci  if (label->is_bound()) {
10711cb0ef41Sopenharmony_ci    int target = label->pos();
10721cb0ef41Sopenharmony_ci    __ li(a0, Operand(target + Code::kHeaderSize - kHeapObjectTag));
10731cb0ef41Sopenharmony_ci  } else {
10741cb0ef41Sopenharmony_ci    Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_.get());
10751cb0ef41Sopenharmony_ci    Label after_constant;
10761cb0ef41Sopenharmony_ci    __ Branch(&after_constant);
10771cb0ef41Sopenharmony_ci    int offset = masm_->pc_offset();
10781cb0ef41Sopenharmony_ci    int cp_offset = offset + Code::kHeaderSize - kHeapObjectTag;
10791cb0ef41Sopenharmony_ci    __ emit(0);
10801cb0ef41Sopenharmony_ci    masm_->label_at_put(label, offset);
10811cb0ef41Sopenharmony_ci    __ bind(&after_constant);
10821cb0ef41Sopenharmony_ci    if (is_int16(cp_offset)) {
10831cb0ef41Sopenharmony_ci      __ Lwu(a0, MemOperand(code_pointer(), cp_offset));
10841cb0ef41Sopenharmony_ci    } else {
10851cb0ef41Sopenharmony_ci      __ Daddu(a0, code_pointer(), cp_offset);
10861cb0ef41Sopenharmony_ci      __ Lwu(a0, MemOperand(a0, 0));
10871cb0ef41Sopenharmony_ci    }
10881cb0ef41Sopenharmony_ci  }
10891cb0ef41Sopenharmony_ci  Push(a0);
10901cb0ef41Sopenharmony_ci  CheckStackLimit();
10911cb0ef41Sopenharmony_ci}
10921cb0ef41Sopenharmony_ci
10931cb0ef41Sopenharmony_ci
10941cb0ef41Sopenharmony_civoid RegExpMacroAssemblerMIPS::PushCurrentPosition() {
10951cb0ef41Sopenharmony_ci  Push(current_input_offset());
10961cb0ef41Sopenharmony_ci}
10971cb0ef41Sopenharmony_ci
10981cb0ef41Sopenharmony_ci
10991cb0ef41Sopenharmony_civoid RegExpMacroAssemblerMIPS::PushRegister(int register_index,
11001cb0ef41Sopenharmony_ci                                            StackCheckFlag check_stack_limit) {
11011cb0ef41Sopenharmony_ci  __ Ld(a0, register_location(register_index));
11021cb0ef41Sopenharmony_ci  Push(a0);
11031cb0ef41Sopenharmony_ci  if (check_stack_limit) CheckStackLimit();
11041cb0ef41Sopenharmony_ci}
11051cb0ef41Sopenharmony_ci
11061cb0ef41Sopenharmony_ci
11071cb0ef41Sopenharmony_civoid RegExpMacroAssemblerMIPS::ReadCurrentPositionFromRegister(int reg) {
11081cb0ef41Sopenharmony_ci  __ Ld(current_input_offset(), register_location(reg));
11091cb0ef41Sopenharmony_ci}
11101cb0ef41Sopenharmony_ci
11111cb0ef41Sopenharmony_civoid RegExpMacroAssemblerMIPS::WriteStackPointerToRegister(int reg) {
11121cb0ef41Sopenharmony_ci  ExternalReference ref =
11131cb0ef41Sopenharmony_ci      ExternalReference::address_of_regexp_stack_memory_top_address(isolate());
11141cb0ef41Sopenharmony_ci  __ li(a0, Operand(ref));
11151cb0ef41Sopenharmony_ci  __ Ld(a0, MemOperand(a0));
11161cb0ef41Sopenharmony_ci  __ Dsubu(a0, backtrack_stackpointer(), a0);
11171cb0ef41Sopenharmony_ci  __ Sd(a0, register_location(reg));
11181cb0ef41Sopenharmony_ci}
11191cb0ef41Sopenharmony_ci
11201cb0ef41Sopenharmony_civoid RegExpMacroAssemblerMIPS::ReadStackPointerFromRegister(int reg) {
11211cb0ef41Sopenharmony_ci  ExternalReference ref =
11221cb0ef41Sopenharmony_ci      ExternalReference::address_of_regexp_stack_memory_top_address(isolate());
11231cb0ef41Sopenharmony_ci  __ li(a0, Operand(ref));
11241cb0ef41Sopenharmony_ci  __ Ld(a0, MemOperand(a0));
11251cb0ef41Sopenharmony_ci  __ Ld(backtrack_stackpointer(), register_location(reg));
11261cb0ef41Sopenharmony_ci  __ Daddu(backtrack_stackpointer(), backtrack_stackpointer(), Operand(a0));
11271cb0ef41Sopenharmony_ci}
11281cb0ef41Sopenharmony_ci
11291cb0ef41Sopenharmony_civoid RegExpMacroAssemblerMIPS::SetCurrentPositionFromEnd(int by) {
11301cb0ef41Sopenharmony_ci  Label after_position;
11311cb0ef41Sopenharmony_ci  __ Branch(&after_position,
11321cb0ef41Sopenharmony_ci            ge,
11331cb0ef41Sopenharmony_ci            current_input_offset(),
11341cb0ef41Sopenharmony_ci            Operand(-by * char_size()));
11351cb0ef41Sopenharmony_ci  __ li(current_input_offset(), -by * char_size());
11361cb0ef41Sopenharmony_ci  // On RegExp code entry (where this operation is used), the character before
11371cb0ef41Sopenharmony_ci  // the current position is expected to be already loaded.
11381cb0ef41Sopenharmony_ci  // We have advanced the position, so it's safe to read backwards.
11391cb0ef41Sopenharmony_ci  LoadCurrentCharacterUnchecked(-1, 1);
11401cb0ef41Sopenharmony_ci  __ bind(&after_position);
11411cb0ef41Sopenharmony_ci}
11421cb0ef41Sopenharmony_ci
11431cb0ef41Sopenharmony_ci
11441cb0ef41Sopenharmony_civoid RegExpMacroAssemblerMIPS::SetRegister(int register_index, int to) {
11451cb0ef41Sopenharmony_ci  DCHECK(register_index >= num_saved_registers_);  // Reserved for positions!
11461cb0ef41Sopenharmony_ci  __ li(a0, Operand(to));
11471cb0ef41Sopenharmony_ci  __ Sd(a0, register_location(register_index));
11481cb0ef41Sopenharmony_ci}
11491cb0ef41Sopenharmony_ci
11501cb0ef41Sopenharmony_ci
11511cb0ef41Sopenharmony_cibool RegExpMacroAssemblerMIPS::Succeed() {
11521cb0ef41Sopenharmony_ci  __ jmp(&success_label_);
11531cb0ef41Sopenharmony_ci  return global();
11541cb0ef41Sopenharmony_ci}
11551cb0ef41Sopenharmony_ci
11561cb0ef41Sopenharmony_ci
11571cb0ef41Sopenharmony_civoid RegExpMacroAssemblerMIPS::WriteCurrentPositionToRegister(int reg,
11581cb0ef41Sopenharmony_ci                                                              int cp_offset) {
11591cb0ef41Sopenharmony_ci  if (cp_offset == 0) {
11601cb0ef41Sopenharmony_ci    __ Sd(current_input_offset(), register_location(reg));
11611cb0ef41Sopenharmony_ci  } else {
11621cb0ef41Sopenharmony_ci    __ Daddu(a0, current_input_offset(), Operand(cp_offset * char_size()));
11631cb0ef41Sopenharmony_ci    __ Sd(a0, register_location(reg));
11641cb0ef41Sopenharmony_ci  }
11651cb0ef41Sopenharmony_ci}
11661cb0ef41Sopenharmony_ci
11671cb0ef41Sopenharmony_ci
11681cb0ef41Sopenharmony_civoid RegExpMacroAssemblerMIPS::ClearRegisters(int reg_from, int reg_to) {
11691cb0ef41Sopenharmony_ci  DCHECK(reg_from <= reg_to);
11701cb0ef41Sopenharmony_ci  __ Ld(a0, MemOperand(frame_pointer(), kStringStartMinusOne));
11711cb0ef41Sopenharmony_ci  for (int reg = reg_from; reg <= reg_to; reg++) {
11721cb0ef41Sopenharmony_ci    __ Sd(a0, register_location(reg));
11731cb0ef41Sopenharmony_ci  }
11741cb0ef41Sopenharmony_ci}
11751cb0ef41Sopenharmony_ci
11761cb0ef41Sopenharmony_cibool RegExpMacroAssemblerMIPS::CanReadUnaligned() const { return false; }
11771cb0ef41Sopenharmony_ci
11781cb0ef41Sopenharmony_ci// Private methods:
11791cb0ef41Sopenharmony_ci
11801cb0ef41Sopenharmony_civoid RegExpMacroAssemblerMIPS::CallCheckStackGuardState(Register scratch) {
11811cb0ef41Sopenharmony_ci  DCHECK(!isolate()->IsGeneratingEmbeddedBuiltins());
11821cb0ef41Sopenharmony_ci  DCHECK(!masm_->options().isolate_independent_code);
11831cb0ef41Sopenharmony_ci
11841cb0ef41Sopenharmony_ci  int stack_alignment = base::OS::ActivationFrameAlignment();
11851cb0ef41Sopenharmony_ci
11861cb0ef41Sopenharmony_ci  // Align the stack pointer and save the original sp value on the stack.
11871cb0ef41Sopenharmony_ci  __ mov(scratch, sp);
11881cb0ef41Sopenharmony_ci  __ Dsubu(sp, sp, Operand(kPointerSize));
11891cb0ef41Sopenharmony_ci  DCHECK(base::bits::IsPowerOfTwo(stack_alignment));
11901cb0ef41Sopenharmony_ci  __ And(sp, sp, Operand(-stack_alignment));
11911cb0ef41Sopenharmony_ci  __ Sd(scratch, MemOperand(sp));
11921cb0ef41Sopenharmony_ci
11931cb0ef41Sopenharmony_ci  __ mov(a2, frame_pointer());
11941cb0ef41Sopenharmony_ci  // Code of self.
11951cb0ef41Sopenharmony_ci  __ li(a1, Operand(masm_->CodeObject()), CONSTANT_SIZE);
11961cb0ef41Sopenharmony_ci
11971cb0ef41Sopenharmony_ci  // We need to make room for the return address on the stack.
11981cb0ef41Sopenharmony_ci  DCHECK(IsAligned(stack_alignment, kPointerSize));
11991cb0ef41Sopenharmony_ci  __ Dsubu(sp, sp, Operand(stack_alignment));
12001cb0ef41Sopenharmony_ci
12011cb0ef41Sopenharmony_ci  // The stack pointer now points to cell where the return address will be
12021cb0ef41Sopenharmony_ci  // written. Arguments are in registers, meaning we treat the return address as
12031cb0ef41Sopenharmony_ci  // argument 5. Since DirectCEntry will handle allocating space for the C
12041cb0ef41Sopenharmony_ci  // argument slots, we don't need to care about that here. This is how the
12051cb0ef41Sopenharmony_ci  // stack will look (sp meaning the value of sp at this moment):
12061cb0ef41Sopenharmony_ci  // [sp + 3] - empty slot if needed for alignment.
12071cb0ef41Sopenharmony_ci  // [sp + 2] - saved sp.
12081cb0ef41Sopenharmony_ci  // [sp + 1] - second word reserved for return value.
12091cb0ef41Sopenharmony_ci  // [sp + 0] - first word reserved for return value.
12101cb0ef41Sopenharmony_ci
12111cb0ef41Sopenharmony_ci  // a0 will point to the return address, placed by DirectCEntry.
12121cb0ef41Sopenharmony_ci  __ mov(a0, sp);
12131cb0ef41Sopenharmony_ci
12141cb0ef41Sopenharmony_ci  ExternalReference stack_guard_check =
12151cb0ef41Sopenharmony_ci      ExternalReference::re_check_stack_guard_state();
12161cb0ef41Sopenharmony_ci  __ li(t9, Operand(stack_guard_check));
12171cb0ef41Sopenharmony_ci
12181cb0ef41Sopenharmony_ci  EmbeddedData d = EmbeddedData::FromBlob();
12191cb0ef41Sopenharmony_ci  CHECK(Builtins::IsIsolateIndependent(Builtin::kDirectCEntry));
12201cb0ef41Sopenharmony_ci  Address entry = d.InstructionStartOfBuiltin(Builtin::kDirectCEntry);
12211cb0ef41Sopenharmony_ci  __ li(kScratchReg, Operand(entry, RelocInfo::OFF_HEAP_TARGET));
12221cb0ef41Sopenharmony_ci  __ Call(kScratchReg);
12231cb0ef41Sopenharmony_ci
12241cb0ef41Sopenharmony_ci  // DirectCEntry allocated space for the C argument slots so we have to
12251cb0ef41Sopenharmony_ci  // drop them with the return address from the stack with loading saved sp.
12261cb0ef41Sopenharmony_ci  // At this point stack must look:
12271cb0ef41Sopenharmony_ci  // [sp + 7] - empty slot if needed for alignment.
12281cb0ef41Sopenharmony_ci  // [sp + 6] - saved sp.
12291cb0ef41Sopenharmony_ci  // [sp + 5] - second word reserved for return value.
12301cb0ef41Sopenharmony_ci  // [sp + 4] - first word reserved for return value.
12311cb0ef41Sopenharmony_ci  // [sp + 3] - C argument slot.
12321cb0ef41Sopenharmony_ci  // [sp + 2] - C argument slot.
12331cb0ef41Sopenharmony_ci  // [sp + 1] - C argument slot.
12341cb0ef41Sopenharmony_ci  // [sp + 0] - C argument slot.
12351cb0ef41Sopenharmony_ci  __ Ld(sp, MemOperand(sp, stack_alignment + kCArgsSlotsSize));
12361cb0ef41Sopenharmony_ci
12371cb0ef41Sopenharmony_ci  __ li(code_pointer(), Operand(masm_->CodeObject()));
12381cb0ef41Sopenharmony_ci}
12391cb0ef41Sopenharmony_ci
12401cb0ef41Sopenharmony_ci
12411cb0ef41Sopenharmony_ci// Helper function for reading a value out of a stack frame.
12421cb0ef41Sopenharmony_citemplate <typename T>
12431cb0ef41Sopenharmony_cistatic T& frame_entry(Address re_frame, int frame_offset) {
12441cb0ef41Sopenharmony_ci  return reinterpret_cast<T&>(Memory<int32_t>(re_frame + frame_offset));
12451cb0ef41Sopenharmony_ci}
12461cb0ef41Sopenharmony_ci
12471cb0ef41Sopenharmony_ci
12481cb0ef41Sopenharmony_citemplate <typename T>
12491cb0ef41Sopenharmony_cistatic T* frame_entry_address(Address re_frame, int frame_offset) {
12501cb0ef41Sopenharmony_ci  return reinterpret_cast<T*>(re_frame + frame_offset);
12511cb0ef41Sopenharmony_ci}
12521cb0ef41Sopenharmony_ci
12531cb0ef41Sopenharmony_ciint64_t RegExpMacroAssemblerMIPS::CheckStackGuardState(Address* return_address,
12541cb0ef41Sopenharmony_ci                                                       Address raw_code,
12551cb0ef41Sopenharmony_ci                                                       Address re_frame) {
12561cb0ef41Sopenharmony_ci  Code re_code = Code::cast(Object(raw_code));
12571cb0ef41Sopenharmony_ci  return NativeRegExpMacroAssembler::CheckStackGuardState(
12581cb0ef41Sopenharmony_ci      frame_entry<Isolate*>(re_frame, kIsolate),
12591cb0ef41Sopenharmony_ci      static_cast<int>(frame_entry<int64_t>(re_frame, kStartIndex)),
12601cb0ef41Sopenharmony_ci      static_cast<RegExp::CallOrigin>(
12611cb0ef41Sopenharmony_ci          frame_entry<int64_t>(re_frame, kDirectCall)),
12621cb0ef41Sopenharmony_ci      return_address, re_code,
12631cb0ef41Sopenharmony_ci      frame_entry_address<Address>(re_frame, kInputString),
12641cb0ef41Sopenharmony_ci      frame_entry_address<const byte*>(re_frame, kInputStart),
12651cb0ef41Sopenharmony_ci      frame_entry_address<const byte*>(re_frame, kInputEnd));
12661cb0ef41Sopenharmony_ci}
12671cb0ef41Sopenharmony_ci
12681cb0ef41Sopenharmony_ci
12691cb0ef41Sopenharmony_ciMemOperand RegExpMacroAssemblerMIPS::register_location(int register_index) {
12701cb0ef41Sopenharmony_ci  DCHECK(register_index < (1<<30));
12711cb0ef41Sopenharmony_ci  if (num_registers_ <= register_index) {
12721cb0ef41Sopenharmony_ci    num_registers_ = register_index + 1;
12731cb0ef41Sopenharmony_ci  }
12741cb0ef41Sopenharmony_ci  return MemOperand(frame_pointer(),
12751cb0ef41Sopenharmony_ci                    kRegisterZero - register_index * kPointerSize);
12761cb0ef41Sopenharmony_ci}
12771cb0ef41Sopenharmony_ci
12781cb0ef41Sopenharmony_ci
12791cb0ef41Sopenharmony_civoid RegExpMacroAssemblerMIPS::CheckPosition(int cp_offset,
12801cb0ef41Sopenharmony_ci                                             Label* on_outside_input) {
12811cb0ef41Sopenharmony_ci  if (cp_offset >= 0) {
12821cb0ef41Sopenharmony_ci    BranchOrBacktrack(on_outside_input, ge, current_input_offset(),
12831cb0ef41Sopenharmony_ci                      Operand(-cp_offset * char_size()));
12841cb0ef41Sopenharmony_ci  } else {
12851cb0ef41Sopenharmony_ci    __ Ld(a1, MemOperand(frame_pointer(), kStringStartMinusOne));
12861cb0ef41Sopenharmony_ci    __ Daddu(a0, current_input_offset(), Operand(cp_offset * char_size()));
12871cb0ef41Sopenharmony_ci    BranchOrBacktrack(on_outside_input, le, a0, Operand(a1));
12881cb0ef41Sopenharmony_ci  }
12891cb0ef41Sopenharmony_ci}
12901cb0ef41Sopenharmony_ci
12911cb0ef41Sopenharmony_ci
12921cb0ef41Sopenharmony_civoid RegExpMacroAssemblerMIPS::BranchOrBacktrack(Label* to,
12931cb0ef41Sopenharmony_ci                                                 Condition condition,
12941cb0ef41Sopenharmony_ci                                                 Register rs,
12951cb0ef41Sopenharmony_ci                                                 const Operand& rt) {
12961cb0ef41Sopenharmony_ci  if (condition == al) {  // Unconditional.
12971cb0ef41Sopenharmony_ci    if (to == nullptr) {
12981cb0ef41Sopenharmony_ci      Backtrack();
12991cb0ef41Sopenharmony_ci      return;
13001cb0ef41Sopenharmony_ci    }
13011cb0ef41Sopenharmony_ci    __ jmp(to);
13021cb0ef41Sopenharmony_ci    return;
13031cb0ef41Sopenharmony_ci  }
13041cb0ef41Sopenharmony_ci  if (to == nullptr) {
13051cb0ef41Sopenharmony_ci    __ Branch(&backtrack_label_, condition, rs, rt);
13061cb0ef41Sopenharmony_ci    return;
13071cb0ef41Sopenharmony_ci  }
13081cb0ef41Sopenharmony_ci  __ Branch(to, condition, rs, rt);
13091cb0ef41Sopenharmony_ci}
13101cb0ef41Sopenharmony_ci
13111cb0ef41Sopenharmony_ci
13121cb0ef41Sopenharmony_civoid RegExpMacroAssemblerMIPS::SafeCall(Label* to,
13131cb0ef41Sopenharmony_ci                                        Condition cond,
13141cb0ef41Sopenharmony_ci                                        Register rs,
13151cb0ef41Sopenharmony_ci                                        const Operand& rt) {
13161cb0ef41Sopenharmony_ci  __ BranchAndLink(to, cond, rs, rt);
13171cb0ef41Sopenharmony_ci}
13181cb0ef41Sopenharmony_ci
13191cb0ef41Sopenharmony_ci
13201cb0ef41Sopenharmony_civoid RegExpMacroAssemblerMIPS::SafeReturn() {
13211cb0ef41Sopenharmony_ci  __ pop(ra);
13221cb0ef41Sopenharmony_ci  __ Daddu(t1, ra, Operand(masm_->CodeObject()));
13231cb0ef41Sopenharmony_ci  __ Jump(t1);
13241cb0ef41Sopenharmony_ci}
13251cb0ef41Sopenharmony_ci
13261cb0ef41Sopenharmony_ci
13271cb0ef41Sopenharmony_civoid RegExpMacroAssemblerMIPS::SafeCallTarget(Label* name) {
13281cb0ef41Sopenharmony_ci  __ bind(name);
13291cb0ef41Sopenharmony_ci  __ Dsubu(ra, ra, Operand(masm_->CodeObject()));
13301cb0ef41Sopenharmony_ci  __ push(ra);
13311cb0ef41Sopenharmony_ci}
13321cb0ef41Sopenharmony_ci
13331cb0ef41Sopenharmony_ci
13341cb0ef41Sopenharmony_civoid RegExpMacroAssemblerMIPS::Push(Register source) {
13351cb0ef41Sopenharmony_ci  DCHECK(source != backtrack_stackpointer());
13361cb0ef41Sopenharmony_ci  __ Daddu(backtrack_stackpointer(),
13371cb0ef41Sopenharmony_ci          backtrack_stackpointer(),
13381cb0ef41Sopenharmony_ci          Operand(-kIntSize));
13391cb0ef41Sopenharmony_ci  __ Sw(source, MemOperand(backtrack_stackpointer()));
13401cb0ef41Sopenharmony_ci}
13411cb0ef41Sopenharmony_ci
13421cb0ef41Sopenharmony_ci
13431cb0ef41Sopenharmony_civoid RegExpMacroAssemblerMIPS::Pop(Register target) {
13441cb0ef41Sopenharmony_ci  DCHECK(target != backtrack_stackpointer());
13451cb0ef41Sopenharmony_ci  __ Lw(target, MemOperand(backtrack_stackpointer()));
13461cb0ef41Sopenharmony_ci  __ Daddu(backtrack_stackpointer(), backtrack_stackpointer(), kIntSize);
13471cb0ef41Sopenharmony_ci}
13481cb0ef41Sopenharmony_ci
13491cb0ef41Sopenharmony_ci
13501cb0ef41Sopenharmony_civoid RegExpMacroAssemblerMIPS::CheckPreemption() {
13511cb0ef41Sopenharmony_ci  // Check for preemption.
13521cb0ef41Sopenharmony_ci  ExternalReference stack_limit =
13531cb0ef41Sopenharmony_ci      ExternalReference::address_of_jslimit(masm_->isolate());
13541cb0ef41Sopenharmony_ci  __ li(a0, Operand(stack_limit));
13551cb0ef41Sopenharmony_ci  __ Ld(a0, MemOperand(a0));
13561cb0ef41Sopenharmony_ci  SafeCall(&check_preempt_label_, ls, sp, Operand(a0));
13571cb0ef41Sopenharmony_ci}
13581cb0ef41Sopenharmony_ci
13591cb0ef41Sopenharmony_ci
13601cb0ef41Sopenharmony_civoid RegExpMacroAssemblerMIPS::CheckStackLimit() {
13611cb0ef41Sopenharmony_ci  ExternalReference stack_limit =
13621cb0ef41Sopenharmony_ci      ExternalReference::address_of_regexp_stack_limit_address(
13631cb0ef41Sopenharmony_ci          masm_->isolate());
13641cb0ef41Sopenharmony_ci
13651cb0ef41Sopenharmony_ci  __ li(a0, Operand(stack_limit));
13661cb0ef41Sopenharmony_ci  __ Ld(a0, MemOperand(a0));
13671cb0ef41Sopenharmony_ci  SafeCall(&stack_overflow_label_, ls, backtrack_stackpointer(), Operand(a0));
13681cb0ef41Sopenharmony_ci}
13691cb0ef41Sopenharmony_ci
13701cb0ef41Sopenharmony_ci
13711cb0ef41Sopenharmony_civoid RegExpMacroAssemblerMIPS::LoadCurrentCharacterUnchecked(int cp_offset,
13721cb0ef41Sopenharmony_ci                                                             int characters) {
13731cb0ef41Sopenharmony_ci  Register offset = current_input_offset();
13741cb0ef41Sopenharmony_ci  if (cp_offset != 0) {
13751cb0ef41Sopenharmony_ci    // t3 is not being used to store the capture start index at this point.
13761cb0ef41Sopenharmony_ci    __ Daddu(t3, current_input_offset(), Operand(cp_offset * char_size()));
13771cb0ef41Sopenharmony_ci    offset = t3;
13781cb0ef41Sopenharmony_ci  }
13791cb0ef41Sopenharmony_ci  // We assume that we cannot do unaligned loads on MIPS, so this function
13801cb0ef41Sopenharmony_ci  // must only be used to load a single character at a time.
13811cb0ef41Sopenharmony_ci  DCHECK_EQ(1, characters);
13821cb0ef41Sopenharmony_ci  __ Daddu(t1, end_of_input_address(), Operand(offset));
13831cb0ef41Sopenharmony_ci  if (mode_ == LATIN1) {
13841cb0ef41Sopenharmony_ci    __ Lbu(current_character(), MemOperand(t1, 0));
13851cb0ef41Sopenharmony_ci  } else {
13861cb0ef41Sopenharmony_ci    DCHECK(mode_ == UC16);
13871cb0ef41Sopenharmony_ci    __ Lhu(current_character(), MemOperand(t1, 0));
13881cb0ef41Sopenharmony_ci  }
13891cb0ef41Sopenharmony_ci}
13901cb0ef41Sopenharmony_ci
13911cb0ef41Sopenharmony_ci#undef __
13921cb0ef41Sopenharmony_ci
13931cb0ef41Sopenharmony_ci}  // namespace internal
13941cb0ef41Sopenharmony_ci}  // namespace v8
13951cb0ef41Sopenharmony_ci
13961cb0ef41Sopenharmony_ci#endif  // V8_TARGET_ARCH_MIPS64
1397