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