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_X64_REGEXP_MACRO_ASSEMBLER_X64_H_ 61cb0ef41Sopenharmony_ci#define V8_REGEXP_X64_REGEXP_MACRO_ASSEMBLER_X64_H_ 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_ci#include "src/codegen/macro-assembler.h" 91cb0ef41Sopenharmony_ci#include "src/regexp/regexp-macro-assembler.h" 101cb0ef41Sopenharmony_ci#include "src/zone/zone-chunk-list.h" 111cb0ef41Sopenharmony_ci 121cb0ef41Sopenharmony_cinamespace v8 { 131cb0ef41Sopenharmony_cinamespace internal { 141cb0ef41Sopenharmony_ci 151cb0ef41Sopenharmony_ciclass V8_EXPORT_PRIVATE RegExpMacroAssemblerX64 161cb0ef41Sopenharmony_ci : public NativeRegExpMacroAssembler { 171cb0ef41Sopenharmony_ci public: 181cb0ef41Sopenharmony_ci RegExpMacroAssemblerX64(Isolate* isolate, Zone* zone, Mode mode, 191cb0ef41Sopenharmony_ci int registers_to_save); 201cb0ef41Sopenharmony_ci ~RegExpMacroAssemblerX64() 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(uint32_t c, Label* on_equal) override; 281cb0ef41Sopenharmony_ci void CheckCharacterAfterAnd(uint32_t c, uint32_t 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(uint32_t c, Label* on_not_equal) override; 421cb0ef41Sopenharmony_ci void CheckNotCharacterAfterAnd(uint32_t c, uint32_t 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 rbp of function parameters and stored registers. 951cb0ef41Sopenharmony_ci static const int kFramePointer = 0; 961cb0ef41Sopenharmony_ci // Above the frame pointer - function parameters and return address. 971cb0ef41Sopenharmony_ci static const int kReturn_eip = kFramePointer + kSystemPointerSize; 981cb0ef41Sopenharmony_ci static const int kFrameAlign = kReturn_eip + kSystemPointerSize; 991cb0ef41Sopenharmony_ci 1001cb0ef41Sopenharmony_ci#ifdef V8_TARGET_OS_WIN 1011cb0ef41Sopenharmony_ci // Parameters (first four passed as registers, but with room on stack). 1021cb0ef41Sopenharmony_ci // In Microsoft 64-bit Calling Convention, there is room on the callers 1031cb0ef41Sopenharmony_ci // stack (before the return address) to spill parameter registers. We 1041cb0ef41Sopenharmony_ci // use this space to store the register passed parameters. 1051cb0ef41Sopenharmony_ci static const int kInputString = kFrameAlign; 1061cb0ef41Sopenharmony_ci // StartIndex is passed as 32 bit int. 1071cb0ef41Sopenharmony_ci static const int kStartIndex = kInputString + kSystemPointerSize; 1081cb0ef41Sopenharmony_ci static const int kInputStart = kStartIndex + kSystemPointerSize; 1091cb0ef41Sopenharmony_ci static const int kInputEnd = kInputStart + kSystemPointerSize; 1101cb0ef41Sopenharmony_ci static const int kRegisterOutput = kInputEnd + kSystemPointerSize; 1111cb0ef41Sopenharmony_ci // For the case of global regular expression, we have room to store at least 1121cb0ef41Sopenharmony_ci // one set of capture results. For the case of non-global regexp, we ignore 1131cb0ef41Sopenharmony_ci // this value. NumOutputRegisters is passed as 32-bit value. The upper 1141cb0ef41Sopenharmony_ci // 32 bit of this 64-bit stack slot may contain garbage. 1151cb0ef41Sopenharmony_ci static const int kNumOutputRegisters = kRegisterOutput + kSystemPointerSize; 1161cb0ef41Sopenharmony_ci // DirectCall is passed as 32 bit int (values 0 or 1). 1171cb0ef41Sopenharmony_ci static const int kDirectCall = kNumOutputRegisters + kSystemPointerSize; 1181cb0ef41Sopenharmony_ci static const int kIsolate = kDirectCall + kSystemPointerSize; 1191cb0ef41Sopenharmony_ci#else 1201cb0ef41Sopenharmony_ci // In AMD64 ABI Calling Convention, the first six integer parameters 1211cb0ef41Sopenharmony_ci // are passed as registers, and caller must allocate space on the stack 1221cb0ef41Sopenharmony_ci // if it wants them stored. We push the parameters after the frame pointer. 1231cb0ef41Sopenharmony_ci static const int kInputString = kFramePointer - kSystemPointerSize; 1241cb0ef41Sopenharmony_ci static const int kStartIndex = kInputString - kSystemPointerSize; 1251cb0ef41Sopenharmony_ci static const int kInputStart = kStartIndex - kSystemPointerSize; 1261cb0ef41Sopenharmony_ci static const int kInputEnd = kInputStart - kSystemPointerSize; 1271cb0ef41Sopenharmony_ci static const int kRegisterOutput = kInputEnd - kSystemPointerSize; 1281cb0ef41Sopenharmony_ci // For the case of global regular expression, we have room to store at least 1291cb0ef41Sopenharmony_ci // one set of capture results. For the case of non-global regexp, we ignore 1301cb0ef41Sopenharmony_ci // this value. 1311cb0ef41Sopenharmony_ci static const int kNumOutputRegisters = kRegisterOutput - kSystemPointerSize; 1321cb0ef41Sopenharmony_ci 1331cb0ef41Sopenharmony_ci static const int kDirectCall = kFrameAlign; 1341cb0ef41Sopenharmony_ci static const int kIsolate = kDirectCall + kSystemPointerSize; 1351cb0ef41Sopenharmony_ci#endif 1361cb0ef41Sopenharmony_ci 1371cb0ef41Sopenharmony_ci // We push callee-save registers that we use after the frame pointer (and 1381cb0ef41Sopenharmony_ci // after the parameters). 1391cb0ef41Sopenharmony_ci#ifdef V8_TARGET_OS_WIN 1401cb0ef41Sopenharmony_ci static const int kBackup_rsi = kFramePointer - kSystemPointerSize; 1411cb0ef41Sopenharmony_ci static const int kBackup_rdi = kBackup_rsi - kSystemPointerSize; 1421cb0ef41Sopenharmony_ci static const int kBackup_rbx = kBackup_rdi - kSystemPointerSize; 1431cb0ef41Sopenharmony_ci static const int kNumCalleeSaveRegisters = 3; 1441cb0ef41Sopenharmony_ci static const int kLastCalleeSaveRegister = kBackup_rbx; 1451cb0ef41Sopenharmony_ci#else 1461cb0ef41Sopenharmony_ci static const int kBackup_rbx = kNumOutputRegisters - kSystemPointerSize; 1471cb0ef41Sopenharmony_ci static const int kNumCalleeSaveRegisters = 1; 1481cb0ef41Sopenharmony_ci static const int kLastCalleeSaveRegister = kBackup_rbx; 1491cb0ef41Sopenharmony_ci#endif 1501cb0ef41Sopenharmony_ci 1511cb0ef41Sopenharmony_ci // When adding local variables remember to push space for them in 1521cb0ef41Sopenharmony_ci // the frame in GetCode. 1531cb0ef41Sopenharmony_ci static const int kSuccessfulCaptures = 1541cb0ef41Sopenharmony_ci kLastCalleeSaveRegister - kSystemPointerSize; 1551cb0ef41Sopenharmony_ci static const int kStringStartMinusOne = 1561cb0ef41Sopenharmony_ci kSuccessfulCaptures - kSystemPointerSize; 1571cb0ef41Sopenharmony_ci static const int kBacktrackCount = kStringStartMinusOne - kSystemPointerSize; 1581cb0ef41Sopenharmony_ci // Stores the initial value of the regexp stack pointer in a 1591cb0ef41Sopenharmony_ci // position-independent representation (in case the regexp stack grows and 1601cb0ef41Sopenharmony_ci // thus moves). 1611cb0ef41Sopenharmony_ci static const int kRegExpStackBasePointer = 1621cb0ef41Sopenharmony_ci kBacktrackCount - kSystemPointerSize; 1631cb0ef41Sopenharmony_ci 1641cb0ef41Sopenharmony_ci // First register address. Following registers are below it on the stack. 1651cb0ef41Sopenharmony_ci static const int kRegisterZero = kRegExpStackBasePointer - kSystemPointerSize; 1661cb0ef41Sopenharmony_ci 1671cb0ef41Sopenharmony_ci // Initial size of code buffer. 1681cb0ef41Sopenharmony_ci static const int kRegExpCodeSize = 1024; 1691cb0ef41Sopenharmony_ci 1701cb0ef41Sopenharmony_ci void PushCallerSavedRegisters(); 1711cb0ef41Sopenharmony_ci void PopCallerSavedRegisters(); 1721cb0ef41Sopenharmony_ci 1731cb0ef41Sopenharmony_ci // Check whether preemption has been requested. 1741cb0ef41Sopenharmony_ci void CheckPreemption(); 1751cb0ef41Sopenharmony_ci 1761cb0ef41Sopenharmony_ci // Check whether we are exceeding the stack limit on the backtrack stack. 1771cb0ef41Sopenharmony_ci void CheckStackLimit(); 1781cb0ef41Sopenharmony_ci 1791cb0ef41Sopenharmony_ci void CallCheckStackGuardState(); 1801cb0ef41Sopenharmony_ci void CallIsCharacterInRangeArray(const ZoneList<CharacterRange>* ranges); 1811cb0ef41Sopenharmony_ci 1821cb0ef41Sopenharmony_ci // The rbp-relative location of a regexp register. 1831cb0ef41Sopenharmony_ci Operand register_location(int register_index); 1841cb0ef41Sopenharmony_ci 1851cb0ef41Sopenharmony_ci // The register containing the current character after LoadCurrentCharacter. 1861cb0ef41Sopenharmony_ci static constexpr Register current_character() { return rdx; } 1871cb0ef41Sopenharmony_ci 1881cb0ef41Sopenharmony_ci // The register containing the backtrack stack top. Provides a meaningful 1891cb0ef41Sopenharmony_ci // name to the register. 1901cb0ef41Sopenharmony_ci static constexpr Register backtrack_stackpointer() { return rcx; } 1911cb0ef41Sopenharmony_ci 1921cb0ef41Sopenharmony_ci // The registers containing a self pointer to this code's Code object. 1931cb0ef41Sopenharmony_ci static constexpr Register code_object_pointer() { return r8; } 1941cb0ef41Sopenharmony_ci 1951cb0ef41Sopenharmony_ci // Byte size of chars in the string to match (decided by the Mode argument) 1961cb0ef41Sopenharmony_ci inline int char_size() { return static_cast<int>(mode_); } 1971cb0ef41Sopenharmony_ci 1981cb0ef41Sopenharmony_ci // Equivalent to a conditional branch to the label, unless the label 1991cb0ef41Sopenharmony_ci // is nullptr, in which case it is a conditional Backtrack. 2001cb0ef41Sopenharmony_ci void BranchOrBacktrack(Condition condition, Label* to); 2011cb0ef41Sopenharmony_ci 2021cb0ef41Sopenharmony_ci void MarkPositionForCodeRelativeFixup() { 2031cb0ef41Sopenharmony_ci code_relative_fixup_positions_.push_back(masm_.pc_offset()); 2041cb0ef41Sopenharmony_ci } 2051cb0ef41Sopenharmony_ci 2061cb0ef41Sopenharmony_ci void FixupCodeRelativePositions(); 2071cb0ef41Sopenharmony_ci 2081cb0ef41Sopenharmony_ci // Call and return internally in the generated code in a way that 2091cb0ef41Sopenharmony_ci // is GC-safe (i.e., doesn't leave absolute code addresses on the stack) 2101cb0ef41Sopenharmony_ci inline void SafeCall(Label* to); 2111cb0ef41Sopenharmony_ci inline void SafeCallTarget(Label* label); 2121cb0ef41Sopenharmony_ci inline void SafeReturn(); 2131cb0ef41Sopenharmony_ci 2141cb0ef41Sopenharmony_ci // Pushes the value of a register on the backtrack stack. Decrements the 2151cb0ef41Sopenharmony_ci // stack pointer (rcx) by a word size and stores the register's value there. 2161cb0ef41Sopenharmony_ci inline void Push(Register source); 2171cb0ef41Sopenharmony_ci 2181cb0ef41Sopenharmony_ci // Pushes a value on the backtrack stack. Decrements the stack pointer (rcx) 2191cb0ef41Sopenharmony_ci // by a word size and stores the value there. 2201cb0ef41Sopenharmony_ci inline void Push(Immediate value); 2211cb0ef41Sopenharmony_ci 2221cb0ef41Sopenharmony_ci // Pushes the Code object relative offset of a label on the backtrack stack 2231cb0ef41Sopenharmony_ci // (i.e., a backtrack target). Decrements the stack pointer (rcx) 2241cb0ef41Sopenharmony_ci // by a word size and stores the value there. 2251cb0ef41Sopenharmony_ci inline void Push(Label* label); 2261cb0ef41Sopenharmony_ci 2271cb0ef41Sopenharmony_ci // Pops a value from the backtrack stack. Reads the word at the stack pointer 2281cb0ef41Sopenharmony_ci // (rcx) and increments it by a word size. 2291cb0ef41Sopenharmony_ci inline void Pop(Register target); 2301cb0ef41Sopenharmony_ci 2311cb0ef41Sopenharmony_ci // Drops the top value from the backtrack stack without reading it. 2321cb0ef41Sopenharmony_ci // Increments the stack pointer (rcx) by a word size. 2331cb0ef41Sopenharmony_ci inline void Drop(); 2341cb0ef41Sopenharmony_ci 2351cb0ef41Sopenharmony_ci void LoadRegExpStackPointerFromMemory(Register dst); 2361cb0ef41Sopenharmony_ci void StoreRegExpStackPointerToMemory(Register src, Register scratch); 2371cb0ef41Sopenharmony_ci void PushRegExpBasePointer(Register scratch_pointer, Register scratch); 2381cb0ef41Sopenharmony_ci void PopRegExpBasePointer(Register scratch_pointer_out, Register scratch); 2391cb0ef41Sopenharmony_ci 2401cb0ef41Sopenharmony_ci inline void ReadPositionFromRegister(Register dst, int reg); 2411cb0ef41Sopenharmony_ci 2421cb0ef41Sopenharmony_ci Isolate* isolate() const { return masm_.isolate(); } 2431cb0ef41Sopenharmony_ci 2441cb0ef41Sopenharmony_ci MacroAssembler masm_; 2451cb0ef41Sopenharmony_ci 2461cb0ef41Sopenharmony_ci // On x64, there is no reason to keep the kRootRegister uninitialized; we 2471cb0ef41Sopenharmony_ci // could easily use it by 1. initializing it and 2. storing/restoring it 2481cb0ef41Sopenharmony_ci // as callee-save on entry/exit. 2491cb0ef41Sopenharmony_ci // But: on other platforms, specifically ia32, it would be tricky to enable 2501cb0ef41Sopenharmony_ci // the kRootRegister since it's currently used for other purposes. Thus, for 2511cb0ef41Sopenharmony_ci // consistency, we also keep it uninitialized here. 2521cb0ef41Sopenharmony_ci const NoRootArrayScope no_root_array_scope_; 2531cb0ef41Sopenharmony_ci 2541cb0ef41Sopenharmony_ci ZoneChunkList<int> code_relative_fixup_positions_; 2551cb0ef41Sopenharmony_ci 2561cb0ef41Sopenharmony_ci // Which mode to generate code for (LATIN1 or UC16). 2571cb0ef41Sopenharmony_ci const Mode mode_; 2581cb0ef41Sopenharmony_ci 2591cb0ef41Sopenharmony_ci // One greater than maximal register index actually used. 2601cb0ef41Sopenharmony_ci int num_registers_; 2611cb0ef41Sopenharmony_ci 2621cb0ef41Sopenharmony_ci // Number of registers to output at the end (the saved registers 2631cb0ef41Sopenharmony_ci // are always 0..num_saved_registers_-1) 2641cb0ef41Sopenharmony_ci const int num_saved_registers_; 2651cb0ef41Sopenharmony_ci 2661cb0ef41Sopenharmony_ci // Labels used internally. 2671cb0ef41Sopenharmony_ci Label entry_label_; 2681cb0ef41Sopenharmony_ci Label start_label_; 2691cb0ef41Sopenharmony_ci Label success_label_; 2701cb0ef41Sopenharmony_ci Label backtrack_label_; 2711cb0ef41Sopenharmony_ci Label exit_label_; 2721cb0ef41Sopenharmony_ci Label check_preempt_label_; 2731cb0ef41Sopenharmony_ci Label stack_overflow_label_; 2741cb0ef41Sopenharmony_ci Label fallback_label_; 2751cb0ef41Sopenharmony_ci}; 2761cb0ef41Sopenharmony_ci 2771cb0ef41Sopenharmony_ci} // namespace internal 2781cb0ef41Sopenharmony_ci} // namespace v8 2791cb0ef41Sopenharmony_ci 2801cb0ef41Sopenharmony_ci#endif // V8_REGEXP_X64_REGEXP_MACRO_ASSEMBLER_X64_H_ 281