mstarzinger@chromium.org | 15613d0 | 2012-05-23 12:04:37 +0000 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 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 | |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 28 | #ifndef V8_IA32_REGEXP_MACRO_ASSEMBLER_IA32_H_ |
| 29 | #define V8_IA32_REGEXP_MACRO_ASSEMBLER_IA32_H_ |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 30 | |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 31 | #include "ia32/assembler-ia32.h" |
| 32 | #include "ia32/assembler-ia32-inl.h" |
ulan@chromium.org | 32d7dba | 2013-04-24 10:59:06 +0000 | [diff] [blame] | 33 | #include "macro-assembler.h" |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 34 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 35 | namespace v8 { |
| 36 | namespace internal { |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 37 | |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 38 | #ifndef V8_INTERPRETED_REGEXP |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 39 | class RegExpMacroAssemblerIA32: public NativeRegExpMacroAssembler { |
| 40 | public: |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 41 | RegExpMacroAssemblerIA32(Mode mode, int registers_to_save, Zone* zone); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 42 | virtual ~RegExpMacroAssemblerIA32(); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 43 | virtual int stack_limit_slack(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 44 | virtual void AdvanceCurrentPosition(int by); |
| 45 | virtual void AdvanceRegister(int reg, int by); |
| 46 | virtual void Backtrack(); |
| 47 | virtual void Bind(Label* label); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 48 | virtual void CheckAtStart(Label* on_at_start); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 49 | virtual void CheckCharacter(uint32_t c, Label* on_equal); |
| 50 | virtual void CheckCharacterAfterAnd(uint32_t c, |
| 51 | uint32_t mask, |
| 52 | Label* on_equal); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 53 | virtual void CheckCharacterGT(uc16 limit, Label* on_greater); |
| 54 | virtual void CheckCharacterLT(uc16 limit, Label* on_less); |
| 55 | virtual void CheckCharacters(Vector<const uc16> str, |
| 56 | int cp_offset, |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 57 | Label* on_failure, |
| 58 | bool check_end_of_string); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 59 | // A "greedy loop" is a loop that is both greedy and with a simple |
| 60 | // body. It has a particularly simple implementation. |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 61 | virtual void CheckGreedyLoop(Label* on_tos_equals_current_position); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 62 | virtual void CheckNotAtStart(Label* on_not_at_start); |
| 63 | virtual void CheckNotBackReference(int start_reg, Label* on_no_match); |
| 64 | virtual void CheckNotBackReferenceIgnoreCase(int start_reg, |
| 65 | Label* on_no_match); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 66 | virtual void CheckNotCharacter(uint32_t c, Label* on_not_equal); |
| 67 | virtual void CheckNotCharacterAfterAnd(uint32_t c, |
| 68 | uint32_t mask, |
| 69 | Label* on_not_equal); |
| 70 | virtual void CheckNotCharacterAfterMinusAnd(uc16 c, |
| 71 | uc16 minus, |
| 72 | uc16 mask, |
| 73 | Label* on_not_equal); |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 74 | virtual void CheckCharacterInRange(uc16 from, |
| 75 | uc16 to, |
| 76 | Label* on_in_range); |
| 77 | virtual void CheckCharacterNotInRange(uc16 from, |
| 78 | uc16 to, |
| 79 | Label* on_not_in_range); |
| 80 | virtual void CheckBitInTable(Handle<ByteArray> table, Label* on_bit_set); |
| 81 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 82 | // Checks whether the given offset from the current position is before |
| 83 | // the end of the string. |
| 84 | virtual void CheckPosition(int cp_offset, Label* on_outside_input); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 85 | virtual bool CheckSpecialCharacterClass(uc16 type, Label* on_no_match); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 86 | virtual void Fail(); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 87 | virtual Handle<HeapObject> GetCode(Handle<String> source); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 88 | virtual void GoTo(Label* label); |
| 89 | virtual void IfRegisterGE(int reg, int comparand, Label* if_ge); |
| 90 | virtual void IfRegisterLT(int reg, int comparand, Label* if_lt); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 91 | virtual void IfRegisterEqPos(int reg, Label* if_eq); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 92 | virtual IrregexpImplementation Implementation(); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 93 | virtual void LoadCurrentCharacter(int cp_offset, |
| 94 | Label* on_end_of_input, |
| 95 | bool check_bounds = true, |
| 96 | int characters = 1); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 97 | virtual void PopCurrentPosition(); |
| 98 | virtual void PopRegister(int register_index); |
| 99 | virtual void PushBacktrack(Label* label); |
| 100 | virtual void PushCurrentPosition(); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 101 | virtual void PushRegister(int register_index, |
| 102 | StackCheckFlag check_stack_limit); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 103 | virtual void ReadCurrentPositionFromRegister(int reg); |
| 104 | virtual void ReadStackPointerFromRegister(int reg); |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 105 | virtual void SetCurrentPositionFromEnd(int by); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 106 | virtual void SetRegister(int register_index, int to); |
mstarzinger@chromium.org | 15613d0 | 2012-05-23 12:04:37 +0000 | [diff] [blame] | 107 | virtual bool Succeed(); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 108 | virtual void WriteCurrentPositionToRegister(int reg, int cp_offset); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 109 | virtual void ClearRegisters(int reg_from, int reg_to); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 110 | virtual void WriteStackPointerToRegister(int reg); |
| 111 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 112 | // Called from RegExp if the stack-guard is triggered. |
| 113 | // If the code object is relocated, the return address is fixed before |
| 114 | // returning. |
| 115 | static int CheckStackGuardState(Address* return_address, |
| 116 | Code* re_code, |
| 117 | Address re_frame); |
| 118 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 119 | private: |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 120 | // Offsets from ebp of function parameters and stored registers. |
| 121 | static const int kFramePointer = 0; |
| 122 | // Above the frame pointer - function parameters and return address. |
| 123 | static const int kReturn_eip = kFramePointer + kPointerSize; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 124 | static const int kFrameAlign = kReturn_eip + kPointerSize; |
| 125 | // Parameters. |
| 126 | static const int kInputString = kFrameAlign; |
| 127 | static const int kStartIndex = kInputString + kPointerSize; |
| 128 | static const int kInputStart = kStartIndex + kPointerSize; |
| 129 | static const int kInputEnd = kInputStart + kPointerSize; |
| 130 | static const int kRegisterOutput = kInputEnd + kPointerSize; |
mstarzinger@chromium.org | 15613d0 | 2012-05-23 12:04:37 +0000 | [diff] [blame] | 131 | // For the case of global regular expression, we have room to store at least |
| 132 | // one set of capture results. For the case of non-global regexp, we ignore |
| 133 | // this value. |
| 134 | static const int kNumOutputRegisters = kRegisterOutput + kPointerSize; |
| 135 | static const int kStackHighEnd = kNumOutputRegisters + kPointerSize; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 136 | static const int kDirectCall = kStackHighEnd + kPointerSize; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 137 | static const int kIsolate = kDirectCall + kPointerSize; |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 138 | // Below the frame pointer - local stack variables. |
| 139 | // When adding local variables remember to push space for them in |
| 140 | // the frame in GetCode. |
| 141 | static const int kBackup_esi = kFramePointer - kPointerSize; |
| 142 | static const int kBackup_edi = kBackup_esi - kPointerSize; |
| 143 | static const int kBackup_ebx = kBackup_edi - kPointerSize; |
mstarzinger@chromium.org | 15613d0 | 2012-05-23 12:04:37 +0000 | [diff] [blame] | 144 | static const int kSuccessfulCaptures = kBackup_ebx - kPointerSize; |
| 145 | static const int kInputStartMinusOne = kSuccessfulCaptures - kPointerSize; |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 146 | // First register address. Following registers are below it on the stack. |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 147 | static const int kRegisterZero = kInputStartMinusOne - kPointerSize; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 148 | |
| 149 | // Initial size of code buffer. |
| 150 | static const size_t kRegExpCodeSize = 1024; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 151 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 152 | // Load a number of characters at the given offset from the |
| 153 | // current position, into the current-character register. |
| 154 | void LoadCurrentCharacterUnchecked(int cp_offset, int character_count); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 155 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 156 | // Check whether preemption has been requested. |
| 157 | void CheckPreemption(); |
| 158 | |
| 159 | // Check whether we are exceeding the stack limit on the backtrack stack. |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 160 | void CheckStackLimit(); |
| 161 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 162 | // Generate a call to CheckStackGuardState. |
| 163 | void CallCheckStackGuardState(Register scratch); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 164 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 165 | // The ebp-relative location of a regexp register. |
| 166 | Operand register_location(int register_index); |
| 167 | |
| 168 | // The register containing the current character after LoadCurrentCharacter. |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 169 | inline Register current_character() { return edx; } |
| 170 | |
| 171 | // The register containing the backtrack stack top. Provides a meaningful |
| 172 | // name to the register. |
| 173 | inline Register backtrack_stackpointer() { return ecx; } |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 174 | |
| 175 | // Byte size of chars in the string to match (decided by the Mode argument) |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 176 | inline int char_size() { return static_cast<int>(mode_); } |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 177 | |
| 178 | // Equivalent to a conditional branch to the label, unless the label |
| 179 | // is NULL, in which case it is a conditional Backtrack. |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 180 | void BranchOrBacktrack(Condition condition, Label* to); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 181 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 182 | // Call and return internally in the generated code in a way that |
| 183 | // is GC-safe (i.e., doesn't leave absolute code addresses on the stack) |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 184 | inline void SafeCall(Label* to); |
| 185 | inline void SafeReturn(); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 186 | inline void SafeCallTarget(Label* name); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 187 | |
| 188 | // Pushes the value of a register on the backtrack stack. Decrements the |
| 189 | // stack pointer (ecx) by a word size and stores the register's value there. |
| 190 | inline void Push(Register source); |
| 191 | |
| 192 | // Pushes a value on the backtrack stack. Decrements the stack pointer (ecx) |
| 193 | // by a word size and stores the value there. |
| 194 | inline void Push(Immediate value); |
| 195 | |
| 196 | // Pops a value from the backtrack stack. Reads the word at the stack pointer |
| 197 | // (ecx) and increments it by a word size. |
| 198 | inline void Pop(Register target); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 199 | |
ulan@chromium.org | 32d7dba | 2013-04-24 10:59:06 +0000 | [diff] [blame] | 200 | Isolate* isolate() const { return masm_->isolate(); } |
| 201 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 202 | MacroAssembler* masm_; |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 203 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 204 | // Which mode to generate code for (ASCII or UC16). |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 205 | Mode mode_; |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 206 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 207 | // One greater than maximal register index actually used. |
| 208 | int num_registers_; |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 209 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 210 | // Number of registers to output at the end (the saved registers |
| 211 | // are always 0..num_saved_registers_-1) |
| 212 | int num_saved_registers_; |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 213 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 214 | // Labels used internally. |
| 215 | Label entry_label_; |
| 216 | Label start_label_; |
| 217 | Label success_label_; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 218 | Label backtrack_label_; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 219 | Label exit_label_; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 220 | Label check_preempt_label_; |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 221 | Label stack_overflow_label_; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 222 | }; |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 223 | #endif // V8_INTERPRETED_REGEXP |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 224 | |
| 225 | }} // namespace v8::internal |
| 226 | |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 227 | #endif // V8_IA32_REGEXP_MACRO_ASSEMBLER_IA32_H_ |