1 // Copyright 2021 the V8 project authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef V8_REGEXP_RISCV64_REGEXP_MACRO_ASSEMBLER_RISCV64_H_ 6 #define V8_REGEXP_RISCV64_REGEXP_MACRO_ASSEMBLER_RISCV64_H_ 7 8 #include "src/base/strings.h" 9 #include "src/codegen/macro-assembler.h" 10 #include "src/codegen/riscv64/assembler-riscv64.h" 11 #include "src/regexp/regexp-macro-assembler.h" 12 13 namespace v8 { 14 namespace internal { 15 16 class V8_EXPORT_PRIVATE RegExpMacroAssemblerRISCV 17 : public NativeRegExpMacroAssembler { 18 public: 19 RegExpMacroAssemblerRISCV(Isolate* isolate, Zone* zone, Mode mode, 20 int registers_to_save); 21 ~RegExpMacroAssemblerRISCV() override; 22 int stack_limit_slack() override; 23 void AdvanceCurrentPosition(int by) override; 24 void AdvanceRegister(int reg, int by) override; 25 void Backtrack() override; 26 void Bind(Label* label) override; 27 void CheckAtStart(int cp_offset, Label* on_at_start) override; 28 void CheckCharacter(uint32_t c, Label* on_equal) override; 29 void CheckCharacterAfterAnd(uint32_t c, uint32_t mask, 30 Label* on_equal) override; 31 void CheckCharacterGT(base::uc16 limit, Label* on_greater) override; 32 void CheckCharacterLT(base::uc16 limit, Label* on_less) override; 33 // A "greedy loop" is a loop that is both greedy and with a simple 34 // body. It has a particularly simple implementation. 35 void CheckGreedyLoop(Label* on_tos_equals_current_position) override; 36 void CheckNotAtStart(int cp_offset, Label* on_not_at_start) override; 37 void CheckNotBackReference(int start_reg, bool read_backward, 38 Label* on_no_match) override; 39 void CheckNotBackReferenceIgnoreCase(int start_reg, bool read_backward, 40 bool unicode, 41 Label* on_no_match) override; 42 void CheckNotCharacter(uint32_t c, Label* on_not_equal) override; 43 void CheckNotCharacterAfterAnd(uint32_t c, uint32_t mask, 44 Label* on_not_equal) override; 45 void CheckNotCharacterAfterMinusAnd(base::uc16 c, base::uc16 minus, 46 base::uc16 mask, 47 Label* on_not_equal) override; 48 void CheckCharacterInRange(base::uc16 from, base::uc16 to, 49 Label* on_in_range) override; 50 void CheckCharacterNotInRange(base::uc16 from, base::uc16 to, 51 Label* on_not_in_range) override; 52 bool CheckCharacterInRangeArray(const ZoneList<CharacterRange>* ranges, 53 Label* on_in_range) override; 54 bool CheckCharacterNotInRangeArray(const ZoneList<CharacterRange>* ranges, 55 Label* on_not_in_range) override; 56 void CheckBitInTable(Handle<ByteArray> table, Label* on_bit_set) override; 57 58 // Checks whether the given offset from the current position is before 59 // the end of the string. 60 void CheckPosition(int cp_offset, Label* on_outside_input) override; 61 bool CheckSpecialCharacterClass(StandardCharacterSet type, 62 Label* on_no_match) override; 63 void Fail() override; 64 Handle<HeapObject> GetCode(Handle<String> source) override; 65 void GoTo(Label* label) override; 66 void IfRegisterGE(int reg, int comparand, Label* if_ge) override; 67 void IfRegisterLT(int reg, int comparand, Label* if_lt) override; 68 void IfRegisterEqPos(int reg, Label* if_eq) override; 69 IrregexpImplementation Implementation() override; 70 void LoadCurrentCharacterUnchecked(int cp_offset, 71 int character_count) override; 72 void PopCurrentPosition() override; 73 void PopRegister(int register_index) override; 74 void PushBacktrack(Label* label) override; 75 void PushCurrentPosition() override; 76 void PushRegister(int register_index, 77 StackCheckFlag check_stack_limit) override; 78 void ReadCurrentPositionFromRegister(int reg) override; 79 void ReadStackPointerFromRegister(int reg) override; 80 void SetCurrentPositionFromEnd(int by) override; 81 void SetRegister(int register_index, int to) override; 82 bool Succeed() override; 83 void WriteCurrentPositionToRegister(int reg, int cp_offset) override; 84 void ClearRegisters(int reg_from, int reg_to) override; 85 void WriteStackPointerToRegister(int reg) override; 86 #ifdef RISCV_HAS_NO_UNALIGNED 87 bool CanReadUnaligned() const override; 88 #endif 89 // Called from RegExp if the stack-guard is triggered. 90 // If the code object is relocated, the return address is fixed before 91 // returning. 92 // {raw_code} is an Address because this is called via ExternalReference. 93 static int64_t CheckStackGuardState(Address* return_address, Address raw_code, 94 Address re_frame); 95 96 void print_regexp_frame_constants(); 97 98 private: 99 // Offsets from frame_pointer() of function parameters and stored registers. 100 static const int kFramePointer = 0; 101 102 // Above the frame pointer - Stored registers and stack passed parameters. 103 // Registers s1 to s8, fp, and ra. 104 static const int kStoredRegisters = kFramePointer; 105 // Return address (stored from link register, read into pc on return). 106 107 // This 9 is 8 s-regs (s1..s8) plus fp. 108 static const int kNumCalleeRegsToRetain = 9; 109 static const int kReturnAddress = 110 kStoredRegisters + kNumCalleeRegsToRetain * kSystemPointerSize; 111 112 // Stack frame header. 113 static const int kStackFrameHeader = kReturnAddress; 114 // Below the frame pointer. 115 // Register parameters stored by setup code. 116 static const int kIsolate = kFramePointer - kSystemPointerSize; 117 static const int kDirectCall = kIsolate - kSystemPointerSize; 118 static const int kNumOutputRegisters = kDirectCall - kSystemPointerSize; 119 static const int kRegisterOutput = kNumOutputRegisters - kSystemPointerSize; 120 static const int kInputEnd = kRegisterOutput - kSystemPointerSize; 121 static const int kInputStart = kInputEnd - kSystemPointerSize; 122 static const int kStartIndex = kInputStart - kSystemPointerSize; 123 static const int kInputString = kStartIndex - kSystemPointerSize; 124 // When adding local variables remember to push space for them in 125 // the frame in GetCode. 126 static const int kSuccessfulCaptures = kInputString - kSystemPointerSize; 127 static const int kStringStartMinusOne = 128 kSuccessfulCaptures - kSystemPointerSize; 129 static const int kBacktrackCount = kStringStartMinusOne - kSystemPointerSize; 130 // Stores the initial value of the regexp stack pointer in a 131 // position-independent representation (in case the regexp stack grows and 132 // thus moves). 133 static const int kRegExpStackBasePointer = 134 kBacktrackCount - kSystemPointerSize; 135 static constexpr int kNumberOfStackLocals = 4; 136 // First register address. Following registers are below it on the stack. 137 static const int kRegisterZero = kRegExpStackBasePointer - kSystemPointerSize; 138 139 // Initial size of code buffer. 140 static const int kRegExpCodeSize = 1024; 141 142 void PushCallerSavedRegisters(); 143 void PopCallerSavedRegisters(); 144 145 // Check whether preemption has been requested. 146 void CheckPreemption(); 147 148 // Check whether we are exceeding the stack limit on the backtrack stack. 149 void CheckStackLimit(); 150 151 void CallCheckStackGuardState(Register scratch); 152 void CallIsCharacterInRangeArray(const ZoneList<CharacterRange>* ranges); 153 154 // The ebp-relative location of a regexp register. 155 MemOperand register_location(int register_index); 156 157 // Register holding the current input position as negative offset from 158 // the end of the string. current_input_offset()159 static constexpr Register current_input_offset() { return s1; } 160 161 // The register containing the current character after LoadCurrentCharacter. current_character()162 static constexpr Register current_character() { return s2; } 163 164 // Register holding address of the end of the input string. end_of_input_address()165 static constexpr Register end_of_input_address() { return t2; } 166 167 // Register holding the frame address. Local variables, parameters and 168 // regexp registers are addressed relative to this. frame_pointer()169 static constexpr Register frame_pointer() { return fp; } 170 171 // The register containing the backtrack stack top. Provides a meaningful 172 // name to the register. backtrack_stackpointer()173 static constexpr Register backtrack_stackpointer() { return t0; } 174 175 // Register holding pointer to the current code object. code_pointer()176 static constexpr Register code_pointer() { return s4; } 177 178 // Byte size of chars in the string to match (decided by the Mode argument). char_size() const179 inline int char_size() const { return static_cast<int>(mode_); } 180 181 // Equivalent to a conditional branch to the label, unless the label 182 // is nullptr, in which case it is a conditional Backtrack. 183 void BranchOrBacktrack(Label* to, Condition condition, Register rs, 184 const Operand& rt); 185 186 // Call and return internally in the generated code in a way that 187 // is GC-safe (i.e., doesn't leave absolute code addresses on the stack) 188 inline void SafeCall(Label* to, Condition cond, Register rs, 189 const Operand& rt); 190 inline void SafeReturn(); 191 inline void SafeCallTarget(Label* name); 192 193 // Pushes the value of a register on the backtrack stack. Decrements the 194 // stack pointer by a word size and stores the register's value there. 195 inline void Push(Register source); 196 197 // Pops a value from the backtrack stack. Reads the word at the stack pointer 198 // and increments it by a word size. 199 inline void Pop(Register target); 200 201 void LoadRegExpStackPointerFromMemory(Register dst); 202 void StoreRegExpStackPointerToMemory(Register src, Register scratch); 203 void PushRegExpBasePointer(Register scratch1, Register scratch2); 204 void PopRegExpBasePointer(Register scratch1, Register scratch2); 205 isolate() const206 Isolate* isolate() const { return masm_->isolate(); } 207 208 const std::unique_ptr<MacroAssembler> masm_; 209 const NoRootArrayScope no_root_array_scope_; 210 211 // Which mode to generate code for (Latin1 or UC16). 212 const Mode mode_; 213 214 // One greater than maximal register index actually used. 215 int num_registers_; 216 217 // Number of registers to output at the end (the saved registers 218 // are always 0..num_saved_registers_-1). 219 const int num_saved_registers_; 220 221 // Labels used internally. 222 Label entry_label_; 223 Label start_label_; 224 Label success_label_; 225 Label backtrack_label_; 226 Label exit_label_; 227 Label check_preempt_label_; 228 Label stack_overflow_label_; 229 Label internal_failure_label_; 230 Label fallback_label_; 231 }; 232 233 } // namespace internal 234 } // namespace v8 235 236 #endif // V8_REGEXP_RISCV64_REGEXP_MACRO_ASSEMBLER_RISCV64_H_ 237