1 // Copyright 2012 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_X64_REGEXP_MACRO_ASSEMBLER_X64_H_ 6 #define V8_X64_REGEXP_MACRO_ASSEMBLER_X64_H_ 7 8 #include "src/x64/assembler-x64.h" 9 #include "src/x64/assembler-x64-inl.h" 10 #include "src/macro-assembler.h" 11 #include "src/code.h" 12 #include "src/x64/macro-assembler-x64.h" 13 14 namespace v8 { 15 namespace internal { 16 17 #ifndef V8_INTERPRETED_REGEXP 18 19 class RegExpMacroAssemblerX64: public NativeRegExpMacroAssembler { 20 public: 21 RegExpMacroAssemblerX64(Mode mode, int registers_to_save, Zone* zone); 22 virtual ~RegExpMacroAssemblerX64(); 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(uint32_t c, Label* on_equal); 30 virtual void CheckCharacterAfterAnd(uint32_t c, 31 uint32_t mask, 32 Label* on_equal); 33 virtual void CheckCharacterGT(uc16 limit, Label* on_greater); 34 virtual void CheckCharacterLT(uc16 limit, Label* on_less); 35 // A "greedy loop" is a loop that is both greedy and with a simple 36 // body. It has a particularly simple implementation. 37 virtual void CheckGreedyLoop(Label* on_tos_equals_current_position); 38 virtual void CheckNotAtStart(Label* on_not_at_start); 39 virtual void CheckNotBackReference(int start_reg, Label* on_no_match); 40 virtual void CheckNotBackReferenceIgnoreCase(int start_reg, 41 Label* on_no_match); 42 virtual void CheckNotCharacter(uint32_t c, Label* on_not_equal); 43 virtual void CheckNotCharacterAfterAnd(uint32_t c, 44 uint32_t mask, 45 Label* on_not_equal); 46 virtual void CheckNotCharacterAfterMinusAnd(uc16 c, 47 uc16 minus, 48 uc16 mask, 49 Label* on_not_equal); 50 virtual void CheckCharacterInRange(uc16 from, 51 uc16 to, 52 Label* on_in_range); 53 virtual void CheckCharacterNotInRange(uc16 from, 54 uc16 to, 55 Label* on_not_in_range); 56 virtual void CheckBitInTable(Handle<ByteArray> table, Label* on_bit_set); 57 58 // Checks whether the given offset from the current position is before 59 // the end of the string. 60 virtual void CheckPosition(int cp_offset, Label* on_outside_input); 61 virtual bool CheckSpecialCharacterClass(uc16 type, 62 Label* on_no_match); 63 virtual void Fail(); 64 virtual Handle<HeapObject> GetCode(Handle<String> source); 65 virtual void GoTo(Label* label); 66 virtual void IfRegisterGE(int reg, int comparand, Label* if_ge); 67 virtual void IfRegisterLT(int reg, int comparand, Label* if_lt); 68 virtual void IfRegisterEqPos(int reg, Label* if_eq); 69 virtual IrregexpImplementation Implementation(); 70 virtual void LoadCurrentCharacter(int cp_offset, 71 Label* on_end_of_input, 72 bool check_bounds = true, 73 int characters = 1); 74 virtual void PopCurrentPosition(); 75 virtual void PopRegister(int register_index); 76 virtual void PushBacktrack(Label* label); 77 virtual void PushCurrentPosition(); 78 virtual void PushRegister(int register_index, 79 StackCheckFlag check_stack_limit); 80 virtual void ReadCurrentPositionFromRegister(int reg); 81 virtual void ReadStackPointerFromRegister(int reg); 82 virtual void SetCurrentPositionFromEnd(int by); 83 virtual void SetRegister(int register_index, int to); 84 virtual bool Succeed(); 85 virtual void WriteCurrentPositionToRegister(int reg, int cp_offset); 86 virtual void ClearRegisters(int reg_from, int reg_to); 87 virtual void WriteStackPointerToRegister(int reg); 88 89 static Result Match(Handle<Code> regexp, 90 Handle<String> subject, 91 int* offsets_vector, 92 int offsets_vector_length, 93 int previous_index, 94 Isolate* isolate); 95 96 static Result Execute(Code* code, 97 String* input, 98 int start_offset, 99 const byte* input_start, 100 const byte* input_end, 101 int* output, 102 bool at_start); 103 104 // Called from RegExp if the stack-guard is triggered. 105 // If the code object is relocated, the return address is fixed before 106 // returning. 107 static int CheckStackGuardState(Address* return_address, 108 Code* re_code, 109 Address re_frame); 110 111 private: 112 // Offsets from rbp of function parameters and stored registers. 113 static const int kFramePointer = 0; 114 // Above the frame pointer - function parameters and return address. 115 static const int kReturn_eip = kFramePointer + kRegisterSize; 116 static const int kFrameAlign = kReturn_eip + kRegisterSize; 117 118 #ifdef _WIN64 119 // Parameters (first four passed as registers, but with room on stack). 120 // In Microsoft 64-bit Calling Convention, there is room on the callers 121 // stack (before the return address) to spill parameter registers. We 122 // use this space to store the register passed parameters. 123 static const int kInputString = kFrameAlign; 124 // StartIndex is passed as 32 bit int. 125 static const int kStartIndex = kInputString + kRegisterSize; 126 static const int kInputStart = kStartIndex + kRegisterSize; 127 static const int kInputEnd = kInputStart + kRegisterSize; 128 static const int kRegisterOutput = kInputEnd + kRegisterSize; 129 // For the case of global regular expression, we have room to store at least 130 // one set of capture results. For the case of non-global regexp, we ignore 131 // this value. NumOutputRegisters is passed as 32-bit value. The upper 132 // 32 bit of this 64-bit stack slot may contain garbage. 133 static const int kNumOutputRegisters = kRegisterOutput + kRegisterSize; 134 static const int kStackHighEnd = kNumOutputRegisters + kRegisterSize; 135 // DirectCall is passed as 32 bit int (values 0 or 1). 136 static const int kDirectCall = kStackHighEnd + kRegisterSize; 137 static const int kIsolate = kDirectCall + kRegisterSize; 138 #else 139 // In AMD64 ABI Calling Convention, the first six integer parameters 140 // are passed as registers, and caller must allocate space on the stack 141 // if it wants them stored. We push the parameters after the frame pointer. 142 static const int kInputString = kFramePointer - kRegisterSize; 143 static const int kStartIndex = kInputString - kRegisterSize; 144 static const int kInputStart = kStartIndex - kRegisterSize; 145 static const int kInputEnd = kInputStart - kRegisterSize; 146 static const int kRegisterOutput = kInputEnd - kRegisterSize; 147 148 // For the case of global regular expression, we have room to store at least 149 // one set of capture results. For the case of non-global regexp, we ignore 150 // this value. 151 static const int kNumOutputRegisters = kRegisterOutput - kRegisterSize; 152 static const int kStackHighEnd = kFrameAlign; 153 static const int kDirectCall = kStackHighEnd + kRegisterSize; 154 static const int kIsolate = kDirectCall + kRegisterSize; 155 #endif 156 157 #ifdef _WIN64 158 // Microsoft calling convention has three callee-saved registers 159 // (that we are using). We push these after the frame pointer. 160 static const int kBackup_rsi = kFramePointer - kRegisterSize; 161 static const int kBackup_rdi = kBackup_rsi - kRegisterSize; 162 static const int kBackup_rbx = kBackup_rdi - kRegisterSize; 163 static const int kLastCalleeSaveRegister = kBackup_rbx; 164 #else 165 // AMD64 Calling Convention has only one callee-save register that 166 // we use. We push this after the frame pointer (and after the 167 // parameters). 168 static const int kBackup_rbx = kNumOutputRegisters - kRegisterSize; 169 static const int kLastCalleeSaveRegister = kBackup_rbx; 170 #endif 171 172 static const int kSuccessfulCaptures = kLastCalleeSaveRegister - kPointerSize; 173 // When adding local variables remember to push space for them in 174 // the frame in GetCode. 175 static const int kInputStartMinusOne = kSuccessfulCaptures - kPointerSize; 176 177 // First register address. Following registers are below it on the stack. 178 static const int kRegisterZero = kInputStartMinusOne - kPointerSize; 179 180 // Initial size of code buffer. 181 static const size_t kRegExpCodeSize = 1024; 182 183 // Load a number of characters at the given offset from the 184 // current position, into the current-character register. 185 void LoadCurrentCharacterUnchecked(int cp_offset, int character_count); 186 187 // Check whether preemption has been requested. 188 void CheckPreemption(); 189 190 // Check whether we are exceeding the stack limit on the backtrack stack. 191 void CheckStackLimit(); 192 193 // Generate a call to CheckStackGuardState. 194 void CallCheckStackGuardState(); 195 196 // The rbp-relative location of a regexp register. 197 Operand register_location(int register_index); 198 199 // The register containing the current character after LoadCurrentCharacter. 200 inline Register current_character() { return rdx; } 201 202 // The register containing the backtrack stack top. Provides a meaningful 203 // name to the register. 204 inline Register backtrack_stackpointer() { return rcx; } 205 206 // The registers containing a self pointer to this code's Code object. 207 inline Register code_object_pointer() { return r8; } 208 209 // Byte size of chars in the string to match (decided by the Mode argument) 210 inline int char_size() { return static_cast<int>(mode_); } 211 212 // Equivalent to a conditional branch to the label, unless the label 213 // is NULL, in which case it is a conditional Backtrack. 214 void BranchOrBacktrack(Condition condition, Label* to); 215 216 void MarkPositionForCodeRelativeFixup() { 217 code_relative_fixup_positions_.Add(masm_.pc_offset(), zone()); 218 } 219 220 void FixupCodeRelativePositions(); 221 222 // Call and return internally in the generated code in a way that 223 // is GC-safe (i.e., doesn't leave absolute code addresses on the stack) 224 inline void SafeCall(Label* to); 225 inline void SafeCallTarget(Label* label); 226 inline void SafeReturn(); 227 228 // Pushes the value of a register on the backtrack stack. Decrements the 229 // stack pointer (rcx) by a word size and stores the register's value there. 230 inline void Push(Register source); 231 232 // Pushes a value on the backtrack stack. Decrements the stack pointer (rcx) 233 // by a word size and stores the value there. 234 inline void Push(Immediate value); 235 236 // Pushes the Code object relative offset of a label on the backtrack stack 237 // (i.e., a backtrack target). Decrements the stack pointer (rcx) 238 // by a word size and stores the value there. 239 inline void Push(Label* label); 240 241 // Pops a value from the backtrack stack. Reads the word at the stack pointer 242 // (rcx) and increments it by a word size. 243 inline void Pop(Register target); 244 245 // Drops the top value from the backtrack stack without reading it. 246 // Increments the stack pointer (rcx) by a word size. 247 inline void Drop(); 248 249 inline void ReadPositionFromRegister(Register dst, int reg); 250 251 Isolate* isolate() const { return masm_.isolate(); } 252 253 MacroAssembler masm_; 254 MacroAssembler::NoRootArrayScope no_root_array_scope_; 255 256 ZoneList<int> code_relative_fixup_positions_; 257 258 // Which mode to generate code for (ASCII or UC16). 259 Mode mode_; 260 261 // One greater than maximal register index actually used. 262 int num_registers_; 263 264 // Number of registers to output at the end (the saved registers 265 // are always 0..num_saved_registers_-1) 266 int num_saved_registers_; 267 268 // Labels used internally. 269 Label entry_label_; 270 Label start_label_; 271 Label success_label_; 272 Label backtrack_label_; 273 Label exit_label_; 274 Label check_preempt_label_; 275 Label stack_overflow_label_; 276 }; 277 278 #endif // V8_INTERPRETED_REGEXP 279 280 }} // namespace v8::internal 281 282 #endif // V8_X64_REGEXP_MACRO_ASSEMBLER_X64_H_ 283