regexp-macro-assembler-arm64.cc 60.5 KB
Newer Older
1
// Copyright 2013 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5
#if V8_TARGET_ARCH_ARM64
6

7 8
#include "src/regexp/arm64/regexp-macro-assembler-arm64.h"

9
#include "src/codegen/arm64/macro-assembler-arm64-inl.h"
10
#include "src/codegen/macro-assembler.h"
11
#include "src/logging/log.h"
12
#include "src/objects/objects-inl.h"
13 14
#include "src/regexp/regexp-macro-assembler.h"
#include "src/regexp/regexp-stack.h"
15
#include "src/snapshot/embedded/embedded-data.h"
16
#include "src/strings/unicode.h"
17

18 19 20 21 22 23 24
namespace v8 {
namespace internal {

/*
 * This assembler uses the following register assignment convention:
 * - w19     : Used to temporarely store a value before a call to C code.
 *             See CheckNotBackReferenceIgnoreCase.
25
 * - x20     : Pointer to the current Code object,
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
 *             it includes the heap object tag.
 * - w21     : Current position in input, as negative offset from
 *             the end of the string. Please notice that this is
 *             the byte offset, not the character offset!
 * - w22     : Currently loaded character. Must be loaded using
 *             LoadCurrentCharacter before using any of the dispatch methods.
 * - x23     : Points to tip of backtrack stack.
 * - w24     : Position of the first character minus one: non_position_value.
 *             Used to initialize capture registers.
 * - x25     : Address at the end of the input string: input_end.
 *             Points to byte after last character in input.
 * - x26     : Address at the start of the input string: input_start.
 * - w27     : Where to start in the input string.
 * - x28     : Output array pointer.
 * - x29/fp  : Frame pointer. Used to access arguments, local variables and
 *             RegExp registers.
 * - x16/x17 : IP registers, used by assembler. Very volatile.
43
 * - sp      : Points to tip of C stack.
44 45 46 47 48 49 50 51 52 53
 *
 * - x0-x7   : Used as a cache to store 32 bit capture registers. These
 *             registers need to be retained every time a call to C code
 *             is done.
 *
 * The remaining registers are free for computations.
 * Each call to a public method should retain this convention.
 *
 * The stack will have the following structure:
 *
54 55 56
 *  Location     Name               Description
 *               (as referred to
 *               in the code)
57
 *
58 59 60 61 62 63 64 65 66 67 68
 *  - fp[104]    Address regexp     Address of the JSRegExp object. Unused in
 *                                  native code, passed to match signature of
 *                                  the interpreter.
 *  - fp[96]     isolate            Address of the current isolate.
 *  ^^^^^^^^^ sp when called ^^^^^^^^^
 *  - fp[16..88] r19-r28            Backup of CalleeSaved registers.
 *  - fp[8]      lr                 Return from the RegExp code.
 *  - fp[0]      fp                 Old frame pointer.
 *  ^^^^^^^^^ fp ^^^^^^^^^
 *  - fp[-8]     direct_call        1 => Direct call from JavaScript code.
 *                                  0 => Call through the runtime system.
69 70 71
 *  - fp[-16]    output_size        Output may fit multiple sets of matches.
 *  - fp[-24]    input              Handle containing the input string.
 *  - fp[-32]    success_counter
72
 *  ^^^^^^^^^^^^^ From here and downwards we store 32 bit values ^^^^^^^^^^^^^
73 74
 *  - fp[-40]    register N         Capture registers initialized with
 *  - fp[-44]    register N + 1     non_position_value.
75 76 77 78 79 80
 *               ...                The first kNumCachedRegisters (N) registers
 *               ...                are cached in x0 to x7.
 *               ...                Only positions must be stored in the first
 *  -            ...                num_saved_registers_ registers.
 *  -            ...
 *  -            register N + num_registers - 1
81
 *  ^^^^^^^^^ sp ^^^^^^^^^
82 83 84 85 86 87 88 89
 *
 * The first num_saved_registers_ registers are initialized to point to
 * "character -1" in the string (i.e., char_size() bytes before the first
 * character of the string). The remaining registers start out as garbage.
 *
 * The data up to the return address must be placed there by the calling
 * code and the remaining arguments are passed in registers, e.g. by calling the
 * code entry as cast to a function with the signature:
90
 * int (*match)(String input_string,
91 92 93 94 95
 *              int start_index,
 *              Address start,
 *              Address end,
 *              int* capture_output_array,
 *              int num_capture_registers,
96
 *              bool direct_call = false,
97 98
 *              Isolate* isolate,
 *              Address regexp);
99
 * The call is performed by NativeRegExpMacroAssembler::Execute()
100
 * (in regexp-macro-assembler.cc) via the GeneratedCode wrapper.
101 102 103 104
 */

#define __ ACCESS_MASM(masm_)

105 106
const int RegExpMacroAssemblerARM64::kRegExpCodeSize;

107 108 109 110
RegExpMacroAssemblerARM64::RegExpMacroAssemblerARM64(Isolate* isolate,
                                                     Zone* zone, Mode mode,
                                                     int registers_to_save)
    : NativeRegExpMacroAssembler(isolate, zone),
111 112 113 114
      masm_(std::make_unique<MacroAssembler>(
          isolate, CodeObjectRequired::kYes,
          NewAssemblerBuffer(kRegExpCodeSize))),
      no_root_array_scope_(masm_.get()),
115 116 117 118 119 120 121 122
      mode_(mode),
      num_registers_(registers_to_save),
      num_saved_registers_(registers_to_save),
      entry_label_(),
      start_label_(),
      success_label_(),
      backtrack_label_(),
      exit_label_() {
123
  DCHECK_EQ(0, registers_to_save % 2);
124
  // We can cache at most 16 W registers in x0-x7.
125 126
  static_assert(kNumCachedRegisters <= 16);
  static_assert((kNumCachedRegisters % 2) == 0);
127 128
  __ CallTarget();

129 130 131 132
  __ B(&entry_label_);   // We'll write the entry code later.
  __ Bind(&start_label_);  // And then continue from here.
}

133 134 135 136
RegExpMacroAssemblerARM64::~RegExpMacroAssemblerARM64() = default;

void RegExpMacroAssemblerARM64::AbortedCodeGeneration() {
  masm_->AbortedCodeGeneration();
137 138 139 140 141 142 143
  entry_label_.Unuse();
  start_label_.Unuse();
  success_label_.Unuse();
  backtrack_label_.Unuse();
  exit_label_.Unuse();
  check_preempt_label_.Unuse();
  stack_overflow_label_.Unuse();
144
  fallback_label_.Unuse();
145 146
}

147
int RegExpMacroAssemblerARM64::stack_limit_slack()  {
148 149 150 151
  return RegExpStack::kStackLimitSlack;
}


152
void RegExpMacroAssemblerARM64::AdvanceCurrentPosition(int by) {
153 154 155 156 157 158 159
  if (by != 0) {
    __ Add(current_input_offset(),
           current_input_offset(), by * char_size());
  }
}


160
void RegExpMacroAssemblerARM64::AdvanceRegister(int reg, int by) {
161
  DCHECK((reg >= 0) && (reg < num_registers_));
162 163 164 165 166 167 168 169
  if (by != 0) {
    RegisterState register_state = GetRegisterState(reg);
    switch (register_state) {
      case STACKED:
        __ Ldr(w10, register_location(reg));
        __ Add(w10, w10, by);
        __ Str(w10, register_location(reg));
        break;
170 171
      case CACHED_LSW: {
        Register to_advance = GetCachedRegister(reg);
172 173
        __ Add(to_advance, to_advance, by);
        break;
174 175 176
      }
      case CACHED_MSW: {
        Register to_advance = GetCachedRegister(reg);
177 178 179 180 181
        // Sign-extend to int64, shift as uint64, cast back to int64.
        __ Add(
            to_advance, to_advance,
            static_cast<int64_t>(static_cast<uint64_t>(static_cast<int64_t>(by))
                                 << kWRegSizeInBits));
182
        break;
183
      }
184 185 186 187 188 189 190
      default:
        UNREACHABLE();
    }
  }
}


191
void RegExpMacroAssemblerARM64::Backtrack() {
192
  CheckPreemption();
193 194
  if (has_backtrack_limit()) {
    Label next;
195
    UseScratchRegisterScope temps(masm_.get());
196 197 198 199 200 201 202
    Register scratch = temps.AcquireW();
    __ Ldr(scratch, MemOperand(frame_pointer(), kBacktrackCount));
    __ Add(scratch, scratch, 1);
    __ Str(scratch, MemOperand(frame_pointer(), kBacktrackCount));
    __ Cmp(scratch, Operand(backtrack_limit()));
    __ B(ne, &next);

203 204 205 206 207 208 209
    // Backtrack limit exceeded.
    if (can_fallback()) {
      __ B(&fallback_label_);
    } else {
      // Can't fallback, so we treat it as a failed match.
      Fail();
    }
210 211 212

    __ bind(&next);
  }
213 214 215 216 217 218
  Pop(w10);
  __ Add(x10, code_pointer(), Operand(w10, UXTW));
  __ Br(x10);
}


219
void RegExpMacroAssemblerARM64::Bind(Label* label) {
220 221 222
  __ Bind(label);
}

223 224 225
void RegExpMacroAssemblerARM64::BindJumpTarget(Label* label) {
  __ BindJumpTarget(label);
}
226

227
void RegExpMacroAssemblerARM64::CheckCharacter(uint32_t c, Label* on_equal) {
228 229 230
  CompareAndBranchOrBacktrack(current_character(), c, eq, on_equal);
}

231
void RegExpMacroAssemblerARM64::CheckCharacterGT(base::uc16 limit,
232
                                                 Label* on_greater) {
233 234 235
  CompareAndBranchOrBacktrack(current_character(), limit, hi, on_greater);
}

236 237 238 239
void RegExpMacroAssemblerARM64::CheckAtStart(int cp_offset,
                                             Label* on_at_start) {
  __ Add(w10, current_input_offset(),
         Operand(-char_size() + cp_offset * char_size()));
240
  __ Cmp(w10, string_start_minus_one());
241 242 243
  BranchOrBacktrack(eq, on_at_start);
}

244 245 246 247 248
void RegExpMacroAssemblerARM64::CheckNotAtStart(int cp_offset,
                                                Label* on_not_at_start) {
  __ Add(w10, current_input_offset(),
         Operand(-char_size() + cp_offset * char_size()));
  __ Cmp(w10, string_start_minus_one());
249 250 251
  BranchOrBacktrack(ne, on_not_at_start);
}

252 253
void RegExpMacroAssemblerARM64::CheckCharacterLT(base::uc16 limit,
                                                 Label* on_less) {
254 255 256
  CompareAndBranchOrBacktrack(current_character(), limit, lo, on_less);
}

257 258 259
void RegExpMacroAssemblerARM64::CheckCharacters(
    base::Vector<const base::uc16> str, int cp_offset, Label* on_failure,
    bool check_end_of_string) {
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
  // This method is only ever called from the cctests.

  if (check_end_of_string) {
    // Is last character of required match inside string.
    CheckPosition(cp_offset + str.length() - 1, on_failure);
  }

  Register characters_address = x11;

  __ Add(characters_address,
         input_end(),
         Operand(current_input_offset(), SXTW));
  if (cp_offset != 0) {
    __ Add(characters_address, characters_address, cp_offset * char_size());
  }

  for (int i = 0; i < str.length(); i++) {
277
    if (mode_ == LATIN1) {
278
      __ Ldrb(w10, MemOperand(characters_address, 1, PostIndex));
279
      DCHECK_GE(String::kMaxOneByteCharCode, str[i]);
280 281 282 283 284 285 286
    } else {
      __ Ldrh(w10, MemOperand(characters_address, 2, PostIndex));
    }
    CompareAndBranchOrBacktrack(w10, str[i], ne, on_failure);
  }
}

287
void RegExpMacroAssemblerARM64::CheckGreedyLoop(Label* on_equal) {
288 289 290 291
  __ Ldr(w10, MemOperand(backtrack_stackpointer()));
  __ Cmp(current_input_offset(), w10);
  __ Cset(x11, eq);
  __ Add(backtrack_stackpointer(),
292
         backtrack_stackpointer(), Operand(x11, LSL, kWRegSizeLog2));
293 294 295
  BranchOrBacktrack(eq, on_equal);
}

296 297 298 299 300 301 302 303 304 305 306 307
void RegExpMacroAssemblerARM64::PushCachedRegisters() {
  CPURegList cached_registers(CPURegister::kRegister, kXRegSizeInBits, 0, 7);
  DCHECK_EQ(kNumCachedRegisters, cached_registers.Count() * 2);
  __ PushCPURegList(cached_registers);
}

void RegExpMacroAssemblerARM64::PopCachedRegisters() {
  CPURegList cached_registers(CPURegister::kRegister, kXRegSizeInBits, 0, 7);
  DCHECK_EQ(kNumCachedRegisters, cached_registers.Count() * 2);
  __ PopCPURegList(cached_registers);
}

308
void RegExpMacroAssemblerARM64::CheckNotBackReferenceIgnoreCase(
309
    int start_reg, bool read_backward, bool unicode, Label* on_no_match) {
310 311 312 313 314 315
  Label fallthrough;

  Register capture_start_offset = w10;
  // Save the capture length in a callee-saved register so it will
  // be preserved if we call a C helper.
  Register capture_length = w19;
316
  DCHECK(kCalleeSaved.IncludesAliasOf(capture_length));
317 318

  // Find length of back-referenced capture.
319
  DCHECK_EQ(0, start_reg % 2);
320 321
  if (start_reg < kNumCachedRegisters) {
    __ Mov(capture_start_offset.X(), GetCachedRegister(start_reg));
322
    __ Lsr(x11, GetCachedRegister(start_reg), kWRegSizeInBits);
323 324 325 326
  } else {
    __ Ldp(w11, capture_start_offset, capture_location(start_reg, x10));
  }
  __ Sub(capture_length, w11, capture_start_offset);  // Length to check.
327 328 329 330 331

  // At this point, the capture registers are either both set or both cleared.
  // If the capture length is zero, then the capture is either empty or cleared.
  // Fall through in both cases.
  __ CompareAndBranch(capture_length, Operand(0), eq, &fallthrough);
332 333

  // Check that there are enough characters left in the input.
334 335 336 337 338 339 340 341
  if (read_backward) {
    __ Add(w12, string_start_minus_one(), capture_length);
    __ Cmp(current_input_offset(), w12);
    BranchOrBacktrack(le, on_no_match);
  } else {
    __ Cmn(capture_length, current_input_offset());
    BranchOrBacktrack(gt, on_no_match);
  }
342

343
  if (mode_ == LATIN1) {
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
    Label success;
    Label fail;
    Label loop_check;

    Register capture_start_address = x12;
    Register capture_end_addresss = x13;
    Register current_position_address = x14;

    __ Add(capture_start_address,
           input_end(),
           Operand(capture_start_offset, SXTW));
    __ Add(capture_end_addresss,
           capture_start_address,
           Operand(capture_length, SXTW));
    __ Add(current_position_address,
           input_end(),
           Operand(current_input_offset(), SXTW));
361 362 363 364 365
    if (read_backward) {
      // Offset by length when matching backwards.
      __ Sub(current_position_address, current_position_address,
             Operand(capture_length, SXTW));
    }
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384

    Label loop;
    __ Bind(&loop);
    __ Ldrb(w10, MemOperand(capture_start_address, 1, PostIndex));
    __ Ldrb(w11, MemOperand(current_position_address, 1, PostIndex));
    __ Cmp(w10, w11);
    __ B(eq, &loop_check);

    // Mismatch, try case-insensitive match (converting letters to lower-case).
    __ Orr(w10, w10, 0x20);  // Convert capture character to lower-case.
    __ Orr(w11, w11, 0x20);  // Also convert input character.
    __ Cmp(w11, w10);
    __ B(ne, &fail);
    __ Sub(w10, w10, 'a');
    __ Cmp(w10, 'z' - 'a');  // Is w10 a lowercase letter?
    __ B(ls, &loop_check);  // In range 'a'-'z'.
    // Latin-1: Check for values in range [224,254] but not 247.
    __ Sub(w10, w10, 224 - 'a');
    __ Cmp(w10, 254 - 224);
385 386
    __ Ccmp(w10, 247 - 224, ZFlag, ls);  // Check for 247.
    __ B(eq, &fail);  // Weren't Latin-1 letters.
387 388 389 390 391 392 393 394 395 396 397 398

    __ Bind(&loop_check);
    __ Cmp(capture_start_address, capture_end_addresss);
    __ B(lt, &loop);
    __ B(&success);

    __ Bind(&fail);
    BranchOrBacktrack(al, on_no_match);

    __ Bind(&success);
    // Compute new value of character position after the matched part.
    __ Sub(current_input_offset().X(), current_position_address, input_end());
399 400 401 402
    if (read_backward) {
      __ Sub(current_input_offset().X(), current_input_offset().X(),
             Operand(capture_length, SXTW));
    }
403
    if (FLAG_debug_code) {
404 405 406
      __ Cmp(current_input_offset().X(), Operand(current_input_offset(), SXTW));
      __ Ccmp(current_input_offset(), 0, NoFlag, eq);
      // The current input offset should be <= 0, and fit in a W register.
407
      __ Check(le, AbortReason::kOffsetOutOfRange);
408 409
    }
  } else {
410
    DCHECK(mode_ == UC16);
411 412
    int argument_count = 4;

413
    PushCachedRegisters();
414 415 416 417 418 419

    // Put arguments into arguments registers.
    // Parameters are
    //   x0: Address byte_offset1 - Address captured substring's start.
    //   x1: Address byte_offset2 - Address of current character position.
    //   w2: size_t byte_length - length of capture in bytes(!)
420
    //   x3: Isolate* isolate.
421 422 423 424 425 426 427

    // Address of start of capture.
    __ Add(x0, input_end(), Operand(capture_start_offset, SXTW));
    // Length of capture.
    __ Mov(w2, capture_length);
    // Address of current input position.
    __ Add(x1, input_end(), Operand(current_input_offset(), SXTW));
428 429 430
    if (read_backward) {
      __ Sub(x1, x1, Operand(capture_length, SXTW));
    }
431
    // Isolate.
432
    __ Mov(x3, ExternalReference::isolate_address(isolate()));
433 434

    {
435
      AllowExternalCallThatCantCauseGC scope(masm_.get());
436
      ExternalReference function =
437 438 439
          unicode
              ? ExternalReference::re_case_insensitive_compare_unicode()
              : ExternalReference::re_case_insensitive_compare_non_unicode();
440 441 442 443
      __ CallCFunction(function, argument_count);
    }

    // Check if function returned non-zero for success or zero for failure.
444 445 446
    // x0 is one of the registers used as a cache so it must be tested before
    // the cache is restored.
    __ Cmp(x0, 0);
447
    PopCachedRegisters();
448 449
    BranchOrBacktrack(eq, on_no_match);

450 451 452 453 454 455
    // On success, advance position by length of capture.
    if (read_backward) {
      __ Sub(current_input_offset(), current_input_offset(), capture_length);
    } else {
      __ Add(current_input_offset(), current_input_offset(), capture_length);
    }
456 457 458 459 460
  }

  __ Bind(&fallthrough);
}

461 462 463
void RegExpMacroAssemblerARM64::CheckNotBackReference(int start_reg,
                                                      bool read_backward,
                                                      Label* on_no_match) {
464 465 466 467 468 469 470 471
  Label fallthrough;

  Register capture_start_address = x12;
  Register capture_end_address = x13;
  Register current_position_address = x14;
  Register capture_length = w15;

  // Find length of back-referenced capture.
472
  DCHECK_EQ(0, start_reg % 2);
473 474
  if (start_reg < kNumCachedRegisters) {
    __ Mov(x10, GetCachedRegister(start_reg));
475
    __ Lsr(x11, GetCachedRegister(start_reg), kWRegSizeInBits);
476 477 478 479
  } else {
    __ Ldp(w11, w10, capture_location(start_reg, x10));
  }
  __ Sub(capture_length, w11, w10);  // Length to check.
480 481 482 483 484

  // At this point, the capture registers are either both set or both cleared.
  // If the capture length is zero, then the capture is either empty or cleared.
  // Fall through in both cases.
  __ CompareAndBranch(capture_length, Operand(0), eq, &fallthrough);
485 486

  // Check that there are enough characters left in the input.
487 488 489 490 491 492 493 494
  if (read_backward) {
    __ Add(w12, string_start_minus_one(), capture_length);
    __ Cmp(current_input_offset(), w12);
    BranchOrBacktrack(le, on_no_match);
  } else {
    __ Cmn(capture_length, current_input_offset());
    BranchOrBacktrack(gt, on_no_match);
  }
495 496 497 498 499 500 501 502 503

  // Compute pointers to match string and capture string
  __ Add(capture_start_address, input_end(), Operand(w10, SXTW));
  __ Add(capture_end_address,
         capture_start_address,
         Operand(capture_length, SXTW));
  __ Add(current_position_address,
         input_end(),
         Operand(current_input_offset(), SXTW));
504 505 506 507 508
  if (read_backward) {
    // Offset by length when matching backwards.
    __ Sub(current_position_address, current_position_address,
           Operand(capture_length, SXTW));
  }
509 510 511

  Label loop;
  __ Bind(&loop);
512
  if (mode_ == LATIN1) {
513 514 515
    __ Ldrb(w10, MemOperand(capture_start_address, 1, PostIndex));
    __ Ldrb(w11, MemOperand(current_position_address, 1, PostIndex));
  } else {
516
    DCHECK(mode_ == UC16);
517 518 519 520 521 522 523 524 525 526
    __ Ldrh(w10, MemOperand(capture_start_address, 2, PostIndex));
    __ Ldrh(w11, MemOperand(current_position_address, 2, PostIndex));
  }
  __ Cmp(w10, w11);
  BranchOrBacktrack(ne, on_no_match);
  __ Cmp(capture_start_address, capture_end_address);
  __ B(lt, &loop);

  // Move current character position to position after match.
  __ Sub(current_input_offset().X(), current_position_address, input_end());
527 528 529 530 531
  if (read_backward) {
    __ Sub(current_input_offset().X(), current_input_offset().X(),
           Operand(capture_length, SXTW));
  }

532
  if (FLAG_debug_code) {
533 534 535
    __ Cmp(current_input_offset().X(), Operand(current_input_offset(), SXTW));
    __ Ccmp(current_input_offset(), 0, NoFlag, eq);
    // The current input offset should be <= 0, and fit in a W register.
536
    __ Check(le, AbortReason::kOffsetOutOfRange);
537 538 539 540 541
  }
  __ Bind(&fallthrough);
}


542 543
void RegExpMacroAssemblerARM64::CheckNotCharacter(unsigned c,
                                                  Label* on_not_equal) {
544 545 546 547
  CompareAndBranchOrBacktrack(current_character(), c, ne, on_not_equal);
}


548 549 550
void RegExpMacroAssemblerARM64::CheckCharacterAfterAnd(uint32_t c,
                                                       uint32_t mask,
                                                       Label* on_equal) {
551 552 553 554 555
  __ And(w10, current_character(), mask);
  CompareAndBranchOrBacktrack(w10, c, eq, on_equal);
}


556 557 558
void RegExpMacroAssemblerARM64::CheckNotCharacterAfterAnd(unsigned c,
                                                          unsigned mask,
                                                          Label* on_not_equal) {
559 560 561 562
  __ And(w10, current_character(), mask);
  CompareAndBranchOrBacktrack(w10, c, ne, on_not_equal);
}

563
void RegExpMacroAssemblerARM64::CheckNotCharacterAfterMinusAnd(
564
    base::uc16 c, base::uc16 minus, base::uc16 mask, Label* on_not_equal) {
565
  DCHECK_GT(String::kMaxUtf16CodeUnit, minus);
566 567 568 569 570
  __ Sub(w10, current_character(), minus);
  __ And(w10, w10, mask);
  CompareAndBranchOrBacktrack(w10, c, ne, on_not_equal);
}

571 572 573
void RegExpMacroAssemblerARM64::CheckCharacterInRange(base::uc16 from,
                                                      base::uc16 to,
                                                      Label* on_in_range) {
574 575 576 577 578
  __ Sub(w10, current_character(), from);
  // Unsigned lower-or-same condition.
  CompareAndBranchOrBacktrack(w10, to - from, ls, on_in_range);
}

579
void RegExpMacroAssemblerARM64::CheckCharacterNotInRange(
580
    base::uc16 from, base::uc16 to, Label* on_not_in_range) {
581 582 583 584 585
  __ Sub(w10, current_character(), from);
  // Unsigned higher condition.
  CompareAndBranchOrBacktrack(w10, to - from, hi, on_not_in_range);
}

586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
void RegExpMacroAssemblerARM64::CallIsCharacterInRangeArray(
    const ZoneList<CharacterRange>* ranges) {
  static const int kNumArguments = 3;
  __ Mov(w0, current_character());
  __ Mov(x1, GetOrAddRangeArray(ranges));
  __ Mov(x2, ExternalReference::isolate_address(isolate()));

  {
    // We have a frame (set up in GetCode), but the assembler doesn't know.
    FrameScope scope(masm_.get(), StackFrame::MANUAL);
    __ CallCFunction(ExternalReference::re_is_character_in_range_array(),
                     kNumArguments);
  }

  __ Mov(code_pointer(), Operand(masm_->CodeObject()));
}

bool RegExpMacroAssemblerARM64::CheckCharacterInRangeArray(
    const ZoneList<CharacterRange>* ranges, Label* on_in_range) {
  // Note: due to the arm64 oddity of x0 being a 'cached register',
  // pushing/popping registers must happen outside of CallIsCharacterInRange
  // s.t. we can compare the return value to 0 before popping x0.
  PushCachedRegisters();
  CallIsCharacterInRangeArray(ranges);
  __ Cmp(x0, 0);
  PopCachedRegisters();
  BranchOrBacktrack(ne, on_in_range);
  return true;
}

bool RegExpMacroAssemblerARM64::CheckCharacterNotInRangeArray(
    const ZoneList<CharacterRange>* ranges, Label* on_not_in_range) {
  // Note: due to the arm64 oddity of x0 being a 'cached register',
  // pushing/popping registers must happen outside of CallIsCharacterInRange
  // s.t. we can compare the return value to 0 before popping x0.
  PushCachedRegisters();
  CallIsCharacterInRangeArray(ranges);
  __ Cmp(x0, 0);
  PopCachedRegisters();
  BranchOrBacktrack(eq, on_not_in_range);
  return true;
}

629
void RegExpMacroAssemblerARM64::CheckBitInTable(
630 631 632
    Handle<ByteArray> table,
    Label* on_bit_set) {
  __ Mov(x11, Operand(table));
633
  if ((mode_ != LATIN1) || (kTableMask != String::kMaxOneByteCharCode)) {
634 635 636 637 638 639 640 641 642
    __ And(w10, current_character(), kTableMask);
    __ Add(w10, w10, ByteArray::kHeaderSize - kHeapObjectTag);
  } else {
    __ Add(w10, current_character(), ByteArray::kHeaderSize - kHeapObjectTag);
  }
  __ Ldrb(w11, MemOperand(x11, w10, UXTW));
  CompareAndBranchOrBacktrack(w11, 0, ne, on_bit_set);
}

643 644
bool RegExpMacroAssemblerARM64::CheckSpecialCharacterClass(
    StandardCharacterSet type, Label* on_no_match) {
645 646
  // Range checks (c in min..max) are generally implemented by an unsigned
  // (c - min) <= (max - min) check
647
  // TODO(jgruber): No custom implementation (yet): s(UC16), S(UC16).
648
  switch (type) {
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
    case StandardCharacterSet::kWhitespace:
      // Match space-characters.
      if (mode_ == LATIN1) {
        // One byte space characters are '\t'..'\r', ' ' and \u00a0.
        Label success;
        // Check for ' ' or 0x00A0.
        __ Cmp(current_character(), ' ');
        __ Ccmp(current_character(), 0x00A0, ZFlag, ne);
        __ B(eq, &success);
        // Check range 0x09..0x0D.
        __ Sub(w10, current_character(), '\t');
        CompareAndBranchOrBacktrack(w10, '\r' - '\t', hi, on_no_match);
        __ Bind(&success);
        return true;
      }
      return false;
    case StandardCharacterSet::kNotWhitespace:
      // The emitted code for generic character classes is good enough.
      return false;
    case StandardCharacterSet::kDigit:
      // Match ASCII digits ('0'..'9').
      __ Sub(w10, current_character(), '0');
      CompareAndBranchOrBacktrack(w10, '9' - '0', hi, on_no_match);
      return true;
    case StandardCharacterSet::kNotDigit:
      // Match ASCII non-digits.
      __ Sub(w10, current_character(), '0');
      CompareAndBranchOrBacktrack(w10, '9' - '0', ls, on_no_match);
      return true;
    case StandardCharacterSet::kNotLineTerminator: {
      // Match non-newlines (not 0x0A('\n'), 0x0D('\r'), 0x2028 and 0x2029)
      // Here we emit the conditional branch only once at the end to make branch
      // prediction more efficient, even though we could branch out of here
      // as soon as a character matches.
      __ Cmp(current_character(), 0x0A);
      __ Ccmp(current_character(), 0x0D, ZFlag, ne);
      if (mode_ == UC16) {
        __ Sub(w10, current_character(), 0x2028);
        // If the Z flag was set we clear the flags to force a branch.
        __ Ccmp(w10, 0x2029 - 0x2028, NoFlag, ne);
        // ls -> !((C==1) && (Z==0))
        BranchOrBacktrack(ls, on_no_match);
      } else {
        BranchOrBacktrack(eq, on_no_match);
      }
694 695
      return true;
    }
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
    case StandardCharacterSet::kLineTerminator: {
      // Match newlines (0x0A('\n'), 0x0D('\r'), 0x2028 and 0x2029)
      // We have to check all 4 newline characters before emitting
      // the conditional branch.
      __ Cmp(current_character(), 0x0A);
      __ Ccmp(current_character(), 0x0D, ZFlag, ne);
      if (mode_ == UC16) {
        __ Sub(w10, current_character(), 0x2028);
        // If the Z flag was set we clear the flags to force a fall-through.
        __ Ccmp(w10, 0x2029 - 0x2028, NoFlag, ne);
        // hi -> (C==1) && (Z==0)
        BranchOrBacktrack(hi, on_no_match);
      } else {
        BranchOrBacktrack(ne, on_no_match);
      }
      return true;
712
    }
713 714 715 716 717 718 719 720 721 722
    case StandardCharacterSet::kWord: {
      if (mode_ != LATIN1) {
        // Table is 256 entries, so all Latin1 characters can be tested.
        CompareAndBranchOrBacktrack(current_character(), 'z', hi, on_no_match);
      }
      ExternalReference map = ExternalReference::re_word_character_map();
      __ Mov(x10, map);
      __ Ldrb(w10, MemOperand(x10, current_character(), UXTW));
      CompareAndBranchOrBacktrack(w10, 0, eq, on_no_match);
      return true;
723
    }
724 725 726 727 728 729 730 731 732 733 734 735 736
    case StandardCharacterSet::kNotWord: {
      Label done;
      if (mode_ != LATIN1) {
        // Table is 256 entries, so all Latin1 characters can be tested.
        __ Cmp(current_character(), 'z');
        __ B(hi, &done);
      }
      ExternalReference map = ExternalReference::re_word_character_map();
      __ Mov(x10, map);
      __ Ldrb(w10, MemOperand(x10, current_character(), UXTW));
      CompareAndBranchOrBacktrack(w10, 0, ne, on_no_match);
      __ Bind(&done);
      return true;
737
    }
738 739 740
    case StandardCharacterSet::kEverything:
      // Match any character.
      return true;
741 742 743
  }
}

744
void RegExpMacroAssemblerARM64::Fail() {
745 746 747 748
  __ Mov(w0, FAILURE);
  __ B(&exit_label_);
}

749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
void RegExpMacroAssemblerARM64::LoadRegExpStackPointerFromMemory(Register dst) {
  ExternalReference ref =
      ExternalReference::address_of_regexp_stack_stack_pointer(isolate());
  __ Mov(dst, ref);
  __ Ldr(dst, MemOperand(dst));
}

void RegExpMacroAssemblerARM64::StoreRegExpStackPointerToMemory(
    Register src, Register scratch) {
  ExternalReference ref =
      ExternalReference::address_of_regexp_stack_stack_pointer(isolate());
  __ Mov(scratch, ref);
  __ Str(src, MemOperand(scratch));
}

764 765
void RegExpMacroAssemblerARM64::PushRegExpBasePointer(Register stack_pointer,
                                                      Register scratch) {
766 767
  ExternalReference ref =
      ExternalReference::address_of_regexp_stack_memory_top_address(isolate());
768 769 770 771
  __ Mov(scratch, ref);
  __ Ldr(scratch, MemOperand(scratch));
  __ Sub(scratch, stack_pointer, scratch);
  __ Str(scratch, MemOperand(frame_pointer(), kRegExpStackBasePointer));
772 773
}

774 775
void RegExpMacroAssemblerARM64::PopRegExpBasePointer(Register stack_pointer_out,
                                                     Register scratch) {
776 777
  ExternalReference ref =
      ExternalReference::address_of_regexp_stack_memory_top_address(isolate());
778 779 780 781 782 783
  __ Ldr(stack_pointer_out,
         MemOperand(frame_pointer(), kRegExpStackBasePointer));
  __ Mov(scratch, ref);
  __ Ldr(scratch, MemOperand(scratch));
  __ Add(stack_pointer_out, stack_pointer_out, scratch);
  StoreRegExpStackPointerToMemory(stack_pointer_out, scratch);
784
}
785

786
Handle<HeapObject> RegExpMacroAssemblerARM64::GetCode(Handle<String> source) {
787 788 789 790 791 792 793 794
  Label return_w0;
  // Finalize code - write the entry point code now we know how many
  // registers we need.

  // Entry code:
  __ Bind(&entry_label_);

  // Arguments on entry:
795
  // x0:  String   input
796 797 798 799 800
  // x1:  int      start_offset
  // x2:  byte*    input_start
  // x3:  byte*    input_end
  // x4:  int*     output array
  // x5:  int      output array size
801 802 803 804
  // x6:  int      direct_call
  // x7:  Isolate* isolate
  //
  // sp[0]:  secondary link/return address used by native call
805 806 807

  // Tell the system that we have a stack frame.  Because the type is MANUAL, no
  // code is generated.
808
  FrameScope scope(masm_.get(), StackFrame::MANUAL);
809 810 811 812 813

  // Push registers on the stack, only push the argument registers that we need.
  CPURegList argument_registers(x0, x5, x6, x7);

  CPURegList registers_to_retain = kCalleeSaved;
814
  DCHECK_EQ(registers_to_retain.Count(), kNumCalleeSavedRegisters);
815

816
  __ PushCPURegList(registers_to_retain);
817
  __ Push<TurboAssembler::kSignLR>(lr, fp);
818 819 820
  __ PushCPURegList(argument_registers);

  // Set frame pointer in place.
821
  __ Add(frame_pointer(), sp, argument_registers.Count() * kSystemPointerSize);
822 823 824 825 826 827 828 829

  // Initialize callee-saved registers.
  __ Mov(start_offset(), w1);
  __ Mov(input_start(), x2);
  __ Mov(input_end(), x3);
  __ Mov(output_array(), x4);

  // Make sure the stack alignment will be respected.
830
  const int alignment = masm_->ActivationFrameAlignment();
831
  DCHECK_EQ(alignment % 16, 0);
832 833 834 835 836 837 838 839
  const int align_mask = (alignment / kWRegSize) - 1;

  // Make room for stack locals.
  static constexpr int kWRegPerXReg = kXRegSize / kWRegSize;
  DCHECK_EQ(kNumberOfStackLocals * kWRegPerXReg,
            ((kNumberOfStackLocals * kWRegPerXReg) + align_mask) & ~align_mask);
  __ Claim(kNumberOfStackLocals * kWRegPerXReg);

840 841
  // Initialize backtrack stack pointer. It must not be clobbered from here on.
  // Note the backtrack_stackpointer is callee-saved.
842
  static_assert(backtrack_stackpointer() == x23);
843 844
  LoadRegExpStackPointerFromMemory(backtrack_stackpointer());

845 846
  // Store the regexp base pointer - we'll later restore it / write it to
  // memory when returning from this irregexp code object.
847
  PushRegExpBasePointer(backtrack_stackpointer(), x11);
848 849 850 851 852 853 854

  // Set the number of registers we will need to allocate, that is:
  //   - (num_registers_ - kNumCachedRegisters) (W registers)
  const int num_stack_registers =
      std::max(0, num_registers_ - kNumCachedRegisters);
  const int num_wreg_to_allocate =
      (num_stack_registers + align_mask) & ~align_mask;
855

856 857 858
  {
    // Check if we have space on the stack.
    Label stack_limit_hit, stack_ok;
859

860 861 862 863 864
    ExternalReference stack_limit =
        ExternalReference::address_of_jslimit(isolate());
    __ Mov(x10, stack_limit);
    __ Ldr(x10, MemOperand(x10));
    __ Subs(x10, sp, x10);
865

866 867
    // Handle it if the stack pointer is already below the stack limit.
    __ B(ls, &stack_limit_hit);
868

869 870 871 872
    // Check if there is room for the variable number of registers above
    // the stack limit.
    __ Cmp(x10, num_wreg_to_allocate * kWRegSize);
    __ B(hs, &stack_ok);
873

874 875 876 877
    // Exit with OutOfMemory exception. There is not enough space on the stack
    // for our working registers.
    __ Mov(w0, EXCEPTION);
    __ B(&return_w0);
878

879 880 881 882
    __ Bind(&stack_limit_hit);
    CallCheckStackGuardState(x10);
    // If returned value is non-zero, we exit with the returned value as result.
    __ Cbnz(w0, &return_w0);
883

884 885
    __ Bind(&stack_ok);
  }
886 887

  // Allocate space on stack.
888
  __ Claim(num_wreg_to_allocate, kWRegSize);
889

890
  // Initialize success_counter and kBacktrackCount with 0.
891
  __ Str(wzr, MemOperand(frame_pointer(), kSuccessCounter));
892
  __ Str(wzr, MemOperand(frame_pointer(), kBacktrackCount));
893 894 895

  // Find negative length (offset of start relative to end).
  __ Sub(x10, input_start(), input_end());
896
  if (FLAG_debug_code) {
897
    // Check that the size of the input string chars is in range.
898
    __ Neg(x11, x10);
899
    __ Cmp(x11, SeqTwoByteString::kMaxCharsSize);
900
    __ Check(ls, AbortReason::kInputStringTooLong);
901 902 903 904 905 906
  }
  __ Mov(current_input_offset(), w10);

  // The non-position value is used as a clearing value for the
  // capture registers, it corresponds to the position of the first character
  // minus one.
907 908
  __ Sub(string_start_minus_one(), current_input_offset(), char_size());
  __ Sub(string_start_minus_one(), string_start_minus_one(),
909 910 911
         Operand(start_offset(), LSL, (mode_ == UC16) ? 1 : 0));
  // We can store this value twice in an X register for initializing
  // on-stack registers later.
912 913
  __ Orr(twice_non_position_value(), string_start_minus_one().X(),
         Operand(string_start_minus_one().X(), LSL, kWRegSizeInBits));
914 915 916 917

  // Initialize code pointer register.
  __ Mov(code_pointer(), Operand(masm_->CodeObject()));

918 919 920 921 922 923 924 925 926 927 928 929 930 931 932
  Label load_char_start_regexp;
  {
    Label start_regexp;
    // Load newline if index is at start, previous character otherwise.
    __ Cbnz(start_offset(), &load_char_start_regexp);
    __ Mov(current_character(), '\n');
    __ B(&start_regexp);

    // Global regexp restarts matching here.
    __ Bind(&load_char_start_regexp);
    // Load previous char as initial value of current character register.
    LoadCurrentCharacterUnchecked(-1, 1);
    __ Bind(&start_regexp);
  }

933 934 935 936 937
  // Initialize on-stack registers.
  if (num_saved_registers_ > 0) {
    ClearRegisters(0, num_saved_registers_ - 1);
  }

938
  // Execute.
939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961
  __ B(&start_label_);

  if (backtrack_label_.is_linked()) {
    __ Bind(&backtrack_label_);
    Backtrack();
  }

  if (success_label_.is_linked()) {
    Register first_capture_start = w15;

    // Save captures when successful.
    __ Bind(&success_label_);

    if (num_saved_registers_ > 0) {
      // V8 expects the output to be an int32_t array.
      Register capture_start = w12;
      Register capture_end = w13;
      Register input_length = w14;

      // Copy captures to output.

      // Get string length.
      __ Sub(x10, input_end(), input_start());
962
      if (FLAG_debug_code) {
963 964
        // Check that the size of the input string chars is in range.
        __ Cmp(x10, SeqTwoByteString::kMaxCharsSize);
965
        __ Check(ls, AbortReason::kInputStringTooLong);
966 967 968 969 970 971 972 973 974 975 976 977 978 979
      }
      // input_start has a start_offset offset on entry. We need to include
      // it when computing the length of the whole string.
      if (mode_ == UC16) {
        __ Add(input_length, start_offset(), Operand(w10, LSR, 1));
      } else {
        __ Add(input_length, start_offset(), w10);
      }

      // Copy the results to the output array from the cached registers first.
      for (int i = 0;
           (i < num_saved_registers_) && (i < kNumCachedRegisters);
           i += 2) {
        __ Mov(capture_start.X(), GetCachedRegister(i));
980
        __ Lsr(capture_end.X(), capture_start.X(), kWRegSizeInBits);
981 982 983 984 985 986 987 988 989 990 991 992 993
        if ((i == 0) && global_with_zero_length_check()) {
          // Keep capture start for the zero-length check later.
          __ Mov(first_capture_start, capture_start);
        }
        // Offsets need to be relative to the start of the string.
        if (mode_ == UC16) {
          __ Add(capture_start, input_length, Operand(capture_start, ASR, 1));
          __ Add(capture_end, input_length, Operand(capture_end, ASR, 1));
        } else {
          __ Add(capture_start, input_length, capture_start);
          __ Add(capture_end, input_length, capture_end);
        }
        // The output pointer advances for a possible global match.
994 995
        __ Stp(capture_start, capture_end,
               MemOperand(output_array(), kSystemPointerSize, PostIndex));
996 997 998 999 1000 1001 1002 1003 1004 1005
      }

      // Only carry on if there are more than kNumCachedRegisters capture
      // registers.
      int num_registers_left_on_stack =
          num_saved_registers_ - kNumCachedRegisters;
      if (num_registers_left_on_stack > 0) {
        Register base = x10;
        // There are always an even number of capture registers. A couple of
        // registers determine one match with two offsets.
1006
        DCHECK_EQ(0, num_registers_left_on_stack % 2);
1007 1008 1009 1010
        __ Add(base, frame_pointer(), kFirstCaptureOnStack);

        // We can unroll the loop here, we should not unroll for less than 2
        // registers.
1011
        static_assert(kNumRegistersToUnroll > 2);
1012 1013
        if (num_registers_left_on_stack <= kNumRegistersToUnroll) {
          for (int i = 0; i < num_registers_left_on_stack / 2; i++) {
1014 1015
            __ Ldp(capture_end, capture_start,
                   MemOperand(base, -kSystemPointerSize, PostIndex));
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
            if ((i == 0) && global_with_zero_length_check()) {
              // Keep capture start for the zero-length check later.
              __ Mov(first_capture_start, capture_start);
            }
            // Offsets need to be relative to the start of the string.
            if (mode_ == UC16) {
              __ Add(capture_start,
                     input_length,
                     Operand(capture_start, ASR, 1));
              __ Add(capture_end, input_length, Operand(capture_end, ASR, 1));
            } else {
              __ Add(capture_start, input_length, capture_start);
              __ Add(capture_end, input_length, capture_end);
            }
            // The output pointer advances for a possible global match.
1031 1032
            __ Stp(capture_start, capture_end,
                   MemOperand(output_array(), kSystemPointerSize, PostIndex));
1033 1034 1035 1036 1037
          }
        } else {
          Label loop, start;
          __ Mov(x11, num_registers_left_on_stack);

1038 1039
          __ Ldp(capture_end, capture_start,
                 MemOperand(base, -kSystemPointerSize, PostIndex));
1040 1041 1042 1043 1044 1045
          if (global_with_zero_length_check()) {
            __ Mov(first_capture_start, capture_start);
          }
          __ B(&start);

          __ Bind(&loop);
1046 1047
          __ Ldp(capture_end, capture_start,
                 MemOperand(base, -kSystemPointerSize, PostIndex));
1048 1049 1050 1051 1052 1053 1054 1055 1056
          __ Bind(&start);
          if (mode_ == UC16) {
            __ Add(capture_start, input_length, Operand(capture_start, ASR, 1));
            __ Add(capture_end, input_length, Operand(capture_end, ASR, 1));
          } else {
            __ Add(capture_start, input_length, capture_start);
            __ Add(capture_end, input_length, capture_end);
          }
          // The output pointer advances for a possible global match.
1057 1058
          __ Stp(capture_start, capture_end,
                 MemOperand(output_array(), kSystemPointerSize, PostIndex));
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
          __ Sub(x11, x11, 2);
          __ Cbnz(x11, &loop);
        }
      }
    }

    if (global()) {
      Register success_counter = w0;
      Register output_size = x10;
      // Restart matching if the regular expression is flagged as global.

      // Increment success counter.
      __ Ldr(success_counter, MemOperand(frame_pointer(), kSuccessCounter));
      __ Add(success_counter, success_counter, 1);
      __ Str(success_counter, MemOperand(frame_pointer(), kSuccessCounter));

      // Capture results have been stored, so the number of remaining global
      // output registers is reduced by the number of stored captures.
      __ Ldr(output_size, MemOperand(frame_pointer(), kOutputSize));
      __ Sub(output_size, output_size, num_saved_registers_);
      // Check whether we have enough room for another set of capture results.
      __ Cmp(output_size, num_saved_registers_);
      __ B(lt, &return_w0);

      // The output pointer is already set to the next field in the output
      // array.
      // Update output size on the frame before we restart matching.
      __ Str(output_size, MemOperand(frame_pointer(), kOutputSize));

1088 1089 1090 1091
      // Restore the original regexp stack pointer value (effectively, pop the
      // stored base pointer).
      PopRegExpBasePointer(backtrack_stackpointer(), x11);

1092 1093 1094 1095 1096 1097 1098 1099
      if (global_with_zero_length_check()) {
        // Special case for zero-length matches.
        __ Cmp(current_input_offset(), first_capture_start);
        // Not a zero-length match, restart.
        __ B(ne, &load_char_start_regexp);
        // Offset from the end is zero if we already reached the end.
        __ Cbz(current_input_offset(), &return_w0);
        // Advance current position after a zero-length match.
1100 1101
        Label advance;
        __ bind(&advance);
1102
        __ Add(current_input_offset(), current_input_offset(),
1103
               Operand((mode_ == UC16) ? 2 : 1));
1104
        if (global_unicode()) CheckNotInSurrogatePair(0, &advance);
1105 1106 1107 1108 1109 1110 1111 1112 1113
      }

      __ B(&load_char_start_regexp);
    } else {
      __ Mov(w0, SUCCESS);
    }
  }

  if (exit_label_.is_linked()) {
1114
    // Exit and return w0.
1115 1116 1117 1118 1119 1120 1121
    __ Bind(&exit_label_);
    if (global()) {
      __ Ldr(w0, MemOperand(frame_pointer(), kSuccessCounter));
    }
  }

  __ Bind(&return_w0);
1122 1123
  // Restore the original regexp stack pointer value (effectively, pop the
  // stored base pointer).
1124
  PopRegExpBasePointer(backtrack_stackpointer(), x11);
1125

1126
  // Set stack pointer back to first register to retain.
1127
  __ Mov(sp, fp);
1128
  __ Pop<TurboAssembler::kAuthLR>(fp, lr);
1129 1130

  // Restore registers.
1131
  __ PopCPURegList(registers_to_retain);
1132 1133 1134 1135 1136 1137

  __ Ret();

  Label exit_with_exception;
  if (check_preempt_label_.is_linked()) {
    __ Bind(&check_preempt_label_);
1138 1139 1140

    StoreRegExpStackPointerToMemory(backtrack_stackpointer(), x10);

1141
    SaveLinkRegister();
1142
    PushCachedRegisters();
1143
    CallCheckStackGuardState(x10);
1144
    // Returning from the regexp code restores the stack (sp <- fp)
1145 1146 1147
    // so we don't need to drop the link register from it before exiting.
    __ Cbnz(w0, &return_w0);
    // Reset the cached registers.
1148
    PopCachedRegisters();
1149 1150 1151

    LoadRegExpStackPointerFromMemory(backtrack_stackpointer());

1152 1153 1154 1155 1156 1157
    RestoreLinkRegister();
    __ Ret();
  }

  if (stack_overflow_label_.is_linked()) {
    __ Bind(&stack_overflow_label_);
1158 1159 1160

    StoreRegExpStackPointerToMemory(backtrack_stackpointer(), x10);

1161
    SaveLinkRegister();
1162 1163
    PushCachedRegisters();
    // Call GrowStack(isolate).
1164 1165
    static constexpr int kNumArguments = 1;
    __ Mov(x0, ExternalReference::isolate_address(isolate()));
1166
    __ CallCFunction(ExternalReference::re_grow_stack(), kNumArguments);
1167 1168 1169 1170
    // If return nullptr, we have failed to grow the stack, and must exit with
    // a stack-overflow exception.  Returning from the regexp code restores the
    // stack (sp <- fp) so we don't need to drop the link register from it
    // before exiting.
1171 1172 1173
    __ Cbz(w0, &exit_with_exception);
    // Otherwise use return value as new stack pointer.
    __ Mov(backtrack_stackpointer(), x0);
1174
    PopCachedRegisters();
1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
    RestoreLinkRegister();
    __ Ret();
  }

  if (exit_with_exception.is_linked()) {
    __ Bind(&exit_with_exception);
    __ Mov(w0, EXCEPTION);
    __ B(&return_w0);
  }

1185 1186 1187 1188 1189 1190
  if (fallback_label_.is_linked()) {
    __ Bind(&fallback_label_);
    __ Mov(w0, FALLBACK_TO_EXPERIMENTAL);
    __ B(&return_w0);
  }

1191
  CodeDesc code_desc;
1192
  masm_->GetCode(isolate(), &code_desc);
1193 1194 1195 1196
  Handle<Code> code =
      Factory::CodeBuilder(isolate(), code_desc, CodeKind::REGEXP)
          .set_self_reference(masm_->CodeObject())
          .Build();
1197
  PROFILE(masm_->isolate(),
1198
          RegExpCodeCreateEvent(Handle<AbstractCode>::cast(code), source));
1199 1200 1201 1202
  return Handle<HeapObject>::cast(code);
}


1203
void RegExpMacroAssemblerARM64::GoTo(Label* to) {
1204 1205 1206
  BranchOrBacktrack(al, to);
}

1207 1208
void RegExpMacroAssemblerARM64::IfRegisterGE(int reg, int comparand,
                                             Label* if_ge) {
1209 1210 1211 1212 1213
  Register to_compare = GetRegister(reg, w10);
  CompareAndBranchOrBacktrack(to_compare, comparand, ge, if_ge);
}


1214 1215
void RegExpMacroAssemblerARM64::IfRegisterLT(int reg, int comparand,
                                             Label* if_lt) {
1216 1217 1218 1219 1220
  Register to_compare = GetRegister(reg, w10);
  CompareAndBranchOrBacktrack(to_compare, comparand, lt, if_lt);
}


1221
void RegExpMacroAssemblerARM64::IfRegisterEqPos(int reg, Label* if_eq) {
1222 1223 1224 1225 1226 1227
  Register to_compare = GetRegister(reg, w10);
  __ Cmp(to_compare, current_input_offset());
  BranchOrBacktrack(eq, if_eq);
}

RegExpMacroAssembler::IrregexpImplementation
1228 1229
    RegExpMacroAssemblerARM64::Implementation() {
  return kARM64Implementation;
1230 1231 1232
}


1233
void RegExpMacroAssemblerARM64::PopCurrentPosition() {
1234 1235 1236 1237
  Pop(current_input_offset());
}


1238
void RegExpMacroAssemblerARM64::PopRegister(int register_index) {
1239 1240 1241 1242 1243
  Pop(w10);
  StoreRegister(register_index, w10);
}


1244
void RegExpMacroAssemblerARM64::PushBacktrack(Label* label) {
1245 1246 1247 1248
  if (label->is_bound()) {
    int target = label->pos();
    __ Mov(w10, target + Code::kHeaderSize - kHeapObjectTag);
  } else {
1249
    __ Adr(x10, label, MacroAssembler::kAdrFar);
1250
    __ Sub(x10, x10, code_pointer());
1251
    if (FLAG_debug_code) {
1252 1253
      __ Cmp(x10, kWRegMask);
      // The code offset has to fit in a W register.
1254
      __ Check(ls, AbortReason::kOffsetOutOfRange);
1255 1256 1257 1258 1259 1260 1261
    }
  }
  Push(w10);
  CheckStackLimit();
}


1262
void RegExpMacroAssemblerARM64::PushCurrentPosition() {
1263 1264 1265 1266
  Push(current_input_offset());
}


1267 1268
void RegExpMacroAssemblerARM64::PushRegister(int register_index,
                                             StackCheckFlag check_stack_limit) {
1269 1270 1271 1272 1273 1274
  Register to_push = GetRegister(register_index, w10);
  Push(to_push);
  if (check_stack_limit) CheckStackLimit();
}


1275
void RegExpMacroAssemblerARM64::ReadCurrentPositionFromRegister(int reg) {
1276 1277 1278 1279 1280 1281
  RegisterState register_state = GetRegisterState(reg);
  switch (register_state) {
    case STACKED:
      __ Ldr(current_input_offset(), register_location(reg));
      break;
    case CACHED_LSW:
1282
      __ Mov(current_input_offset(), GetCachedRegister(reg).W());
1283 1284
      break;
    case CACHED_MSW:
1285 1286
      __ Lsr(current_input_offset().X(), GetCachedRegister(reg),
             kWRegSizeInBits);
1287 1288 1289 1290 1291 1292
      break;
    default:
      UNREACHABLE();
  }
}

1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305
void RegExpMacroAssemblerARM64::WriteStackPointerToRegister(int reg) {
  ExternalReference ref =
      ExternalReference::address_of_regexp_stack_memory_top_address(isolate());
  __ Mov(x10, ref);
  __ Ldr(x10, MemOperand(x10));
  __ Sub(x10, backtrack_stackpointer(), x10);
  if (FLAG_debug_code) {
    __ Cmp(x10, Operand(w10, SXTW));
    // The stack offset needs to fit in a W register.
    __ Check(eq, AbortReason::kOffsetOutOfRange);
  }
  StoreRegister(reg, w10);
}
1306

1307
void RegExpMacroAssemblerARM64::ReadStackPointerFromRegister(int reg) {
1308 1309
  ExternalReference ref =
      ExternalReference::address_of_regexp_stack_memory_top_address(isolate());
1310
  Register read_from = GetRegister(reg, w10);
1311 1312
  __ Mov(x11, ref);
  __ Ldr(x11, MemOperand(x11));
1313 1314 1315
  __ Add(backtrack_stackpointer(), x11, Operand(read_from, SXTW));
}

1316
void RegExpMacroAssemblerARM64::SetCurrentPositionFromEnd(int by) {
1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328
  Label after_position;
  __ Cmp(current_input_offset(), -by * char_size());
  __ B(ge, &after_position);
  __ Mov(current_input_offset(), -by * char_size());
  // On RegExp code entry (where this operation is used), the character before
  // the current position is expected to be already loaded.
  // We have advanced the position, so it's safe to read backwards.
  LoadCurrentCharacterUnchecked(-1, 1);
  __ Bind(&after_position);
}


1329
void RegExpMacroAssemblerARM64::SetRegister(int register_index, int to) {
1330
  DCHECK(register_index >= num_saved_registers_);  // Reserved for positions!
1331 1332 1333 1334 1335 1336 1337 1338 1339
  Register set_to = wzr;
  if (to != 0) {
    set_to = w10;
    __ Mov(set_to, to);
  }
  StoreRegister(register_index, set_to);
}


1340
bool RegExpMacroAssemblerARM64::Succeed() {
1341 1342 1343 1344 1345
  __ B(&success_label_);
  return global();
}


1346 1347
void RegExpMacroAssemblerARM64::WriteCurrentPositionToRegister(int reg,
                                                               int cp_offset) {
1348 1349 1350 1351 1352 1353 1354 1355 1356
  Register position = current_input_offset();
  if (cp_offset != 0) {
    position = w10;
    __ Add(position, current_input_offset(), cp_offset * char_size());
  }
  StoreRegister(reg, position);
}


1357
void RegExpMacroAssemblerARM64::ClearRegisters(int reg_from, int reg_to) {
1358
  DCHECK(reg_from <= reg_to);
1359 1360 1361 1362 1363
  int num_registers = reg_to - reg_from + 1;

  // If the first capture register is cached in a hardware register but not
  // aligned on a 64-bit one, we need to clear the first one specifically.
  if ((reg_from < kNumCachedRegisters) && ((reg_from % 2) != 0)) {
1364
    StoreRegister(reg_from, string_start_minus_one());
1365 1366 1367 1368 1369 1370
    num_registers--;
    reg_from++;
  }

  // Clear cached registers in pairs as far as possible.
  while ((num_registers >= 2) && (reg_from < kNumCachedRegisters)) {
1371
    DCHECK(GetRegisterState(reg_from) == CACHED_LSW);
1372 1373 1374 1375 1376 1377
    __ Mov(GetCachedRegister(reg_from), twice_non_position_value());
    reg_from += 2;
    num_registers -= 2;
  }

  if ((num_registers % 2) == 1) {
1378
    StoreRegister(reg_from, string_start_minus_one());
1379 1380 1381 1382 1383 1384
    num_registers--;
    reg_from++;
  }

  if (num_registers > 0) {
    // If there are some remaining registers, they are stored on the stack.
1385
    DCHECK_LE(kNumCachedRegisters, reg_from);
1386 1387 1388 1389 1390 1391

    // Move down the indexes of the registers on stack to get the correct offset
    // in memory.
    reg_from -= kNumCachedRegisters;
    reg_to -= kNumCachedRegisters;
    // We should not unroll the loop for less than 2 registers.
1392
    static_assert(kNumRegistersToUnroll > 2);
1393 1394
    // We position the base pointer to (reg_from + 1).
    int base_offset = kFirstRegisterOnStack -
1395
        kWRegSize - (kWRegSize * reg_from);
1396 1397 1398 1399 1400 1401 1402 1403
    if (num_registers > kNumRegistersToUnroll) {
      Register base = x10;
      __ Add(base, frame_pointer(), base_offset);

      Label loop;
      __ Mov(x11, num_registers);
      __ Bind(&loop);
      __ Str(twice_non_position_value(),
1404
             MemOperand(base, -kSystemPointerSize, PostIndex));
1405 1406 1407 1408 1409 1410
      __ Sub(x11, x11, 2);
      __ Cbnz(x11, &loop);
    } else {
      for (int i = reg_from; i <= reg_to; i += 2) {
        __ Str(twice_non_position_value(),
               MemOperand(frame_pointer(), base_offset));
1411
        base_offset -= kWRegSize * 2;
1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423
      }
    }
  }
}

// Helper function for reading a value out of a stack frame.
template <typename T>
static T& frame_entry(Address re_frame, int frame_offset) {
  return *reinterpret_cast<T*>(re_frame + frame_offset);
}


1424 1425 1426 1427
template <typename T>
static T* frame_entry_address(Address re_frame, int frame_offset) {
  return reinterpret_cast<T*>(re_frame + frame_offset);
}
1428

1429
int RegExpMacroAssemblerARM64::CheckStackGuardState(
1430 1431
    Address* return_address, Address raw_code, Address re_frame,
    int start_index, const byte** input_start, const byte** input_end) {
1432
  Code re_code = Code::cast(Object(raw_code));
1433 1434
  return NativeRegExpMacroAssembler::CheckStackGuardState(
      frame_entry<Isolate*>(re_frame, kIsolate), start_index,
1435 1436 1437
      static_cast<RegExp::CallOrigin>(frame_entry<int>(re_frame, kDirectCall)),
      return_address, re_code, frame_entry_address<Address>(re_frame, kInput),
      input_start, input_end);
1438 1439 1440
}


1441 1442
void RegExpMacroAssemblerARM64::CheckPosition(int cp_offset,
                                              Label* on_outside_input) {
1443 1444 1445 1446 1447 1448 1449 1450
  if (cp_offset >= 0) {
    CompareAndBranchOrBacktrack(current_input_offset(),
                                -cp_offset * char_size(), ge, on_outside_input);
  } else {
    __ Add(w12, current_input_offset(), Operand(cp_offset * char_size()));
    __ Cmp(w12, string_start_minus_one());
    BranchOrBacktrack(le, on_outside_input);
  }
1451 1452 1453 1454 1455
}


// Private methods:

1456
void RegExpMacroAssemblerARM64::CallCheckStackGuardState(Register scratch) {
1457
  DCHECK(!isolate()->IsGeneratingEmbeddedBuiltins());
1458 1459
  DCHECK(!masm_->options().isolate_independent_code);

1460 1461 1462 1463 1464
  // Allocate space on the stack to store the return address. The
  // CheckStackGuardState C++ function will override it if the code
  // moved. Allocate extra space for 2 arguments passed by pointers.
  // AAPCS64 requires the stack to be 16 byte aligned.
  int alignment = masm_->ActivationFrameAlignment();
1465
  DCHECK_EQ(alignment % 16, 0);
1466
  int align_mask = (alignment / kXRegSize) - 1;
1467 1468 1469 1470 1471
  int xreg_to_claim = (3 + align_mask) & ~align_mask;

  __ Claim(xreg_to_claim);

  // CheckStackGuardState needs the end and start addresses of the input string.
1472 1473 1474 1475
  __ Poke(input_end(), 2 * kSystemPointerSize);
  __ Add(x5, sp, 2 * kSystemPointerSize);
  __ Poke(input_start(), kSystemPointerSize);
  __ Add(x4, sp, kSystemPointerSize);
1476 1477 1478 1479

  __ Mov(w3, start_offset());
  // RegExp code frame pointer.
  __ Mov(x2, frame_pointer());
1480
  // Code of self.
1481 1482 1483
  __ Mov(x1, Operand(masm_->CodeObject()));

  // We need to pass a pointer to the return address as first argument.
1484 1485
  // DirectCEntry will place the return address on the stack before calling so
  // the stack pointer will point to it.
1486
  __ Mov(x0, sp);
1487

1488
  DCHECK_EQ(scratch, x10);
1489
  ExternalReference check_stack_guard_state =
1490
      ExternalReference::re_check_stack_guard_state();
1491
  __ Mov(scratch, check_stack_guard_state);
1492

1493
  __ CallBuiltin(Builtin::kDirectCEntry);
1494 1495

  // The input string may have been moved in memory, we need to reload it.
1496 1497
  __ Peek(input_start(), kSystemPointerSize);
  __ Peek(input_end(), 2 * kSystemPointerSize);
1498 1499 1500 1501 1502 1503 1504

  __ Drop(xreg_to_claim);

  // Reload the Code pointer.
  __ Mov(code_pointer(), Operand(masm_->CodeObject()));
}

1505 1506
void RegExpMacroAssemblerARM64::BranchOrBacktrack(Condition condition,
                                                  Label* to) {
1507
  if (condition == al) {  // Unconditional.
1508
    if (to == nullptr) {
1509 1510 1511 1512 1513 1514
      Backtrack();
      return;
    }
    __ B(to);
    return;
  }
1515
  if (to == nullptr) {
1516 1517
    to = &backtrack_label_;
  }
1518
  __ B(condition, to);
1519 1520
}

1521 1522 1523 1524
void RegExpMacroAssemblerARM64::CompareAndBranchOrBacktrack(Register reg,
                                                            int immediate,
                                                            Condition condition,
                                                            Label* to) {
1525
  if ((immediate == 0) && ((condition == eq) || (condition == ne))) {
1526
    if (to == nullptr) {
1527 1528 1529
      to = &backtrack_label_;
    }
    if (condition == eq) {
1530
      __ Cbz(reg, to);
1531
    } else {
1532
      __ Cbnz(reg, to);
1533 1534 1535 1536 1537 1538 1539 1540
    }
  } else {
    __ Cmp(reg, immediate);
    BranchOrBacktrack(condition, to);
  }
}


1541
void RegExpMacroAssemblerARM64::CheckPreemption() {
1542 1543
  // Check for preemption.
  ExternalReference stack_limit =
1544
      ExternalReference::address_of_jslimit(isolate());
1545
  __ Mov(x10, stack_limit);
1546
  __ Ldr(x10, MemOperand(x10));
1547
  __ Cmp(sp, x10);
1548 1549 1550 1551
  CallIf(&check_preempt_label_, ls);
}


1552
void RegExpMacroAssemblerARM64::CheckStackLimit() {
1553
  ExternalReference stack_limit =
1554
      ExternalReference::address_of_regexp_stack_limit_address(isolate());
1555
  __ Mov(x10, stack_limit);
1556 1557 1558 1559 1560 1561
  __ Ldr(x10, MemOperand(x10));
  __ Cmp(backtrack_stackpointer(), x10);
  CallIf(&stack_overflow_label_, ls);
}


1562
void RegExpMacroAssemblerARM64::Push(Register source) {
1563
  DCHECK(source.Is32Bits());
1564
  DCHECK_NE(source, backtrack_stackpointer());
1565 1566
  __ Str(source,
         MemOperand(backtrack_stackpointer(),
1567
                    -static_cast<int>(kWRegSize),
1568 1569 1570 1571
                    PreIndex));
}


1572
void RegExpMacroAssemblerARM64::Pop(Register target) {
1573
  DCHECK(target.Is32Bits());
1574
  DCHECK_NE(target, backtrack_stackpointer());
1575
  __ Ldr(target,
1576
         MemOperand(backtrack_stackpointer(), kWRegSize, PostIndex));
1577 1578 1579
}


1580
Register RegExpMacroAssemblerARM64::GetCachedRegister(int register_index) {
1581
  DCHECK_GT(kNumCachedRegisters, register_index);
1582
  return Register::Create(register_index / 2, kXRegSizeInBits);
1583 1584 1585
}


1586 1587
Register RegExpMacroAssemblerARM64::GetRegister(int register_index,
                                                Register maybe_result) {
1588
  DCHECK(maybe_result.Is32Bits());
1589
  DCHECK_LE(0, register_index);
1590 1591 1592
  if (num_registers_ <= register_index) {
    num_registers_ = register_index + 1;
  }
1593
  Register result = NoReg;
1594 1595 1596 1597 1598 1599 1600 1601 1602 1603
  RegisterState register_state = GetRegisterState(register_index);
  switch (register_state) {
    case STACKED:
      __ Ldr(maybe_result, register_location(register_index));
      result = maybe_result;
      break;
    case CACHED_LSW:
      result = GetCachedRegister(register_index).W();
      break;
    case CACHED_MSW:
1604 1605
      __ Lsr(maybe_result.X(), GetCachedRegister(register_index),
             kWRegSizeInBits);
1606 1607 1608 1609 1610
      result = maybe_result;
      break;
    default:
      UNREACHABLE();
  }
1611
  DCHECK(result.Is32Bits());
1612 1613 1614 1615
  return result;
}


1616 1617
void RegExpMacroAssemblerARM64::StoreRegister(int register_index,
                                              Register source) {
1618
  DCHECK(source.Is32Bits());
1619
  DCHECK_LE(0, register_index);
1620 1621 1622 1623 1624 1625 1626 1627 1628
  if (num_registers_ <= register_index) {
    num_registers_ = register_index + 1;
  }

  RegisterState register_state = GetRegisterState(register_index);
  switch (register_state) {
    case STACKED:
      __ Str(source, register_location(register_index));
      break;
1629 1630
    case CACHED_LSW: {
      Register cached_register = GetCachedRegister(register_index);
1631
      if (source != cached_register.W()) {
1632
        __ Bfi(cached_register, source.X(), 0, kWRegSizeInBits);
1633 1634
      }
      break;
1635 1636 1637
    }
    case CACHED_MSW: {
      Register cached_register = GetCachedRegister(register_index);
1638
      __ Bfi(cached_register, source.X(), kWRegSizeInBits, kWRegSizeInBits);
1639
      break;
1640
    }
1641 1642 1643 1644 1645 1646
    default:
      UNREACHABLE();
  }
}


1647
void RegExpMacroAssemblerARM64::CallIf(Label* to, Condition condition) {
1648
  Label skip_call;
1649
  if (condition != al) __ B(&skip_call, NegateCondition(condition));
1650 1651 1652 1653 1654
  __ Bl(to);
  __ Bind(&skip_call);
}


1655
void RegExpMacroAssemblerARM64::RestoreLinkRegister() {
1656 1657
  // TODO(v8:10026): Remove when we stop compacting for code objects that are
  // active on the call stack.
1658
  __ Pop<TurboAssembler::kAuthLR>(padreg, lr);
1659 1660 1661 1662
  __ Add(lr, lr, Operand(masm_->CodeObject()));
}


1663
void RegExpMacroAssemblerARM64::SaveLinkRegister() {
1664
  __ Sub(lr, lr, Operand(masm_->CodeObject()));
1665
  __ Push<TurboAssembler::kSignLR>(lr, padreg);
1666 1667 1668
}


1669
MemOperand RegExpMacroAssemblerARM64::register_location(int register_index) {
1670
  DCHECK(register_index < (1<<30));
1671
  DCHECK_LE(kNumCachedRegisters, register_index);
1672 1673 1674 1675
  if (num_registers_ <= register_index) {
    num_registers_ = register_index + 1;
  }
  register_index -= kNumCachedRegisters;
1676
  int offset = kFirstRegisterOnStack - register_index * kWRegSize;
1677 1678 1679
  return MemOperand(frame_pointer(), offset);
}

1680
MemOperand RegExpMacroAssemblerARM64::capture_location(int register_index,
1681
                                                     Register scratch) {
1682 1683
  DCHECK(register_index < (1<<30));
  DCHECK(register_index < num_saved_registers_);
1684
  DCHECK_LE(kNumCachedRegisters, register_index);
1685
  DCHECK_EQ(register_index % 2, 0);
1686
  register_index -= kNumCachedRegisters;
1687
  int offset = kFirstCaptureOnStack - register_index * kWRegSize;
1688 1689 1690 1691 1692 1693 1694 1695 1696 1697
  // capture_location is used with Stp instructions to load/store 2 registers.
  // The immediate field in the encoding is limited to 7 bits (signed).
  if (is_int7(offset)) {
    return MemOperand(frame_pointer(), offset);
  } else {
    __ Add(scratch, frame_pointer(), offset);
    return MemOperand(scratch);
  }
}

1698 1699
void RegExpMacroAssemblerARM64::LoadCurrentCharacterUnchecked(int cp_offset,
                                                              int characters) {
1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710
  Register offset = current_input_offset();

  // The ldr, str, ldrh, strh instructions can do unaligned accesses, if the CPU
  // and the operating system running on the target allow it.
  // If unaligned load/stores are not supported then this function must only
  // be used to load a single character at a time.

  // ARMv8 supports unaligned accesses but V8 or the kernel can decide to
  // disable it.
  // TODO(pielan): See whether or not we should disable unaligned accesses.
  if (!CanReadUnaligned()) {
1711
    DCHECK_EQ(1, characters);
1712 1713 1714
  }

  if (cp_offset != 0) {
1715
    if (FLAG_debug_code) {
1716 1717 1718 1719
      __ Mov(x10, cp_offset * char_size());
      __ Add(x10, x10, Operand(current_input_offset(), SXTW));
      __ Cmp(x10, Operand(w10, SXTW));
      // The offset needs to fit in a W register.
1720
      __ Check(eq, AbortReason::kOffsetOutOfRange);
1721 1722 1723 1724 1725 1726
    } else {
      __ Add(w10, current_input_offset(), cp_offset * char_size());
    }
    offset = w10;
  }

1727
  if (mode_ == LATIN1) {
1728 1729 1730 1731 1732
    if (characters == 4) {
      __ Ldr(current_character(), MemOperand(input_end(), offset, SXTW));
    } else if (characters == 2) {
      __ Ldrh(current_character(), MemOperand(input_end(), offset, SXTW));
    } else {
1733
      DCHECK_EQ(1, characters);
1734 1735 1736
      __ Ldrb(current_character(), MemOperand(input_end(), offset, SXTW));
    }
  } else {
1737
    DCHECK(mode_ == UC16);
1738 1739 1740
    if (characters == 2) {
      __ Ldr(current_character(), MemOperand(input_end(), offset, SXTW));
    } else {
1741
      DCHECK_EQ(1, characters);
1742 1743 1744 1745 1746
      __ Ldrh(current_character(), MemOperand(input_end(), offset, SXTW));
    }
  }
}

1747 1748
}  // namespace internal
}  // namespace v8
1749

1750 1751
#undef __

1752
#endif  // V8_TARGET_ARCH_ARM64