11cb0ef41Sopenharmony_ci// Copyright 2013 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_ARM64_REGEXP_MACRO_ASSEMBLER_ARM64_H_ 61cb0ef41Sopenharmony_ci#define V8_REGEXP_ARM64_REGEXP_MACRO_ASSEMBLER_ARM64_H_ 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_ci#include "src/base/strings.h" 91cb0ef41Sopenharmony_ci#include "src/codegen/arm64/assembler-arm64.h" 101cb0ef41Sopenharmony_ci#include "src/codegen/macro-assembler.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 RegExpMacroAssemblerARM64 171cb0ef41Sopenharmony_ci : public NativeRegExpMacroAssembler { 181cb0ef41Sopenharmony_ci public: 191cb0ef41Sopenharmony_ci RegExpMacroAssemblerARM64(Isolate* isolate, Zone* zone, Mode mode, 201cb0ef41Sopenharmony_ci int registers_to_save); 211cb0ef41Sopenharmony_ci ~RegExpMacroAssemblerARM64() override; 221cb0ef41Sopenharmony_ci void AbortedCodeGeneration() override; 231cb0ef41Sopenharmony_ci int stack_limit_slack() override; 241cb0ef41Sopenharmony_ci void AdvanceCurrentPosition(int by) override; 251cb0ef41Sopenharmony_ci void AdvanceRegister(int reg, int by) override; 261cb0ef41Sopenharmony_ci void Backtrack() override; 271cb0ef41Sopenharmony_ci void Bind(Label* label) override; 281cb0ef41Sopenharmony_ci void CheckAtStart(int cp_offset, Label* on_at_start) override; 291cb0ef41Sopenharmony_ci void CheckCharacter(unsigned c, Label* on_equal) override; 301cb0ef41Sopenharmony_ci void CheckCharacterAfterAnd(unsigned c, unsigned mask, 311cb0ef41Sopenharmony_ci Label* on_equal) override; 321cb0ef41Sopenharmony_ci void CheckCharacterGT(base::uc16 limit, Label* on_greater) override; 331cb0ef41Sopenharmony_ci void CheckCharacterLT(base::uc16 limit, Label* on_less) override; 341cb0ef41Sopenharmony_ci void CheckCharacters(base::Vector<const base::uc16> str, int cp_offset, 351cb0ef41Sopenharmony_ci Label* on_failure, bool check_end_of_string); 361cb0ef41Sopenharmony_ci // A "greedy loop" is a loop that is both greedy and with a simple 371cb0ef41Sopenharmony_ci // body. It has a particularly simple implementation. 381cb0ef41Sopenharmony_ci void CheckGreedyLoop(Label* on_tos_equals_current_position) override; 391cb0ef41Sopenharmony_ci void CheckNotAtStart(int cp_offset, Label* on_not_at_start) override; 401cb0ef41Sopenharmony_ci void CheckNotBackReference(int start_reg, bool read_backward, 411cb0ef41Sopenharmony_ci Label* on_no_match) override; 421cb0ef41Sopenharmony_ci void CheckNotBackReferenceIgnoreCase(int start_reg, bool read_backward, 431cb0ef41Sopenharmony_ci bool unicode, 441cb0ef41Sopenharmony_ci Label* on_no_match) override; 451cb0ef41Sopenharmony_ci void CheckNotCharacter(unsigned c, Label* on_not_equal) override; 461cb0ef41Sopenharmony_ci void CheckNotCharacterAfterAnd(unsigned c, unsigned mask, 471cb0ef41Sopenharmony_ci Label* on_not_equal) override; 481cb0ef41Sopenharmony_ci void CheckNotCharacterAfterMinusAnd(base::uc16 c, base::uc16 minus, 491cb0ef41Sopenharmony_ci base::uc16 mask, 501cb0ef41Sopenharmony_ci Label* on_not_equal) override; 511cb0ef41Sopenharmony_ci void CheckCharacterInRange(base::uc16 from, base::uc16 to, 521cb0ef41Sopenharmony_ci Label* on_in_range) override; 531cb0ef41Sopenharmony_ci void CheckCharacterNotInRange(base::uc16 from, base::uc16 to, 541cb0ef41Sopenharmony_ci Label* on_not_in_range) override; 551cb0ef41Sopenharmony_ci bool CheckCharacterInRangeArray(const ZoneList<CharacterRange>* ranges, 561cb0ef41Sopenharmony_ci Label* on_in_range) override; 571cb0ef41Sopenharmony_ci bool CheckCharacterNotInRangeArray(const ZoneList<CharacterRange>* ranges, 581cb0ef41Sopenharmony_ci Label* on_not_in_range) override; 591cb0ef41Sopenharmony_ci void CheckBitInTable(Handle<ByteArray> table, Label* on_bit_set) override; 601cb0ef41Sopenharmony_ci 611cb0ef41Sopenharmony_ci // Checks whether the given offset from the current position is before 621cb0ef41Sopenharmony_ci // the end of the string. 631cb0ef41Sopenharmony_ci void CheckPosition(int cp_offset, Label* on_outside_input) override; 641cb0ef41Sopenharmony_ci bool CheckSpecialCharacterClass(StandardCharacterSet type, 651cb0ef41Sopenharmony_ci Label* on_no_match) override; 661cb0ef41Sopenharmony_ci void BindJumpTarget(Label* label = nullptr) override; 671cb0ef41Sopenharmony_ci void Fail() override; 681cb0ef41Sopenharmony_ci Handle<HeapObject> GetCode(Handle<String> source) override; 691cb0ef41Sopenharmony_ci void GoTo(Label* label) override; 701cb0ef41Sopenharmony_ci void IfRegisterGE(int reg, int comparand, Label* if_ge) override; 711cb0ef41Sopenharmony_ci void IfRegisterLT(int reg, int comparand, Label* if_lt) override; 721cb0ef41Sopenharmony_ci void IfRegisterEqPos(int reg, Label* if_eq) override; 731cb0ef41Sopenharmony_ci IrregexpImplementation Implementation() override; 741cb0ef41Sopenharmony_ci void LoadCurrentCharacterUnchecked(int cp_offset, 751cb0ef41Sopenharmony_ci int character_count) override; 761cb0ef41Sopenharmony_ci void PopCurrentPosition() override; 771cb0ef41Sopenharmony_ci void PopRegister(int register_index) override; 781cb0ef41Sopenharmony_ci void PushBacktrack(Label* label) override; 791cb0ef41Sopenharmony_ci void PushCurrentPosition() override; 801cb0ef41Sopenharmony_ci void PushRegister(int register_index, 811cb0ef41Sopenharmony_ci StackCheckFlag check_stack_limit) override; 821cb0ef41Sopenharmony_ci void ReadCurrentPositionFromRegister(int reg) override; 831cb0ef41Sopenharmony_ci void ReadStackPointerFromRegister(int reg) override; 841cb0ef41Sopenharmony_ci void SetCurrentPositionFromEnd(int by) override; 851cb0ef41Sopenharmony_ci void SetRegister(int register_index, int to) override; 861cb0ef41Sopenharmony_ci bool Succeed() override; 871cb0ef41Sopenharmony_ci void WriteCurrentPositionToRegister(int reg, int cp_offset) override; 881cb0ef41Sopenharmony_ci void ClearRegisters(int reg_from, int reg_to) override; 891cb0ef41Sopenharmony_ci void WriteStackPointerToRegister(int reg) override; 901cb0ef41Sopenharmony_ci 911cb0ef41Sopenharmony_ci // Called from RegExp if the stack-guard is triggered. 921cb0ef41Sopenharmony_ci // If the code object is relocated, the return address is fixed before 931cb0ef41Sopenharmony_ci // returning. 941cb0ef41Sopenharmony_ci // {raw_code} is an Address because this is called via ExternalReference. 951cb0ef41Sopenharmony_ci static int CheckStackGuardState(Address* return_address, Address raw_code, 961cb0ef41Sopenharmony_ci Address re_frame, int start_offset, 971cb0ef41Sopenharmony_ci const byte** input_start, 981cb0ef41Sopenharmony_ci const byte** input_end); 991cb0ef41Sopenharmony_ci 1001cb0ef41Sopenharmony_ci private: 1011cb0ef41Sopenharmony_ci // Above the frame pointer - Stored registers and stack passed parameters. 1021cb0ef41Sopenharmony_ci static const int kFramePointer = 0; 1031cb0ef41Sopenharmony_ci static const int kReturnAddress = kFramePointer + kSystemPointerSize; 1041cb0ef41Sopenharmony_ci // Callee-saved registers (x19-x28). 1051cb0ef41Sopenharmony_ci static const int kNumCalleeSavedRegisters = 10; 1061cb0ef41Sopenharmony_ci static const int kCalleeSavedRegisters = kReturnAddress + kSystemPointerSize; 1071cb0ef41Sopenharmony_ci 1081cb0ef41Sopenharmony_ci // Below the frame pointer. 1091cb0ef41Sopenharmony_ci // Register parameters stored by setup code. 1101cb0ef41Sopenharmony_ci static const int kIsolate = -kSystemPointerSize; 1111cb0ef41Sopenharmony_ci static const int kDirectCall = kIsolate - kSystemPointerSize; 1121cb0ef41Sopenharmony_ci static const int kOutputSize = kDirectCall - kSystemPointerSize; 1131cb0ef41Sopenharmony_ci static const int kInput = kOutputSize - kSystemPointerSize; 1141cb0ef41Sopenharmony_ci // When adding local variables remember to push space for them in 1151cb0ef41Sopenharmony_ci // the frame in GetCode. 1161cb0ef41Sopenharmony_ci static const int kSuccessCounter = kInput - kSystemPointerSize; 1171cb0ef41Sopenharmony_ci static const int kBacktrackCount = kSuccessCounter - 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 // A padding slot to preserve alignment. 1241cb0ef41Sopenharmony_ci static const int kStackLocalPadding = 1251cb0ef41Sopenharmony_ci kRegExpStackBasePointer - kSystemPointerSize; 1261cb0ef41Sopenharmony_ci static constexpr int kNumberOfStackLocals = 4; 1271cb0ef41Sopenharmony_ci 1281cb0ef41Sopenharmony_ci // First position register address on the stack. Following positions are 1291cb0ef41Sopenharmony_ci // below it. A position is a 32 bit value. 1301cb0ef41Sopenharmony_ci static const int kFirstRegisterOnStack = kStackLocalPadding - kWRegSize; 1311cb0ef41Sopenharmony_ci // A capture is a 64 bit value holding two position. 1321cb0ef41Sopenharmony_ci static const int kFirstCaptureOnStack = kStackLocalPadding - kXRegSize; 1331cb0ef41Sopenharmony_ci 1341cb0ef41Sopenharmony_ci // Initial size of code buffer. 1351cb0ef41Sopenharmony_ci static const int kRegExpCodeSize = 1024; 1361cb0ef41Sopenharmony_ci 1371cb0ef41Sopenharmony_ci // Registers x0 to x7 are used to store the first captures, they need to be 1381cb0ef41Sopenharmony_ci // retained over calls to C++ code. 1391cb0ef41Sopenharmony_ci void PushCachedRegisters(); 1401cb0ef41Sopenharmony_ci void PopCachedRegisters(); 1411cb0ef41Sopenharmony_ci 1421cb0ef41Sopenharmony_ci // When initializing registers to a non-position value we can unroll 1431cb0ef41Sopenharmony_ci // the loop. Set the limit of registers to unroll. 1441cb0ef41Sopenharmony_ci static const int kNumRegistersToUnroll = 16; 1451cb0ef41Sopenharmony_ci 1461cb0ef41Sopenharmony_ci // We are using x0 to x7 as a register cache. Each hardware register must 1471cb0ef41Sopenharmony_ci // contain one capture, that is two 32 bit registers. We can cache at most 1481cb0ef41Sopenharmony_ci // 16 registers. 1491cb0ef41Sopenharmony_ci static const int kNumCachedRegisters = 16; 1501cb0ef41Sopenharmony_ci 1511cb0ef41Sopenharmony_ci // Check whether preemption has been requested. 1521cb0ef41Sopenharmony_ci void CheckPreemption(); 1531cb0ef41Sopenharmony_ci 1541cb0ef41Sopenharmony_ci // Check whether we are exceeding the stack limit on the backtrack stack. 1551cb0ef41Sopenharmony_ci void CheckStackLimit(); 1561cb0ef41Sopenharmony_ci 1571cb0ef41Sopenharmony_ci void CallCheckStackGuardState(Register scratch); 1581cb0ef41Sopenharmony_ci void CallIsCharacterInRangeArray(const ZoneList<CharacterRange>* ranges); 1591cb0ef41Sopenharmony_ci 1601cb0ef41Sopenharmony_ci // Location of a 32 bit position register. 1611cb0ef41Sopenharmony_ci MemOperand register_location(int register_index); 1621cb0ef41Sopenharmony_ci 1631cb0ef41Sopenharmony_ci // Location of a 64 bit capture, combining two position registers. 1641cb0ef41Sopenharmony_ci MemOperand capture_location(int register_index, Register scratch); 1651cb0ef41Sopenharmony_ci 1661cb0ef41Sopenharmony_ci // Register holding the current input position as negative offset from 1671cb0ef41Sopenharmony_ci // the end of the string. 1681cb0ef41Sopenharmony_ci static constexpr Register current_input_offset() { return w21; } 1691cb0ef41Sopenharmony_ci 1701cb0ef41Sopenharmony_ci // The register containing the current character after LoadCurrentCharacter. 1711cb0ef41Sopenharmony_ci static constexpr Register current_character() { return w22; } 1721cb0ef41Sopenharmony_ci 1731cb0ef41Sopenharmony_ci // Register holding address of the end of the input string. 1741cb0ef41Sopenharmony_ci static constexpr Register input_end() { return x25; } 1751cb0ef41Sopenharmony_ci 1761cb0ef41Sopenharmony_ci // Register holding address of the start of the input string. 1771cb0ef41Sopenharmony_ci static constexpr Register input_start() { return x26; } 1781cb0ef41Sopenharmony_ci 1791cb0ef41Sopenharmony_ci // Register holding the offset from the start of the string where we should 1801cb0ef41Sopenharmony_ci // start matching. 1811cb0ef41Sopenharmony_ci static constexpr Register start_offset() { return w27; } 1821cb0ef41Sopenharmony_ci 1831cb0ef41Sopenharmony_ci // Pointer to the output array's first element. 1841cb0ef41Sopenharmony_ci static constexpr Register output_array() { return x28; } 1851cb0ef41Sopenharmony_ci 1861cb0ef41Sopenharmony_ci // Register holding the frame address. Local variables, parameters and 1871cb0ef41Sopenharmony_ci // regexp registers are addressed relative to this. 1881cb0ef41Sopenharmony_ci static constexpr Register frame_pointer() { return fp; } 1891cb0ef41Sopenharmony_ci 1901cb0ef41Sopenharmony_ci // The register containing the backtrack stack top. Provides a meaningful 1911cb0ef41Sopenharmony_ci // name to the register. 1921cb0ef41Sopenharmony_ci static constexpr Register backtrack_stackpointer() { return x23; } 1931cb0ef41Sopenharmony_ci 1941cb0ef41Sopenharmony_ci // Register holding pointer to the current code object. 1951cb0ef41Sopenharmony_ci static constexpr Register code_pointer() { return x20; } 1961cb0ef41Sopenharmony_ci 1971cb0ef41Sopenharmony_ci // Register holding the value used for clearing capture registers. 1981cb0ef41Sopenharmony_ci static constexpr Register string_start_minus_one() { return w24; } 1991cb0ef41Sopenharmony_ci // The top 32 bit of this register is used to store this value 2001cb0ef41Sopenharmony_ci // twice. This is used for clearing more than one register at a time. 2011cb0ef41Sopenharmony_ci static constexpr Register twice_non_position_value() { return x24; } 2021cb0ef41Sopenharmony_ci 2031cb0ef41Sopenharmony_ci // Byte size of chars in the string to match (decided by the Mode argument) 2041cb0ef41Sopenharmony_ci int char_size() const { return static_cast<int>(mode_); } 2051cb0ef41Sopenharmony_ci 2061cb0ef41Sopenharmony_ci // Equivalent to a conditional branch to the label, unless the label 2071cb0ef41Sopenharmony_ci // is nullptr, in which case it is a conditional Backtrack. 2081cb0ef41Sopenharmony_ci void BranchOrBacktrack(Condition condition, Label* to); 2091cb0ef41Sopenharmony_ci 2101cb0ef41Sopenharmony_ci // Compares reg against immmediate before calling BranchOrBacktrack. 2111cb0ef41Sopenharmony_ci // It makes use of the Cbz and Cbnz instructions. 2121cb0ef41Sopenharmony_ci void CompareAndBranchOrBacktrack(Register reg, 2131cb0ef41Sopenharmony_ci int immediate, 2141cb0ef41Sopenharmony_ci Condition condition, 2151cb0ef41Sopenharmony_ci Label* to); 2161cb0ef41Sopenharmony_ci 2171cb0ef41Sopenharmony_ci inline void CallIf(Label* to, Condition condition); 2181cb0ef41Sopenharmony_ci 2191cb0ef41Sopenharmony_ci // Save and restore the link register on the stack in a way that 2201cb0ef41Sopenharmony_ci // is GC-safe. 2211cb0ef41Sopenharmony_ci inline void SaveLinkRegister(); 2221cb0ef41Sopenharmony_ci inline void RestoreLinkRegister(); 2231cb0ef41Sopenharmony_ci 2241cb0ef41Sopenharmony_ci // Pushes the value of a register on the backtrack stack. Decrements the 2251cb0ef41Sopenharmony_ci // stack pointer by a word size and stores the register's value there. 2261cb0ef41Sopenharmony_ci inline void Push(Register source); 2271cb0ef41Sopenharmony_ci 2281cb0ef41Sopenharmony_ci // Pops a value from the backtrack stack. Reads the word at the stack pointer 2291cb0ef41Sopenharmony_ci // and increments it by a word size. 2301cb0ef41Sopenharmony_ci inline void Pop(Register target); 2311cb0ef41Sopenharmony_ci 2321cb0ef41Sopenharmony_ci // This state indicates where the register actually is. 2331cb0ef41Sopenharmony_ci enum RegisterState { 2341cb0ef41Sopenharmony_ci STACKED, // Resides in memory. 2351cb0ef41Sopenharmony_ci CACHED_LSW, // Least Significant Word of a 64 bit hardware register. 2361cb0ef41Sopenharmony_ci CACHED_MSW // Most Significant Word of a 64 bit hardware register. 2371cb0ef41Sopenharmony_ci }; 2381cb0ef41Sopenharmony_ci 2391cb0ef41Sopenharmony_ci RegisterState GetRegisterState(int register_index) { 2401cb0ef41Sopenharmony_ci DCHECK_LE(0, register_index); 2411cb0ef41Sopenharmony_ci if (register_index >= kNumCachedRegisters) { 2421cb0ef41Sopenharmony_ci return STACKED; 2431cb0ef41Sopenharmony_ci } else { 2441cb0ef41Sopenharmony_ci if ((register_index % 2) == 0) { 2451cb0ef41Sopenharmony_ci return CACHED_LSW; 2461cb0ef41Sopenharmony_ci } else { 2471cb0ef41Sopenharmony_ci return CACHED_MSW; 2481cb0ef41Sopenharmony_ci } 2491cb0ef41Sopenharmony_ci } 2501cb0ef41Sopenharmony_ci } 2511cb0ef41Sopenharmony_ci 2521cb0ef41Sopenharmony_ci // Store helper that takes the state of the register into account. 2531cb0ef41Sopenharmony_ci inline void StoreRegister(int register_index, Register source); 2541cb0ef41Sopenharmony_ci 2551cb0ef41Sopenharmony_ci // Returns a hardware W register that holds the value of the capture 2561cb0ef41Sopenharmony_ci // register. 2571cb0ef41Sopenharmony_ci // 2581cb0ef41Sopenharmony_ci // This function will try to use an existing cache register (w0-w7) for the 2591cb0ef41Sopenharmony_ci // result. Otherwise, it will load the value into maybe_result. 2601cb0ef41Sopenharmony_ci // 2611cb0ef41Sopenharmony_ci // If the returned register is anything other than maybe_result, calling code 2621cb0ef41Sopenharmony_ci // must not write to it. 2631cb0ef41Sopenharmony_ci inline Register GetRegister(int register_index, Register maybe_result); 2641cb0ef41Sopenharmony_ci 2651cb0ef41Sopenharmony_ci // Returns the harware register (x0-x7) holding the value of the capture 2661cb0ef41Sopenharmony_ci // register. 2671cb0ef41Sopenharmony_ci // This assumes that the state of the register is not STACKED. 2681cb0ef41Sopenharmony_ci inline Register GetCachedRegister(int register_index); 2691cb0ef41Sopenharmony_ci 2701cb0ef41Sopenharmony_ci void LoadRegExpStackPointerFromMemory(Register dst); 2711cb0ef41Sopenharmony_ci void StoreRegExpStackPointerToMemory(Register src, Register scratch); 2721cb0ef41Sopenharmony_ci void PushRegExpBasePointer(Register stack_pointer, Register scratch); 2731cb0ef41Sopenharmony_ci void PopRegExpBasePointer(Register stack_pointer_out, Register scratch); 2741cb0ef41Sopenharmony_ci 2751cb0ef41Sopenharmony_ci Isolate* isolate() const { return masm_->isolate(); } 2761cb0ef41Sopenharmony_ci 2771cb0ef41Sopenharmony_ci const std::unique_ptr<MacroAssembler> masm_; 2781cb0ef41Sopenharmony_ci const NoRootArrayScope no_root_array_scope_; 2791cb0ef41Sopenharmony_ci 2801cb0ef41Sopenharmony_ci // Which mode to generate code for (LATIN1 or UC16). 2811cb0ef41Sopenharmony_ci const Mode mode_; 2821cb0ef41Sopenharmony_ci 2831cb0ef41Sopenharmony_ci // One greater than maximal register index actually used. 2841cb0ef41Sopenharmony_ci int num_registers_; 2851cb0ef41Sopenharmony_ci 2861cb0ef41Sopenharmony_ci // Number of registers to output at the end (the saved registers 2871cb0ef41Sopenharmony_ci // are always 0..num_saved_registers_-1) 2881cb0ef41Sopenharmony_ci const int num_saved_registers_; 2891cb0ef41Sopenharmony_ci 2901cb0ef41Sopenharmony_ci // Labels used internally. 2911cb0ef41Sopenharmony_ci Label entry_label_; 2921cb0ef41Sopenharmony_ci Label start_label_; 2931cb0ef41Sopenharmony_ci Label success_label_; 2941cb0ef41Sopenharmony_ci Label backtrack_label_; 2951cb0ef41Sopenharmony_ci Label exit_label_; 2961cb0ef41Sopenharmony_ci Label check_preempt_label_; 2971cb0ef41Sopenharmony_ci Label stack_overflow_label_; 2981cb0ef41Sopenharmony_ci Label fallback_label_; 2991cb0ef41Sopenharmony_ci}; 3001cb0ef41Sopenharmony_ci 3011cb0ef41Sopenharmony_ci} // namespace internal 3021cb0ef41Sopenharmony_ci} // namespace v8 3031cb0ef41Sopenharmony_ci 3041cb0ef41Sopenharmony_ci#endif // V8_REGEXP_ARM64_REGEXP_MACRO_ASSEMBLER_ARM64_H_ 305