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