ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1 | // Copyright 2008-2009 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 | |
| 28 | #include "v8.h" |
| 29 | #include "ast.h" |
| 30 | #include "bytecodes-irregexp.h" |
| 31 | #include "regexp-macro-assembler.h" |
| 32 | #include "regexp-macro-assembler-irregexp.h" |
| 33 | #include "regexp-macro-assembler-irregexp-inl.h" |
| 34 | |
| 35 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 36 | namespace v8 { |
| 37 | namespace internal { |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 38 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 39 | #ifdef V8_INTERPRETED_REGEXP |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 40 | |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 41 | RegExpMacroAssemblerIrregexp::RegExpMacroAssemblerIrregexp(Vector<byte> buffer, |
| 42 | Zone* zone) |
| 43 | : RegExpMacroAssembler(zone), |
| 44 | buffer_(buffer), |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 45 | pc_(0), |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 46 | own_buffer_(false), |
| 47 | advance_current_end_(kInvalidPC) { |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | |
| 51 | RegExpMacroAssemblerIrregexp::~RegExpMacroAssemblerIrregexp() { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 52 | if (backtrack_.is_linked()) backtrack_.Unuse(); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 53 | if (own_buffer_) buffer_.Dispose(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | |
| 57 | RegExpMacroAssemblerIrregexp::IrregexpImplementation |
| 58 | RegExpMacroAssemblerIrregexp::Implementation() { |
| 59 | return kBytecodeImplementation; |
| 60 | } |
| 61 | |
| 62 | |
| 63 | void RegExpMacroAssemblerIrregexp::Bind(Label* l) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 64 | advance_current_end_ = kInvalidPC; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 65 | ASSERT(!l->is_bound()); |
| 66 | if (l->is_linked()) { |
| 67 | int pos = l->pos(); |
| 68 | while (pos != 0) { |
| 69 | int fixup = pos; |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 70 | pos = *reinterpret_cast<int32_t*>(buffer_.start() + fixup); |
| 71 | *reinterpret_cast<uint32_t*>(buffer_.start() + fixup) = pc_; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | l->bind_to(pc_); |
| 75 | } |
| 76 | |
| 77 | |
| 78 | void RegExpMacroAssemblerIrregexp::EmitOrLink(Label* l) { |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 79 | if (l == NULL) l = &backtrack_; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 80 | if (l->is_bound()) { |
| 81 | Emit32(l->pos()); |
| 82 | } else { |
| 83 | int pos = 0; |
| 84 | if (l->is_linked()) { |
| 85 | pos = l->pos(); |
| 86 | } |
| 87 | l->link_to(pc_); |
| 88 | Emit32(pos); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | |
| 93 | void RegExpMacroAssemblerIrregexp::PopRegister(int register_index) { |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 94 | ASSERT(register_index >= 0); |
| 95 | ASSERT(register_index <= kMaxRegister); |
| 96 | Emit(BC_POP_REGISTER, register_index); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 100 | void RegExpMacroAssemblerIrregexp::PushRegister( |
| 101 | int register_index, |
| 102 | StackCheckFlag check_stack_limit) { |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 103 | ASSERT(register_index >= 0); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 104 | ASSERT(register_index <= kMaxRegister); |
| 105 | Emit(BC_PUSH_REGISTER, register_index); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | |
| 109 | void RegExpMacroAssemblerIrregexp::WriteCurrentPositionToRegister( |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 110 | int register_index, int cp_offset) { |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 111 | ASSERT(register_index >= 0); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 112 | ASSERT(register_index <= kMaxRegister); |
| 113 | Emit(BC_SET_REGISTER_TO_CP, register_index); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 114 | Emit32(cp_offset); // Current position offset. |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 118 | void RegExpMacroAssemblerIrregexp::ClearRegisters(int reg_from, int reg_to) { |
| 119 | ASSERT(reg_from <= reg_to); |
| 120 | for (int reg = reg_from; reg <= reg_to; reg++) { |
| 121 | SetRegister(reg, -1); |
| 122 | } |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 126 | void RegExpMacroAssemblerIrregexp::ReadCurrentPositionFromRegister( |
| 127 | int register_index) { |
| 128 | ASSERT(register_index >= 0); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 129 | ASSERT(register_index <= kMaxRegister); |
| 130 | Emit(BC_SET_CP_TO_REGISTER, register_index); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | |
| 134 | void RegExpMacroAssemblerIrregexp::WriteStackPointerToRegister( |
| 135 | int register_index) { |
| 136 | ASSERT(register_index >= 0); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 137 | ASSERT(register_index <= kMaxRegister); |
| 138 | Emit(BC_SET_REGISTER_TO_SP, register_index); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | |
| 142 | void RegExpMacroAssemblerIrregexp::ReadStackPointerFromRegister( |
| 143 | int register_index) { |
| 144 | ASSERT(register_index >= 0); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 145 | ASSERT(register_index <= kMaxRegister); |
| 146 | Emit(BC_SET_SP_TO_REGISTER, register_index); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 150 | void RegExpMacroAssemblerIrregexp::SetCurrentPositionFromEnd(int by) { |
| 151 | ASSERT(is_uint24(by)); |
| 152 | Emit(BC_SET_CURRENT_POSITION_FROM_END, by); |
| 153 | } |
| 154 | |
| 155 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 156 | void RegExpMacroAssemblerIrregexp::SetRegister(int register_index, int to) { |
| 157 | ASSERT(register_index >= 0); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 158 | ASSERT(register_index <= kMaxRegister); |
| 159 | Emit(BC_SET_REGISTER, register_index); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 160 | Emit32(to); |
| 161 | } |
| 162 | |
| 163 | |
| 164 | void RegExpMacroAssemblerIrregexp::AdvanceRegister(int register_index, int by) { |
| 165 | ASSERT(register_index >= 0); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 166 | ASSERT(register_index <= kMaxRegister); |
| 167 | Emit(BC_ADVANCE_REGISTER, register_index); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 168 | Emit32(by); |
| 169 | } |
| 170 | |
| 171 | |
| 172 | void RegExpMacroAssemblerIrregexp::PopCurrentPosition() { |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 173 | Emit(BC_POP_CP, 0); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | |
| 177 | void RegExpMacroAssemblerIrregexp::PushCurrentPosition() { |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 178 | Emit(BC_PUSH_CP, 0); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | |
| 182 | void RegExpMacroAssemblerIrregexp::Backtrack() { |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 183 | Emit(BC_POP_BT, 0); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | |
| 187 | void RegExpMacroAssemblerIrregexp::GoTo(Label* l) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 188 | if (advance_current_end_ == pc_) { |
| 189 | // Combine advance current and goto. |
| 190 | pc_ = advance_current_start_; |
| 191 | Emit(BC_ADVANCE_CP_AND_GOTO, advance_current_offset_); |
| 192 | EmitOrLink(l); |
| 193 | advance_current_end_ = kInvalidPC; |
| 194 | } else { |
| 195 | // Regular goto. |
| 196 | Emit(BC_GOTO, 0); |
| 197 | EmitOrLink(l); |
| 198 | } |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | |
| 202 | void RegExpMacroAssemblerIrregexp::PushBacktrack(Label* l) { |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 203 | Emit(BC_PUSH_BT, 0); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 204 | EmitOrLink(l); |
| 205 | } |
| 206 | |
| 207 | |
mstarzinger@chromium.org | 15613d0 | 2012-05-23 12:04:37 +0000 | [diff] [blame] | 208 | bool RegExpMacroAssemblerIrregexp::Succeed() { |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 209 | Emit(BC_SUCCEED, 0); |
mstarzinger@chromium.org | 15613d0 | 2012-05-23 12:04:37 +0000 | [diff] [blame] | 210 | return false; // Restart matching for global regexp not supported. |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | |
| 214 | void RegExpMacroAssemblerIrregexp::Fail() { |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 215 | Emit(BC_FAIL, 0); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | |
| 219 | void RegExpMacroAssemblerIrregexp::AdvanceCurrentPosition(int by) { |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 220 | ASSERT(by >= kMinCPOffset); |
| 221 | ASSERT(by <= kMaxCPOffset); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 222 | advance_current_start_ = pc_; |
| 223 | advance_current_offset_ = by; |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 224 | Emit(BC_ADVANCE_CP, by); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 225 | advance_current_end_ = pc_; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 229 | void RegExpMacroAssemblerIrregexp::CheckGreedyLoop( |
| 230 | Label* on_tos_equals_current_position) { |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 231 | Emit(BC_CHECK_GREEDY, 0); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 232 | EmitOrLink(on_tos_equals_current_position); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | |
| 236 | void RegExpMacroAssemblerIrregexp::LoadCurrentCharacter(int cp_offset, |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 237 | Label* on_failure, |
| 238 | bool check_bounds, |
| 239 | int characters) { |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 240 | ASSERT(cp_offset >= kMinCPOffset); |
| 241 | ASSERT(cp_offset <= kMaxCPOffset); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 242 | int bytecode; |
| 243 | if (check_bounds) { |
| 244 | if (characters == 4) { |
| 245 | bytecode = BC_LOAD_4_CURRENT_CHARS; |
| 246 | } else if (characters == 2) { |
| 247 | bytecode = BC_LOAD_2_CURRENT_CHARS; |
| 248 | } else { |
| 249 | ASSERT(characters == 1); |
| 250 | bytecode = BC_LOAD_CURRENT_CHAR; |
| 251 | } |
| 252 | } else { |
| 253 | if (characters == 4) { |
| 254 | bytecode = BC_LOAD_4_CURRENT_CHARS_UNCHECKED; |
| 255 | } else if (characters == 2) { |
| 256 | bytecode = BC_LOAD_2_CURRENT_CHARS_UNCHECKED; |
| 257 | } else { |
| 258 | ASSERT(characters == 1); |
| 259 | bytecode = BC_LOAD_CURRENT_CHAR_UNCHECKED; |
| 260 | } |
| 261 | } |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 262 | Emit(bytecode, cp_offset); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 263 | if (check_bounds) EmitOrLink(on_failure); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 267 | void RegExpMacroAssemblerIrregexp::CheckCharacterLT(uc16 limit, |
| 268 | Label* on_less) { |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 269 | Emit(BC_CHECK_LT, limit); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 270 | EmitOrLink(on_less); |
| 271 | } |
| 272 | |
| 273 | |
| 274 | void RegExpMacroAssemblerIrregexp::CheckCharacterGT(uc16 limit, |
| 275 | Label* on_greater) { |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 276 | Emit(BC_CHECK_GT, limit); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 277 | EmitOrLink(on_greater); |
| 278 | } |
| 279 | |
| 280 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 281 | void RegExpMacroAssemblerIrregexp::CheckCharacter(uint32_t c, Label* on_equal) { |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 282 | if (c > MAX_FIRST_ARG) { |
| 283 | Emit(BC_CHECK_4_CHARS, 0); |
| 284 | Emit32(c); |
| 285 | } else { |
| 286 | Emit(BC_CHECK_CHAR, c); |
| 287 | } |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 288 | EmitOrLink(on_equal); |
| 289 | } |
| 290 | |
| 291 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 292 | void RegExpMacroAssemblerIrregexp::CheckAtStart(Label* on_at_start) { |
| 293 | Emit(BC_CHECK_AT_START, 0); |
| 294 | EmitOrLink(on_at_start); |
| 295 | } |
| 296 | |
| 297 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 298 | void RegExpMacroAssemblerIrregexp::CheckNotAtStart(Label* on_not_at_start) { |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 299 | Emit(BC_CHECK_NOT_AT_START, 0); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 300 | EmitOrLink(on_not_at_start); |
| 301 | } |
| 302 | |
| 303 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 304 | void RegExpMacroAssemblerIrregexp::CheckNotCharacter(uint32_t c, |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 305 | Label* on_not_equal) { |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 306 | if (c > MAX_FIRST_ARG) { |
| 307 | Emit(BC_CHECK_NOT_4_CHARS, 0); |
| 308 | Emit32(c); |
| 309 | } else { |
| 310 | Emit(BC_CHECK_NOT_CHAR, c); |
| 311 | } |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 312 | EmitOrLink(on_not_equal); |
| 313 | } |
| 314 | |
| 315 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 316 | void RegExpMacroAssemblerIrregexp::CheckCharacterAfterAnd( |
| 317 | uint32_t c, |
| 318 | uint32_t mask, |
| 319 | Label* on_equal) { |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 320 | if (c > MAX_FIRST_ARG) { |
| 321 | Emit(BC_AND_CHECK_4_CHARS, 0); |
| 322 | Emit32(c); |
| 323 | } else { |
| 324 | Emit(BC_AND_CHECK_CHAR, c); |
| 325 | } |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 326 | Emit32(mask); |
| 327 | EmitOrLink(on_equal); |
| 328 | } |
| 329 | |
| 330 | |
| 331 | void RegExpMacroAssemblerIrregexp::CheckNotCharacterAfterAnd( |
| 332 | uint32_t c, |
| 333 | uint32_t mask, |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 334 | Label* on_not_equal) { |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 335 | if (c > MAX_FIRST_ARG) { |
| 336 | Emit(BC_AND_CHECK_NOT_4_CHARS, 0); |
| 337 | Emit32(c); |
| 338 | } else { |
| 339 | Emit(BC_AND_CHECK_NOT_CHAR, c); |
| 340 | } |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 341 | Emit32(mask); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 342 | EmitOrLink(on_not_equal); |
| 343 | } |
| 344 | |
| 345 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 346 | void RegExpMacroAssemblerIrregexp::CheckNotCharacterAfterMinusAnd( |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 347 | uc16 c, |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 348 | uc16 minus, |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 349 | uc16 mask, |
| 350 | Label* on_not_equal) { |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 351 | Emit(BC_MINUS_AND_CHECK_NOT_CHAR, c); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 352 | Emit16(minus); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 353 | Emit16(mask); |
| 354 | EmitOrLink(on_not_equal); |
| 355 | } |
| 356 | |
| 357 | |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 358 | void RegExpMacroAssemblerIrregexp::CheckCharacterInRange( |
| 359 | uc16 from, |
| 360 | uc16 to, |
| 361 | Label* on_in_range) { |
| 362 | Emit(BC_CHECK_CHAR_IN_RANGE, 0); |
| 363 | Emit16(from); |
| 364 | Emit16(to); |
| 365 | EmitOrLink(on_in_range); |
| 366 | } |
| 367 | |
| 368 | |
| 369 | void RegExpMacroAssemblerIrregexp::CheckCharacterNotInRange( |
| 370 | uc16 from, |
| 371 | uc16 to, |
| 372 | Label* on_not_in_range) { |
| 373 | Emit(BC_CHECK_CHAR_NOT_IN_RANGE, 0); |
| 374 | Emit16(from); |
| 375 | Emit16(to); |
| 376 | EmitOrLink(on_not_in_range); |
| 377 | } |
| 378 | |
| 379 | |
| 380 | void RegExpMacroAssemblerIrregexp::CheckBitInTable( |
| 381 | Handle<ByteArray> table, Label* on_bit_set) { |
| 382 | Emit(BC_CHECK_BIT_IN_TABLE, 0); |
| 383 | EmitOrLink(on_bit_set); |
| 384 | for (int i = 0; i < kTableSize; i += kBitsPerByte) { |
| 385 | int byte = 0; |
| 386 | for (int j = 0; j < kBitsPerByte; j++) { |
| 387 | if (table->get(i + j) != 0) byte |= 1 << j; |
| 388 | } |
| 389 | Emit8(byte); |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 394 | void RegExpMacroAssemblerIrregexp::CheckNotBackReference(int start_reg, |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 395 | Label* on_not_equal) { |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 396 | ASSERT(start_reg >= 0); |
| 397 | ASSERT(start_reg <= kMaxRegister); |
| 398 | Emit(BC_CHECK_NOT_BACK_REF, start_reg); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 399 | EmitOrLink(on_not_equal); |
| 400 | } |
| 401 | |
| 402 | |
| 403 | void RegExpMacroAssemblerIrregexp::CheckNotBackReferenceIgnoreCase( |
| 404 | int start_reg, |
| 405 | Label* on_not_equal) { |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 406 | ASSERT(start_reg >= 0); |
| 407 | ASSERT(start_reg <= kMaxRegister); |
| 408 | Emit(BC_CHECK_NOT_BACK_REF_NO_CASE, start_reg); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 409 | EmitOrLink(on_not_equal); |
| 410 | } |
| 411 | |
| 412 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 413 | void RegExpMacroAssemblerIrregexp::CheckCharacters( |
| 414 | Vector<const uc16> str, |
| 415 | int cp_offset, |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 416 | Label* on_failure, |
| 417 | bool check_end_of_string) { |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 418 | ASSERT(cp_offset >= kMinCPOffset); |
| 419 | ASSERT(cp_offset + str.length() - 1 <= kMaxCPOffset); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 420 | // It is vital that this loop is backwards due to the unchecked character |
| 421 | // load below. |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 422 | for (int i = str.length() - 1; i >= 0; i--) { |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 423 | if (check_end_of_string && i == str.length() - 1) { |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 424 | Emit(BC_LOAD_CURRENT_CHAR, cp_offset + i); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 425 | EmitOrLink(on_failure); |
| 426 | } else { |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 427 | Emit(BC_LOAD_CURRENT_CHAR_UNCHECKED, cp_offset + i); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 428 | } |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 429 | Emit(BC_CHECK_NOT_CHAR, str[i]); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 430 | EmitOrLink(on_failure); |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | |
| 435 | void RegExpMacroAssemblerIrregexp::IfRegisterLT(int register_index, |
| 436 | int comparand, |
| 437 | Label* on_less_than) { |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 438 | ASSERT(register_index >= 0); |
| 439 | ASSERT(register_index <= kMaxRegister); |
| 440 | Emit(BC_CHECK_REGISTER_LT, register_index); |
| 441 | Emit32(comparand); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 442 | EmitOrLink(on_less_than); |
| 443 | } |
| 444 | |
| 445 | |
| 446 | void RegExpMacroAssemblerIrregexp::IfRegisterGE(int register_index, |
| 447 | int comparand, |
| 448 | Label* on_greater_or_equal) { |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 449 | ASSERT(register_index >= 0); |
| 450 | ASSERT(register_index <= kMaxRegister); |
| 451 | Emit(BC_CHECK_REGISTER_GE, register_index); |
| 452 | Emit32(comparand); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 453 | EmitOrLink(on_greater_or_equal); |
| 454 | } |
| 455 | |
| 456 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 457 | void RegExpMacroAssemblerIrregexp::IfRegisterEqPos(int register_index, |
| 458 | Label* on_eq) { |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 459 | ASSERT(register_index >= 0); |
| 460 | ASSERT(register_index <= kMaxRegister); |
| 461 | Emit(BC_CHECK_REGISTER_EQ_POS, register_index); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 462 | EmitOrLink(on_eq); |
| 463 | } |
| 464 | |
| 465 | |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 466 | Handle<HeapObject> RegExpMacroAssemblerIrregexp::GetCode( |
| 467 | Handle<String> source) { |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 468 | Bind(&backtrack_); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 469 | Emit(BC_POP_BT, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 470 | Handle<ByteArray> array = FACTORY->NewByteArray(length()); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 471 | Copy(array->GetDataStartAddress()); |
| 472 | return array; |
| 473 | } |
| 474 | |
| 475 | |
| 476 | int RegExpMacroAssemblerIrregexp::length() { |
| 477 | return pc_; |
| 478 | } |
| 479 | |
| 480 | |
| 481 | void RegExpMacroAssemblerIrregexp::Copy(Address a) { |
| 482 | memcpy(a, buffer_.start(), length()); |
| 483 | } |
| 484 | |
| 485 | |
| 486 | void RegExpMacroAssemblerIrregexp::Expand() { |
| 487 | bool old_buffer_was_our_own = own_buffer_; |
| 488 | Vector<byte> old_buffer = buffer_; |
| 489 | buffer_ = Vector<byte>::New(old_buffer.length() * 2); |
| 490 | own_buffer_ = true; |
| 491 | memcpy(buffer_.start(), old_buffer.start(), old_buffer.length()); |
| 492 | if (old_buffer_was_our_own) { |
| 493 | old_buffer.Dispose(); |
| 494 | } |
| 495 | } |
| 496 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 497 | #endif // V8_INTERPRETED_REGEXP |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 498 | |
| 499 | } } // namespace v8::internal |