ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 1 | // Copyright 2009 the V8 project authors. All rights reserved. |
| 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 28 | #ifndef V8_X64_REGEXP_MACRO_ASSEMBLER_X64_H_ |
| 29 | #define V8_X64_REGEXP_MACRO_ASSEMBLER_X64_H_ |
| 30 | |
| 31 | namespace v8 { |
| 32 | namespace internal { |
| 33 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 34 | #ifdef V8_NATIVE_REGEXP |
| 35 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 36 | class RegExpMacroAssemblerX64: public NativeRegExpMacroAssembler { |
| 37 | public: |
| 38 | RegExpMacroAssemblerX64(Mode mode, int registers_to_save); |
| 39 | virtual ~RegExpMacroAssemblerX64(); |
| 40 | virtual int stack_limit_slack(); |
| 41 | virtual void AdvanceCurrentPosition(int by); |
| 42 | virtual void AdvanceRegister(int reg, int by); |
| 43 | virtual void Backtrack(); |
| 44 | virtual void Bind(Label* label); |
| 45 | virtual void CheckAtStart(Label* on_at_start); |
| 46 | virtual void CheckCharacter(uint32_t c, Label* on_equal); |
| 47 | virtual void CheckCharacterAfterAnd(uint32_t c, |
| 48 | uint32_t mask, |
| 49 | Label* on_equal); |
| 50 | virtual void CheckCharacterGT(uc16 limit, Label* on_greater); |
| 51 | virtual void CheckCharacterLT(uc16 limit, Label* on_less); |
| 52 | virtual void CheckCharacters(Vector<const uc16> str, |
| 53 | int cp_offset, |
| 54 | Label* on_failure, |
| 55 | bool check_end_of_string); |
| 56 | // A "greedy loop" is a loop that is both greedy and with a simple |
| 57 | // body. It has a particularly simple implementation. |
| 58 | virtual void CheckGreedyLoop(Label* on_tos_equals_current_position); |
| 59 | virtual void CheckNotAtStart(Label* on_not_at_start); |
| 60 | virtual void CheckNotBackReference(int start_reg, Label* on_no_match); |
| 61 | virtual void CheckNotBackReferenceIgnoreCase(int start_reg, |
| 62 | Label* on_no_match); |
| 63 | virtual void CheckNotRegistersEqual(int reg1, int reg2, Label* on_not_equal); |
| 64 | virtual void CheckNotCharacter(uint32_t c, Label* on_not_equal); |
| 65 | virtual void CheckNotCharacterAfterAnd(uint32_t c, |
| 66 | uint32_t mask, |
| 67 | Label* on_not_equal); |
| 68 | virtual void CheckNotCharacterAfterMinusAnd(uc16 c, |
| 69 | uc16 minus, |
| 70 | uc16 mask, |
| 71 | Label* on_not_equal); |
| 72 | // Checks whether the given offset from the current position is before |
| 73 | // the end of the string. |
| 74 | virtual void CheckPosition(int cp_offset, Label* on_outside_input); |
| 75 | virtual bool CheckSpecialCharacterClass(uc16 type, |
| 76 | int cp_offset, |
| 77 | bool check_offset, |
| 78 | Label* on_no_match); |
| 79 | virtual void Fail(); |
| 80 | virtual Handle<Object> GetCode(Handle<String> source); |
| 81 | virtual void GoTo(Label* label); |
| 82 | virtual void IfRegisterGE(int reg, int comparand, Label* if_ge); |
| 83 | virtual void IfRegisterLT(int reg, int comparand, Label* if_lt); |
| 84 | virtual void IfRegisterEqPos(int reg, Label* if_eq); |
| 85 | virtual IrregexpImplementation Implementation(); |
| 86 | virtual void LoadCurrentCharacter(int cp_offset, |
| 87 | Label* on_end_of_input, |
| 88 | bool check_bounds = true, |
| 89 | int characters = 1); |
| 90 | virtual void PopCurrentPosition(); |
| 91 | virtual void PopRegister(int register_index); |
| 92 | virtual void PushBacktrack(Label* label); |
| 93 | virtual void PushCurrentPosition(); |
| 94 | virtual void PushRegister(int register_index, |
| 95 | StackCheckFlag check_stack_limit); |
| 96 | virtual void ReadCurrentPositionFromRegister(int reg); |
| 97 | virtual void ReadStackPointerFromRegister(int reg); |
| 98 | virtual void SetRegister(int register_index, int to); |
| 99 | virtual void Succeed(); |
| 100 | virtual void WriteCurrentPositionToRegister(int reg, int cp_offset); |
| 101 | virtual void ClearRegisters(int reg_from, int reg_to); |
| 102 | virtual void WriteStackPointerToRegister(int reg); |
| 103 | |
| 104 | static Result Match(Handle<Code> regexp, |
| 105 | Handle<String> subject, |
| 106 | int* offsets_vector, |
| 107 | int offsets_vector_length, |
| 108 | int previous_index); |
| 109 | |
| 110 | static Result Execute(Code* code, |
| 111 | String* input, |
| 112 | int start_offset, |
| 113 | const byte* input_start, |
| 114 | const byte* input_end, |
| 115 | int* output, |
| 116 | bool at_start); |
| 117 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 118 | // Called from RegExp if the stack-guard is triggered. |
| 119 | // If the code object is relocated, the return address is fixed before |
| 120 | // returning. |
| 121 | static int CheckStackGuardState(Address* return_address, |
| 122 | Code* re_code, |
| 123 | Address re_frame); |
| 124 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 125 | private: |
| 126 | // Offsets from rbp of function parameters and stored registers. |
| 127 | static const int kFramePointer = 0; |
| 128 | // Above the frame pointer - function parameters and return address. |
| 129 | static const int kReturn_eip = kFramePointer + kPointerSize; |
| 130 | static const int kFrameAlign = kReturn_eip + kPointerSize; |
| 131 | |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 132 | #ifdef _WIN64 |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 133 | // Parameters (first four passed as registers, but with room on stack). |
| 134 | // In Microsoft 64-bit Calling Convention, there is room on the callers |
| 135 | // stack (before the return address) to spill parameter registers. We |
| 136 | // use this space to store the register passed parameters. |
| 137 | static const int kInputString = kFrameAlign; |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 138 | // StartIndex is passed as 32 bit int. |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 139 | static const int kStartIndex = kInputString + kPointerSize; |
| 140 | static const int kInputStart = kStartIndex + kPointerSize; |
| 141 | static const int kInputEnd = kInputStart + kPointerSize; |
| 142 | static const int kRegisterOutput = kInputEnd + kPointerSize; |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 143 | // AtStart is passed as 32 bit int (values 0 or 1). |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 144 | static const int kAtStart = kRegisterOutput + kPointerSize; |
| 145 | static const int kStackHighEnd = kAtStart + kPointerSize; |
| 146 | #else |
| 147 | // In AMD64 ABI Calling Convention, the first six integer parameters |
| 148 | // are passed as registers, and caller must allocate space on the stack |
| 149 | // if it wants them stored. We push the parameters after the frame pointer. |
| 150 | static const int kInputString = kFramePointer - kPointerSize; |
| 151 | static const int kStartIndex = kInputString - kPointerSize; |
| 152 | static const int kInputStart = kStartIndex - kPointerSize; |
| 153 | static const int kInputEnd = kInputStart - kPointerSize; |
| 154 | static const int kRegisterOutput = kInputEnd - kPointerSize; |
| 155 | static const int kAtStart = kRegisterOutput - kPointerSize; |
| 156 | static const int kStackHighEnd = kFrameAlign; |
| 157 | #endif |
| 158 | |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 159 | #ifdef _WIN64 |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 160 | // Microsoft calling convention has three callee-saved registers |
| 161 | // (that we are using). We push these after the frame pointer. |
| 162 | static const int kBackup_rsi = kFramePointer - kPointerSize; |
| 163 | static const int kBackup_rdi = kBackup_rsi - kPointerSize; |
| 164 | static const int kBackup_rbx = kBackup_rdi - kPointerSize; |
| 165 | static const int kLastCalleeSaveRegister = kBackup_rbx; |
| 166 | #else |
| 167 | // AMD64 Calling Convention has only one callee-save register that |
| 168 | // we use. We push this after the frame pointer (and after the |
| 169 | // parameters). |
| 170 | static const int kBackup_rbx = kAtStart - kPointerSize; |
| 171 | static const int kLastCalleeSaveRegister = kBackup_rbx; |
| 172 | #endif |
| 173 | |
| 174 | // When adding local variables remember to push space for them in |
| 175 | // the frame in GetCode. |
| 176 | static const int kInputStartMinusOne = |
| 177 | kLastCalleeSaveRegister - kPointerSize; |
| 178 | |
| 179 | // First register address. Following registers are below it on the stack. |
| 180 | static const int kRegisterZero = kInputStartMinusOne - kPointerSize; |
| 181 | |
| 182 | // Initial size of code buffer. |
| 183 | static const size_t kRegExpCodeSize = 1024; |
| 184 | |
| 185 | // Load a number of characters at the given offset from the |
| 186 | // current position, into the current-character register. |
| 187 | void LoadCurrentCharacterUnchecked(int cp_offset, int character_count); |
| 188 | |
| 189 | // Check whether preemption has been requested. |
| 190 | void CheckPreemption(); |
| 191 | |
| 192 | // Check whether we are exceeding the stack limit on the backtrack stack. |
| 193 | void CheckStackLimit(); |
| 194 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 195 | // Generate a call to CheckStackGuardState. |
| 196 | void CallCheckStackGuardState(); |
| 197 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 198 | // The rbp-relative location of a regexp register. |
| 199 | Operand register_location(int register_index); |
| 200 | |
| 201 | // The register containing the current character after LoadCurrentCharacter. |
| 202 | inline Register current_character() { return rdx; } |
| 203 | |
| 204 | // The register containing the backtrack stack top. Provides a meaningful |
| 205 | // name to the register. |
| 206 | inline Register backtrack_stackpointer() { return rcx; } |
| 207 | |
| 208 | // The registers containing a self pointer to this code's Code object. |
| 209 | inline Register code_object_pointer() { return r8; } |
| 210 | |
| 211 | // Byte size of chars in the string to match (decided by the Mode argument) |
| 212 | inline int char_size() { return static_cast<int>(mode_); } |
| 213 | |
| 214 | // Equivalent to a conditional branch to the label, unless the label |
| 215 | // is NULL, in which case it is a conditional Backtrack. |
| 216 | void BranchOrBacktrack(Condition condition, Label* to); |
| 217 | |
| 218 | void MarkPositionForCodeRelativeFixup() { |
| 219 | code_relative_fixup_positions_.Add(masm_->pc_offset()); |
| 220 | } |
| 221 | |
| 222 | void FixupCodeRelativePositions(); |
| 223 | |
| 224 | // Call and return internally in the generated code in a way that |
| 225 | // is GC-safe (i.e., doesn't leave absolute code addresses on the stack) |
| 226 | inline void SafeCall(Label* to); |
| 227 | inline void SafeCallTarget(Label* label); |
| 228 | inline void SafeReturn(); |
| 229 | |
| 230 | // Pushes the value of a register on the backtrack stack. Decrements the |
| 231 | // stack pointer (rcx) by a word size and stores the register's value there. |
| 232 | inline void Push(Register source); |
| 233 | |
| 234 | // Pushes a value on the backtrack stack. Decrements the stack pointer (rcx) |
| 235 | // by a word size and stores the value there. |
| 236 | inline void Push(Immediate value); |
| 237 | |
| 238 | // Pushes the Code object relative offset of a label on the backtrack stack |
| 239 | // (i.e., a backtrack target). Decrements the stack pointer (rcx) |
| 240 | // by a word size and stores the value there. |
| 241 | inline void Push(Label* label); |
| 242 | |
| 243 | // Pops a value from the backtrack stack. Reads the word at the stack pointer |
| 244 | // (rcx) and increments it by a word size. |
| 245 | inline void Pop(Register target); |
| 246 | |
| 247 | // Drops the top value from the backtrack stack without reading it. |
| 248 | // Increments the stack pointer (rcx) by a word size. |
| 249 | inline void Drop(); |
| 250 | |
| 251 | // Before calling a C-function from generated code, align arguments on stack. |
| 252 | // After aligning the frame, arguments must be stored in esp[0], esp[4], |
| 253 | // etc., not pushed. The argument count assumes all arguments are word sized. |
| 254 | // Some compilers/platforms require the stack to be aligned when calling |
| 255 | // C++ code. |
| 256 | // Needs a scratch register to do some arithmetic. This register will be |
| 257 | // trashed. |
| 258 | inline void FrameAlign(int num_arguments); |
| 259 | |
| 260 | // Calls a C function and cleans up the space for arguments allocated |
| 261 | // by FrameAlign. The called function is not allowed to trigger a garbage |
| 262 | // collection, since that might move the code and invalidate the return |
| 263 | // address (unless this is somehow accounted for by the called function). |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 264 | inline void CallCFunction(ExternalReference function, int num_arguments); |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 265 | |
| 266 | MacroAssembler* masm_; |
| 267 | |
| 268 | ZoneList<int> code_relative_fixup_positions_; |
| 269 | |
| 270 | // Which mode to generate code for (ASCII or UC16). |
| 271 | Mode mode_; |
| 272 | |
| 273 | // One greater than maximal register index actually used. |
| 274 | int num_registers_; |
| 275 | |
| 276 | // Number of registers to output at the end (the saved registers |
| 277 | // are always 0..num_saved_registers_-1) |
| 278 | int num_saved_registers_; |
| 279 | |
| 280 | // Labels used internally. |
| 281 | Label entry_label_; |
| 282 | Label start_label_; |
| 283 | Label success_label_; |
| 284 | Label backtrack_label_; |
| 285 | Label exit_label_; |
| 286 | Label check_preempt_label_; |
| 287 | Label stack_overflow_label_; |
| 288 | }; |
| 289 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 290 | #endif // V8_NATIVE_REGEXP |
| 291 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 292 | }} // namespace v8::internal |
| 293 | |
| 294 | #endif // V8_X64_REGEXP_MACRO_ASSEMBLER_X64_H_ |