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#ifndef V8_REGEXP_ARM_REGEXP_MACRO_ASSEMBLER_ARM_H_
61cb0ef41Sopenharmony_ci#define V8_REGEXP_ARM_REGEXP_MACRO_ASSEMBLER_ARM_H_
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci#include "src/codegen/macro-assembler.h"
91cb0ef41Sopenharmony_ci#include "src/regexp/regexp-macro-assembler.h"
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_cinamespace v8 {
121cb0ef41Sopenharmony_cinamespace internal {
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ciclass V8_EXPORT_PRIVATE RegExpMacroAssemblerARM
151cb0ef41Sopenharmony_ci    : public NativeRegExpMacroAssembler {
161cb0ef41Sopenharmony_ci public:
171cb0ef41Sopenharmony_ci  RegExpMacroAssemblerARM(Isolate* isolate, Zone* zone, Mode mode,
181cb0ef41Sopenharmony_ci                          int registers_to_save);
191cb0ef41Sopenharmony_ci  ~RegExpMacroAssemblerARM() override;
201cb0ef41Sopenharmony_ci  void AbortedCodeGeneration() override;
211cb0ef41Sopenharmony_ci  int stack_limit_slack() override;
221cb0ef41Sopenharmony_ci  void AdvanceCurrentPosition(int by) override;
231cb0ef41Sopenharmony_ci  void AdvanceRegister(int reg, int by) override;
241cb0ef41Sopenharmony_ci  void Backtrack() override;
251cb0ef41Sopenharmony_ci  void Bind(Label* label) override;
261cb0ef41Sopenharmony_ci  void CheckAtStart(int cp_offset, Label* on_at_start) override;
271cb0ef41Sopenharmony_ci  void CheckCharacter(unsigned c, Label* on_equal) override;
281cb0ef41Sopenharmony_ci  void CheckCharacterAfterAnd(unsigned c, unsigned mask,
291cb0ef41Sopenharmony_ci                              Label* on_equal) override;
301cb0ef41Sopenharmony_ci  void CheckCharacterGT(base::uc16 limit, Label* on_greater) override;
311cb0ef41Sopenharmony_ci  void CheckCharacterLT(base::uc16 limit, Label* on_less) override;
321cb0ef41Sopenharmony_ci  // A "greedy loop" is a loop that is both greedy and with a simple
331cb0ef41Sopenharmony_ci  // body. It has a particularly simple implementation.
341cb0ef41Sopenharmony_ci  void CheckGreedyLoop(Label* on_tos_equals_current_position) override;
351cb0ef41Sopenharmony_ci  void CheckNotAtStart(int cp_offset, Label* on_not_at_start) override;
361cb0ef41Sopenharmony_ci  void CheckNotBackReference(int start_reg, bool read_backward,
371cb0ef41Sopenharmony_ci                             Label* on_no_match) override;
381cb0ef41Sopenharmony_ci  void CheckNotBackReferenceIgnoreCase(int start_reg, bool read_backward,
391cb0ef41Sopenharmony_ci                                       bool unicode,
401cb0ef41Sopenharmony_ci                                       Label* on_no_match) override;
411cb0ef41Sopenharmony_ci  void CheckNotCharacter(unsigned c, Label* on_not_equal) override;
421cb0ef41Sopenharmony_ci  void CheckNotCharacterAfterAnd(unsigned c, unsigned mask,
431cb0ef41Sopenharmony_ci                                 Label* on_not_equal) override;
441cb0ef41Sopenharmony_ci  void CheckNotCharacterAfterMinusAnd(base::uc16 c, base::uc16 minus,
451cb0ef41Sopenharmony_ci                                      base::uc16 mask,
461cb0ef41Sopenharmony_ci                                      Label* on_not_equal) override;
471cb0ef41Sopenharmony_ci  void CheckCharacterInRange(base::uc16 from, base::uc16 to,
481cb0ef41Sopenharmony_ci                             Label* on_in_range) override;
491cb0ef41Sopenharmony_ci  void CheckCharacterNotInRange(base::uc16 from, base::uc16 to,
501cb0ef41Sopenharmony_ci                                Label* on_not_in_range) override;
511cb0ef41Sopenharmony_ci  bool CheckCharacterInRangeArray(const ZoneList<CharacterRange>* ranges,
521cb0ef41Sopenharmony_ci                                  Label* on_in_range) override;
531cb0ef41Sopenharmony_ci  bool CheckCharacterNotInRangeArray(const ZoneList<CharacterRange>* ranges,
541cb0ef41Sopenharmony_ci                                     Label* on_not_in_range) override;
551cb0ef41Sopenharmony_ci  void CheckBitInTable(Handle<ByteArray> table, Label* on_bit_set) override;
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci  // Checks whether the given offset from the current position is before
581cb0ef41Sopenharmony_ci  // the end of the string.
591cb0ef41Sopenharmony_ci  void CheckPosition(int cp_offset, Label* on_outside_input) override;
601cb0ef41Sopenharmony_ci  bool CheckSpecialCharacterClass(StandardCharacterSet type,
611cb0ef41Sopenharmony_ci                                  Label* on_no_match) override;
621cb0ef41Sopenharmony_ci  void Fail() override;
631cb0ef41Sopenharmony_ci  Handle<HeapObject> GetCode(Handle<String> source) override;
641cb0ef41Sopenharmony_ci  void GoTo(Label* label) override;
651cb0ef41Sopenharmony_ci  void IfRegisterGE(int reg, int comparand, Label* if_ge) override;
661cb0ef41Sopenharmony_ci  void IfRegisterLT(int reg, int comparand, Label* if_lt) override;
671cb0ef41Sopenharmony_ci  void IfRegisterEqPos(int reg, Label* if_eq) override;
681cb0ef41Sopenharmony_ci  IrregexpImplementation Implementation() override;
691cb0ef41Sopenharmony_ci  void LoadCurrentCharacterUnchecked(int cp_offset,
701cb0ef41Sopenharmony_ci                                     int character_count) override;
711cb0ef41Sopenharmony_ci  void PopCurrentPosition() override;
721cb0ef41Sopenharmony_ci  void PopRegister(int register_index) override;
731cb0ef41Sopenharmony_ci  void PushBacktrack(Label* label) override;
741cb0ef41Sopenharmony_ci  void PushCurrentPosition() override;
751cb0ef41Sopenharmony_ci  void PushRegister(int register_index,
761cb0ef41Sopenharmony_ci                    StackCheckFlag check_stack_limit) override;
771cb0ef41Sopenharmony_ci  void ReadCurrentPositionFromRegister(int reg) override;
781cb0ef41Sopenharmony_ci  void ReadStackPointerFromRegister(int reg) override;
791cb0ef41Sopenharmony_ci  void SetCurrentPositionFromEnd(int by) override;
801cb0ef41Sopenharmony_ci  void SetRegister(int register_index, int to) override;
811cb0ef41Sopenharmony_ci  bool Succeed() override;
821cb0ef41Sopenharmony_ci  void WriteCurrentPositionToRegister(int reg, int cp_offset) override;
831cb0ef41Sopenharmony_ci  void ClearRegisters(int reg_from, int reg_to) override;
841cb0ef41Sopenharmony_ci  void WriteStackPointerToRegister(int reg) override;
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_ci  // Called from RegExp if the stack-guard is triggered.
871cb0ef41Sopenharmony_ci  // If the code object is relocated, the return address is fixed before
881cb0ef41Sopenharmony_ci  // returning.
891cb0ef41Sopenharmony_ci  // {raw_code} is an Address because this is called via ExternalReference.
901cb0ef41Sopenharmony_ci  static int CheckStackGuardState(Address* return_address, Address raw_code,
911cb0ef41Sopenharmony_ci                                  Address re_frame);
921cb0ef41Sopenharmony_ci
931cb0ef41Sopenharmony_ci private:
941cb0ef41Sopenharmony_ci  // Offsets from frame_pointer() of function parameters and stored registers.
951cb0ef41Sopenharmony_ci  static const int kFramePointer = 0;
961cb0ef41Sopenharmony_ci
971cb0ef41Sopenharmony_ci  // Above the frame pointer - Stored registers and stack passed parameters.
981cb0ef41Sopenharmony_ci  static const int kStoredRegisters = kFramePointer;
991cb0ef41Sopenharmony_ci  // Return address (stored from link register, read into pc on return).
1001cb0ef41Sopenharmony_ci  static const int kReturnAddress = kStoredRegisters + 8 * kPointerSize;
1011cb0ef41Sopenharmony_ci  // Stack parameters placed by caller.
1021cb0ef41Sopenharmony_ci  static const int kRegisterOutput = kReturnAddress + kPointerSize;
1031cb0ef41Sopenharmony_ci  static const int kNumOutputRegisters = kRegisterOutput + kPointerSize;
1041cb0ef41Sopenharmony_ci  static const int kDirectCall = kNumOutputRegisters + kPointerSize;
1051cb0ef41Sopenharmony_ci  static const int kIsolate = kDirectCall + kPointerSize;
1061cb0ef41Sopenharmony_ci
1071cb0ef41Sopenharmony_ci  // Below the frame pointer.
1081cb0ef41Sopenharmony_ci  // Register parameters stored by setup code.
1091cb0ef41Sopenharmony_ci  static const int kInputEnd = kFramePointer - kPointerSize;
1101cb0ef41Sopenharmony_ci  static const int kInputStart = kInputEnd - kPointerSize;
1111cb0ef41Sopenharmony_ci  static const int kStartIndex = kInputStart - kPointerSize;
1121cb0ef41Sopenharmony_ci  static const int kInputString = kStartIndex - kPointerSize;
1131cb0ef41Sopenharmony_ci  // When adding local variables remember to push space for them in
1141cb0ef41Sopenharmony_ci  // the frame in GetCode.
1151cb0ef41Sopenharmony_ci  static const int kSuccessfulCaptures = kInputString - kPointerSize;
1161cb0ef41Sopenharmony_ci  static const int kStringStartMinusOne = kSuccessfulCaptures - kPointerSize;
1171cb0ef41Sopenharmony_ci  static const int kBacktrackCount = kStringStartMinusOne - kSystemPointerSize;
1181cb0ef41Sopenharmony_ci  // Stores the initial value of the regexp stack pointer in a
1191cb0ef41Sopenharmony_ci  // position-independent representation (in case the regexp stack grows and
1201cb0ef41Sopenharmony_ci  // thus moves).
1211cb0ef41Sopenharmony_ci  static const int kRegExpStackBasePointer =
1221cb0ef41Sopenharmony_ci      kBacktrackCount - kSystemPointerSize;
1231cb0ef41Sopenharmony_ci
1241cb0ef41Sopenharmony_ci  // First register address. Following registers are below it on the stack.
1251cb0ef41Sopenharmony_ci  static const int kRegisterZero = kRegExpStackBasePointer - kSystemPointerSize;
1261cb0ef41Sopenharmony_ci
1271cb0ef41Sopenharmony_ci  // Initial size of code buffer.
1281cb0ef41Sopenharmony_ci  static const int kRegExpCodeSize = 1024;
1291cb0ef41Sopenharmony_ci
1301cb0ef41Sopenharmony_ci  static const int kBacktrackConstantPoolSize = 4;
1311cb0ef41Sopenharmony_ci
1321cb0ef41Sopenharmony_ci  // Check whether preemption has been requested.
1331cb0ef41Sopenharmony_ci  void CheckPreemption();
1341cb0ef41Sopenharmony_ci
1351cb0ef41Sopenharmony_ci  // Check whether we are exceeding the stack limit on the backtrack stack.
1361cb0ef41Sopenharmony_ci  void CheckStackLimit();
1371cb0ef41Sopenharmony_ci
1381cb0ef41Sopenharmony_ci  void CallCheckStackGuardState();
1391cb0ef41Sopenharmony_ci  void CallIsCharacterInRangeArray(const ZoneList<CharacterRange>* ranges);
1401cb0ef41Sopenharmony_ci
1411cb0ef41Sopenharmony_ci  // The ebp-relative location of a regexp register.
1421cb0ef41Sopenharmony_ci  MemOperand register_location(int register_index);
1431cb0ef41Sopenharmony_ci
1441cb0ef41Sopenharmony_ci  // Register holding the current input position as negative offset from
1451cb0ef41Sopenharmony_ci  // the end of the string.
1461cb0ef41Sopenharmony_ci  static constexpr Register current_input_offset() { return r6; }
1471cb0ef41Sopenharmony_ci
1481cb0ef41Sopenharmony_ci  // The register containing the current character after LoadCurrentCharacter.
1491cb0ef41Sopenharmony_ci  static constexpr Register current_character() { return r7; }
1501cb0ef41Sopenharmony_ci
1511cb0ef41Sopenharmony_ci  // Register holding address of the end of the input string.
1521cb0ef41Sopenharmony_ci  static constexpr Register end_of_input_address() { return r10; }
1531cb0ef41Sopenharmony_ci
1541cb0ef41Sopenharmony_ci  // Register holding the frame address. Local variables, parameters and
1551cb0ef41Sopenharmony_ci  // regexp registers are addressed relative to this.
1561cb0ef41Sopenharmony_ci  static constexpr Register frame_pointer() { return fp; }
1571cb0ef41Sopenharmony_ci
1581cb0ef41Sopenharmony_ci  // The register containing the backtrack stack top. Provides a meaningful
1591cb0ef41Sopenharmony_ci  // name to the register.
1601cb0ef41Sopenharmony_ci  static constexpr Register backtrack_stackpointer() { return r8; }
1611cb0ef41Sopenharmony_ci
1621cb0ef41Sopenharmony_ci  // Register holding pointer to the current code object.
1631cb0ef41Sopenharmony_ci  static constexpr Register code_pointer() { return r5; }
1641cb0ef41Sopenharmony_ci
1651cb0ef41Sopenharmony_ci  // Byte size of chars in the string to match (decided by the Mode argument)
1661cb0ef41Sopenharmony_ci  inline int char_size() const { return static_cast<int>(mode_); }
1671cb0ef41Sopenharmony_ci
1681cb0ef41Sopenharmony_ci  // Equivalent to a conditional branch to the label, unless the label
1691cb0ef41Sopenharmony_ci  // is nullptr, in which case it is a conditional Backtrack.
1701cb0ef41Sopenharmony_ci  void BranchOrBacktrack(Condition condition, Label* to);
1711cb0ef41Sopenharmony_ci
1721cb0ef41Sopenharmony_ci  // Call and return internally in the generated code in a way that
1731cb0ef41Sopenharmony_ci  // is GC-safe (i.e., doesn't leave absolute code addresses on the stack)
1741cb0ef41Sopenharmony_ci  inline void SafeCall(Label* to, Condition cond = al);
1751cb0ef41Sopenharmony_ci  inline void SafeReturn();
1761cb0ef41Sopenharmony_ci  inline void SafeCallTarget(Label* name);
1771cb0ef41Sopenharmony_ci
1781cb0ef41Sopenharmony_ci  // Pushes the value of a register on the backtrack stack. Decrements the
1791cb0ef41Sopenharmony_ci  // stack pointer by a word size and stores the register's value there.
1801cb0ef41Sopenharmony_ci  inline void Push(Register source);
1811cb0ef41Sopenharmony_ci
1821cb0ef41Sopenharmony_ci  // Pops a value from the backtrack stack. Reads the word at the stack pointer
1831cb0ef41Sopenharmony_ci  // and increments it by a word size.
1841cb0ef41Sopenharmony_ci  inline void Pop(Register target);
1851cb0ef41Sopenharmony_ci
1861cb0ef41Sopenharmony_ci  void LoadRegExpStackPointerFromMemory(Register dst);
1871cb0ef41Sopenharmony_ci  void StoreRegExpStackPointerToMemory(Register src, Register scratch);
1881cb0ef41Sopenharmony_ci  void PushRegExpBasePointer(Register stack_pointer, Register scratch);
1891cb0ef41Sopenharmony_ci  void PopRegExpBasePointer(Register stack_pointer_out, Register scratch);
1901cb0ef41Sopenharmony_ci
1911cb0ef41Sopenharmony_ci  Isolate* isolate() const { return masm_->isolate(); }
1921cb0ef41Sopenharmony_ci
1931cb0ef41Sopenharmony_ci  const std::unique_ptr<MacroAssembler> masm_;
1941cb0ef41Sopenharmony_ci  const NoRootArrayScope no_root_array_scope_;
1951cb0ef41Sopenharmony_ci
1961cb0ef41Sopenharmony_ci  // Which mode to generate code for (Latin1 or UC16).
1971cb0ef41Sopenharmony_ci  const Mode mode_;
1981cb0ef41Sopenharmony_ci
1991cb0ef41Sopenharmony_ci  // One greater than maximal register index actually used.
2001cb0ef41Sopenharmony_ci  int num_registers_;
2011cb0ef41Sopenharmony_ci
2021cb0ef41Sopenharmony_ci  // Number of registers to output at the end (the saved registers
2031cb0ef41Sopenharmony_ci  // are always 0..num_saved_registers_-1)
2041cb0ef41Sopenharmony_ci  const int num_saved_registers_;
2051cb0ef41Sopenharmony_ci
2061cb0ef41Sopenharmony_ci  // Labels used internally.
2071cb0ef41Sopenharmony_ci  Label entry_label_;
2081cb0ef41Sopenharmony_ci  Label start_label_;
2091cb0ef41Sopenharmony_ci  Label success_label_;
2101cb0ef41Sopenharmony_ci  Label backtrack_label_;
2111cb0ef41Sopenharmony_ci  Label exit_label_;
2121cb0ef41Sopenharmony_ci  Label check_preempt_label_;
2131cb0ef41Sopenharmony_ci  Label stack_overflow_label_;
2141cb0ef41Sopenharmony_ci  Label fallback_label_;
2151cb0ef41Sopenharmony_ci};
2161cb0ef41Sopenharmony_ci
2171cb0ef41Sopenharmony_ci}  // namespace internal
2181cb0ef41Sopenharmony_ci}  // namespace v8
2191cb0ef41Sopenharmony_ci
2201cb0ef41Sopenharmony_ci#endif  // V8_REGEXP_ARM_REGEXP_MACRO_ASSEMBLER_ARM_H_
221