1 // Copyright 2013 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_ARM64_REGEXP_MACRO_ASSEMBLER_ARM64_H_ 6 #define V8_REGEXP_ARM64_REGEXP_MACRO_ASSEMBLER_ARM64_H_ 7 8 #include "src/codegen/arm64/assembler-arm64.h" 9 #include "src/codegen/macro-assembler.h" 10 #include "src/regexp/regexp-macro-assembler.h" 11 12 namespace v8 { 13 namespace internal { 14 15 class V8_EXPORT_PRIVATE RegExpMacroAssemblerARM64 16 : public NativeRegExpMacroAssembler { 17 public: 18 RegExpMacroAssemblerARM64(Isolate* isolate, Zone* zone, Mode mode, 19 int registers_to_save); 20 virtual ~RegExpMacroAssemblerARM64(); AbortedCodeGeneration()21 virtual void AbortedCodeGeneration() { masm_->AbortedCodeGeneration(); } 22 virtual int stack_limit_slack(); 23 virtual void AdvanceCurrentPosition(int by); 24 virtual void AdvanceRegister(int reg, int by); 25 virtual void Backtrack(); 26 virtual void Bind(Label* label); 27 virtual void CheckAtStart(int cp_offset, Label* on_at_start); 28 virtual void CheckCharacter(unsigned c, Label* on_equal); 29 virtual void CheckCharacterAfterAnd(unsigned c, 30 unsigned mask, 31 Label* on_equal); 32 virtual void CheckCharacterGT(uc16 limit, Label* on_greater); 33 virtual void CheckCharacterLT(uc16 limit, Label* on_less); 34 virtual void CheckCharacters(Vector<const uc16> str, 35 int cp_offset, 36 Label* on_failure, 37 bool check_end_of_string); 38 // A "greedy loop" is a loop that is both greedy and with a simple 39 // body. It has a particularly simple implementation. 40 virtual void CheckGreedyLoop(Label* on_tos_equals_current_position); 41 virtual void CheckNotAtStart(int cp_offset, Label* on_not_at_start); 42 virtual void CheckNotBackReference(int start_reg, bool read_backward, 43 Label* on_no_match); 44 virtual void CheckNotBackReferenceIgnoreCase(int start_reg, 45 bool read_backward, bool unicode, 46 Label* on_no_match); 47 virtual void CheckNotCharacter(unsigned c, Label* on_not_equal); 48 virtual void CheckNotCharacterAfterAnd(unsigned c, 49 unsigned mask, 50 Label* on_not_equal); 51 virtual void CheckNotCharacterAfterMinusAnd(uc16 c, 52 uc16 minus, 53 uc16 mask, 54 Label* on_not_equal); 55 virtual void CheckCharacterInRange(uc16 from, 56 uc16 to, 57 Label* on_in_range); 58 virtual void CheckCharacterNotInRange(uc16 from, 59 uc16 to, 60 Label* on_not_in_range); 61 virtual void CheckBitInTable(Handle<ByteArray> table, Label* on_bit_set); 62 63 // Checks whether the given offset from the current position is before 64 // the end of the string. 65 virtual void CheckPosition(int cp_offset, Label* on_outside_input); 66 virtual bool CheckSpecialCharacterClass(uc16 type, 67 Label* on_no_match); 68 virtual void BindJumpTarget(Label* label = nullptr); 69 virtual void Fail(); 70 virtual Handle<HeapObject> GetCode(Handle<String> source); 71 virtual void GoTo(Label* label); 72 virtual void IfRegisterGE(int reg, int comparand, Label* if_ge); 73 virtual void IfRegisterLT(int reg, int comparand, Label* if_lt); 74 virtual void IfRegisterEqPos(int reg, Label* if_eq); 75 virtual IrregexpImplementation Implementation(); 76 virtual void LoadCurrentCharacterUnchecked(int cp_offset, 77 int character_count); 78 virtual void PopCurrentPosition(); 79 virtual void PopRegister(int register_index); 80 virtual void PushBacktrack(Label* label); 81 virtual void PushCurrentPosition(); 82 virtual void PushRegister(int register_index, 83 StackCheckFlag check_stack_limit); 84 virtual void ReadCurrentPositionFromRegister(int reg); 85 virtual void ReadStackPointerFromRegister(int reg); 86 virtual void SetCurrentPositionFromEnd(int by); 87 virtual void SetRegister(int register_index, int to); 88 virtual bool Succeed(); 89 virtual void WriteCurrentPositionToRegister(int reg, int cp_offset); 90 virtual void ClearRegisters(int reg_from, int reg_to); 91 virtual void WriteStackPointerToRegister(int reg); 92 93 // Called from RegExp if the stack-guard is triggered. 94 // If the code object is relocated, the return address is fixed before 95 // returning. 96 // {raw_code} is an Address because this is called via ExternalReference. 97 static int CheckStackGuardState(Address* return_address, Address raw_code, 98 Address re_frame, int start_offset, 99 const byte** input_start, 100 const byte** input_end); 101 102 private: 103 // Above the frame pointer - Stored registers and stack passed parameters. 104 // Callee-saved registers x19-x29, where x29 is the old frame pointer. 105 static const int kCalleeSavedRegisters = 0; 106 // Return address. 107 // It is placed above the 11 callee-saved registers. 108 static const int kReturnAddress = 109 kCalleeSavedRegisters + 11 * kSystemPointerSize; 110 // Stack parameter placed by caller. 111 static const int kIsolate = kReturnAddress + kSystemPointerSize; 112 113 // Below the frame pointer. 114 // Register parameters stored by setup code. 115 static const int kDirectCall = kCalleeSavedRegisters - kSystemPointerSize; 116 static const int kStackBase = kDirectCall - kSystemPointerSize; 117 static const int kOutputSize = kStackBase - kSystemPointerSize; 118 static const int kInput = kOutputSize - kSystemPointerSize; 119 // When adding local variables remember to push space for them in 120 // the frame in GetCode. 121 static const int kSuccessCounter = kInput - kSystemPointerSize; 122 static const int kBacktrackCount = kSuccessCounter - kSystemPointerSize; 123 // First position register address on the stack. Following positions are 124 // below it. A position is a 32 bit value. 125 static const int kFirstRegisterOnStack = kBacktrackCount - kWRegSize; 126 // A capture is a 64 bit value holding two position. 127 static const int kFirstCaptureOnStack = kBacktrackCount - kXRegSize; 128 129 // Initial size of code buffer. 130 static const int kRegExpCodeSize = 1024; 131 132 // When initializing registers to a non-position value we can unroll 133 // the loop. Set the limit of registers to unroll. 134 static const int kNumRegistersToUnroll = 16; 135 136 // We are using x0 to x7 as a register cache. Each hardware register must 137 // contain one capture, that is two 32 bit registers. We can cache at most 138 // 16 registers. 139 static const int kNumCachedRegisters = 16; 140 141 // Check whether preemption has been requested. 142 void CheckPreemption(); 143 144 // Check whether we are exceeding the stack limit on the backtrack stack. 145 void CheckStackLimit(); 146 147 // Generate a call to CheckStackGuardState. 148 void CallCheckStackGuardState(Register scratch); 149 150 // Location of a 32 bit position register. 151 MemOperand register_location(int register_index); 152 153 // Location of a 64 bit capture, combining two position registers. 154 MemOperand capture_location(int register_index, Register scratch); 155 156 // Register holding the current input position as negative offset from 157 // the end of the string. current_input_offset()158 Register current_input_offset() { return w21; } 159 160 // The register containing the current character after LoadCurrentCharacter. current_character()161 Register current_character() { return w22; } 162 163 // Register holding address of the end of the input string. input_end()164 Register input_end() { return x25; } 165 166 // Register holding address of the start of the input string. input_start()167 Register input_start() { return x26; } 168 169 // Register holding the offset from the start of the string where we should 170 // start matching. start_offset()171 Register start_offset() { return w27; } 172 173 // Pointer to the output array's first element. output_array()174 Register output_array() { return x28; } 175 176 // Register holding the frame address. Local variables, parameters and 177 // regexp registers are addressed relative to this. frame_pointer()178 Register frame_pointer() { return fp; } 179 180 // The register containing the backtrack stack top. Provides a meaningful 181 // name to the register. backtrack_stackpointer()182 Register backtrack_stackpointer() { return x23; } 183 184 // Register holding pointer to the current code object. code_pointer()185 Register code_pointer() { return x20; } 186 187 // Register holding the value used for clearing capture registers. string_start_minus_one()188 Register string_start_minus_one() { return w24; } 189 // The top 32 bit of this register is used to store this value 190 // twice. This is used for clearing more than one register at a time. twice_non_position_value()191 Register twice_non_position_value() { return x24; } 192 193 // Byte size of chars in the string to match (decided by the Mode argument) char_size()194 int char_size() { return static_cast<int>(mode_); } 195 196 // Equivalent to a conditional branch to the label, unless the label 197 // is nullptr, in which case it is a conditional Backtrack. 198 void BranchOrBacktrack(Condition condition, Label* to); 199 200 // Compares reg against immmediate before calling BranchOrBacktrack. 201 // It makes use of the Cbz and Cbnz instructions. 202 void CompareAndBranchOrBacktrack(Register reg, 203 int immediate, 204 Condition condition, 205 Label* to); 206 207 inline void CallIf(Label* to, Condition condition); 208 209 // Save and restore the link register on the stack in a way that 210 // is GC-safe. 211 inline void SaveLinkRegister(); 212 inline void RestoreLinkRegister(); 213 214 // Pushes the value of a register on the backtrack stack. Decrements the 215 // stack pointer by a word size and stores the register's value there. 216 inline void Push(Register source); 217 218 // Pops a value from the backtrack stack. Reads the word at the stack pointer 219 // and increments it by a word size. 220 inline void Pop(Register target); 221 222 // This state indicates where the register actually is. 223 enum RegisterState { 224 STACKED, // Resides in memory. 225 CACHED_LSW, // Least Significant Word of a 64 bit hardware register. 226 CACHED_MSW // Most Significant Word of a 64 bit hardware register. 227 }; 228 GetRegisterState(int register_index)229 RegisterState GetRegisterState(int register_index) { 230 DCHECK_LE(0, register_index); 231 if (register_index >= kNumCachedRegisters) { 232 return STACKED; 233 } else { 234 if ((register_index % 2) == 0) { 235 return CACHED_LSW; 236 } else { 237 return CACHED_MSW; 238 } 239 } 240 } 241 242 // Store helper that takes the state of the register into account. 243 inline void StoreRegister(int register_index, Register source); 244 245 // Returns a hardware W register that holds the value of the capture 246 // register. 247 // 248 // This function will try to use an existing cache register (w0-w7) for the 249 // result. Otherwise, it will load the value into maybe_result. 250 // 251 // If the returned register is anything other than maybe_result, calling code 252 // must not write to it. 253 inline Register GetRegister(int register_index, Register maybe_result); 254 255 // Returns the harware register (x0-x7) holding the value of the capture 256 // register. 257 // This assumes that the state of the register is not STACKED. 258 inline Register GetCachedRegister(int register_index); 259 isolate()260 Isolate* isolate() const { return masm_->isolate(); } 261 262 MacroAssembler* masm_; 263 264 // Which mode to generate code for (LATIN1 or UC16). 265 Mode mode_; 266 267 // One greater than maximal register index actually used. 268 int num_registers_; 269 270 // Number of registers to output at the end (the saved registers 271 // are always 0..num_saved_registers_-1) 272 int num_saved_registers_; 273 274 // Labels used internally. 275 Label entry_label_; 276 Label start_label_; 277 Label success_label_; 278 Label backtrack_label_; 279 Label exit_label_; 280 Label check_preempt_label_; 281 Label stack_overflow_label_; 282 Label fallback_label_; 283 }; 284 285 } // namespace internal 286 } // namespace v8 287 288 #endif // V8_REGEXP_ARM64_REGEXP_MACRO_ASSEMBLER_ARM64_H_ 289