| OLD | NEW |
| (Empty) |
| 1 // Copyright 2008 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 | |
| 28 // A light-weight assembler for the Irregexp byte code. | |
| 29 | |
| 30 | |
| 31 #include "v8.h" | |
| 32 #include "ast.h" | |
| 33 #include "bytecodes-irregexp.h" | |
| 34 #include "assembler-irregexp.h" | |
| 35 | |
| 36 #include "assembler-irregexp-inl.h" | |
| 37 | |
| 38 | |
| 39 namespace v8 { namespace internal { | |
| 40 | |
| 41 | |
| 42 IrregexpAssembler::IrregexpAssembler(Vector<byte> buffer) | |
| 43 : buffer_(buffer), | |
| 44 pc_(0), | |
| 45 own_buffer_(false) { | |
| 46 } | |
| 47 | |
| 48 | |
| 49 IrregexpAssembler::~IrregexpAssembler() { | |
| 50 if (own_buffer_) { | |
| 51 buffer_.Dispose(); | |
| 52 } | |
| 53 } | |
| 54 | |
| 55 | |
| 56 void IrregexpAssembler::PushCurrentPosition(int cp_offset) { | |
| 57 ASSERT(cp_offset >= 0); | |
| 58 Emit(BC_PUSH_CP); | |
| 59 Emit32(cp_offset); | |
| 60 } | |
| 61 | |
| 62 | |
| 63 void IrregexpAssembler::PushBacktrack(Label* l) { | |
| 64 Emit(BC_PUSH_BT); | |
| 65 EmitOrLink(l); | |
| 66 } | |
| 67 | |
| 68 | |
| 69 void IrregexpAssembler::PushRegister(int index) { | |
| 70 ASSERT(index >= 0); | |
| 71 Emit(BC_PUSH_REGISTER); | |
| 72 Emit(index); | |
| 73 } | |
| 74 | |
| 75 | |
| 76 void IrregexpAssembler::WriteCurrentPositionToRegister(int index, | |
| 77 int cp_offset) { | |
| 78 ASSERT(cp_offset >= 0); | |
| 79 ASSERT(index >= 0); | |
| 80 Emit(BC_SET_REGISTER_TO_CP); | |
| 81 Emit(index); | |
| 82 Emit32(cp_offset); | |
| 83 } | |
| 84 | |
| 85 | |
| 86 void IrregexpAssembler::ReadCurrentPositionFromRegister(int index) { | |
| 87 ASSERT(index >= 0); | |
| 88 Emit(BC_SET_CP_TO_REGISTER); | |
| 89 Emit(index); | |
| 90 } | |
| 91 | |
| 92 | |
| 93 void IrregexpAssembler::WriteStackPointerToRegister(int index) { | |
| 94 ASSERT(index >= 0); | |
| 95 Emit(BC_SET_REGISTER_TO_SP); | |
| 96 Emit(index); | |
| 97 } | |
| 98 | |
| 99 | |
| 100 void IrregexpAssembler::ReadStackPointerFromRegister(int index) { | |
| 101 ASSERT(index >= 0); | |
| 102 Emit(BC_SET_SP_TO_REGISTER); | |
| 103 Emit(index); | |
| 104 } | |
| 105 | |
| 106 | |
| 107 void IrregexpAssembler::SetRegister(int index, int value) { | |
| 108 ASSERT(index >= 0); | |
| 109 Emit(BC_SET_REGISTER); | |
| 110 Emit(index); | |
| 111 Emit32(value); | |
| 112 } | |
| 113 | |
| 114 | |
| 115 void IrregexpAssembler::AdvanceRegister(int index, int by) { | |
| 116 ASSERT(index >= 0); | |
| 117 Emit(BC_ADVANCE_REGISTER); | |
| 118 Emit(index); | |
| 119 Emit32(by); | |
| 120 } | |
| 121 | |
| 122 | |
| 123 void IrregexpAssembler::PopCurrentPosition() { | |
| 124 Emit(BC_POP_CP); | |
| 125 } | |
| 126 | |
| 127 | |
| 128 void IrregexpAssembler::PopBacktrack() { | |
| 129 Emit(BC_POP_BT); | |
| 130 } | |
| 131 | |
| 132 | |
| 133 void IrregexpAssembler::PopRegister(int index) { | |
| 134 Emit(BC_POP_REGISTER); | |
| 135 Emit(index); | |
| 136 } | |
| 137 | |
| 138 | |
| 139 void IrregexpAssembler::Fail() { | |
| 140 Emit(BC_FAIL); | |
| 141 } | |
| 142 | |
| 143 | |
| 144 void IrregexpAssembler::Break() { | |
| 145 Emit(BC_BREAK); | |
| 146 } | |
| 147 | |
| 148 | |
| 149 void IrregexpAssembler::Succeed() { | |
| 150 Emit(BC_SUCCEED); | |
| 151 } | |
| 152 | |
| 153 | |
| 154 void IrregexpAssembler::Bind(Label* l) { | |
| 155 ASSERT(!l->is_bound()); | |
| 156 if (l->is_linked()) { | |
| 157 int pos = l->pos(); | |
| 158 while (pos != 0) { | |
| 159 int fixup = pos; | |
| 160 pos = Load32(buffer_.start() + fixup); | |
| 161 Store32(buffer_.start() + fixup, pc_); | |
| 162 } | |
| 163 } | |
| 164 l->bind_to(pc_); | |
| 165 } | |
| 166 | |
| 167 | |
| 168 void IrregexpAssembler::AdvanceCP(int cp_offset) { | |
| 169 Emit(BC_ADVANCE_CP); | |
| 170 Emit32(cp_offset); | |
| 171 } | |
| 172 | |
| 173 | |
| 174 void IrregexpAssembler::GoTo(Label* l) { | |
| 175 Emit(BC_GOTO); | |
| 176 EmitOrLink(l); | |
| 177 } | |
| 178 | |
| 179 | |
| 180 void IrregexpAssembler::LoadCurrentChar(int cp_offset, Label* on_end) { | |
| 181 Emit(BC_LOAD_CURRENT_CHAR); | |
| 182 Emit32(cp_offset); | |
| 183 EmitOrLink(on_end); | |
| 184 } | |
| 185 | |
| 186 | |
| 187 void IrregexpAssembler::CheckCharacter(uc16 c, Label* on_match) { | |
| 188 Emit(BC_CHECK_CHAR); | |
| 189 Emit16(c); | |
| 190 EmitOrLink(on_match); | |
| 191 } | |
| 192 | |
| 193 | |
| 194 void IrregexpAssembler::CheckNotCharacter(uc16 c, Label* on_mismatch) { | |
| 195 Emit(BC_CHECK_NOT_CHAR); | |
| 196 Emit16(c); | |
| 197 EmitOrLink(on_mismatch); | |
| 198 } | |
| 199 | |
| 200 void IrregexpAssembler::OrThenCheckNotCharacter(uc16 c, | |
| 201 uc16 mask, | |
| 202 Label* on_mismatch) { | |
| 203 Emit(BC_OR_CHECK_NOT_CHAR); | |
| 204 Emit16(c); | |
| 205 Emit16(mask); | |
| 206 EmitOrLink(on_mismatch); | |
| 207 } | |
| 208 | |
| 209 | |
| 210 void IrregexpAssembler::MinusOrThenCheckNotCharacter(uc16 c, | |
| 211 uc16 mask, | |
| 212 Label* on_mismatch) { | |
| 213 Emit(BC_MINUS_OR_CHECK_NOT_CHAR); | |
| 214 Emit16(c); | |
| 215 Emit16(mask); | |
| 216 EmitOrLink(on_mismatch); | |
| 217 } | |
| 218 | |
| 219 | |
| 220 void IrregexpAssembler::CheckCharacterLT(uc16 limit, Label* on_less) { | |
| 221 Emit(BC_CHECK_LT); | |
| 222 Emit16(limit); | |
| 223 EmitOrLink(on_less); | |
| 224 } | |
| 225 | |
| 226 | |
| 227 void IrregexpAssembler::CheckCharacterGT(uc16 limit, Label* on_greater) { | |
| 228 Emit(BC_CHECK_GT); | |
| 229 Emit16(limit); | |
| 230 EmitOrLink(on_greater); | |
| 231 } | |
| 232 | |
| 233 | |
| 234 void IrregexpAssembler::CheckNotBackReference(int capture_index, | |
| 235 Label* on_mismatch) { | |
| 236 Emit(BC_CHECK_NOT_BACK_REF); | |
| 237 Emit(capture_index); | |
| 238 EmitOrLink(on_mismatch); | |
| 239 } | |
| 240 | |
| 241 | |
| 242 void IrregexpAssembler::CheckNotBackReferenceNoCase(int capture_index, | |
| 243 Label* on_mismatch) { | |
| 244 Emit(BC_CHECK_NOT_BACK_REF_NO_CASE); | |
| 245 Emit(capture_index); | |
| 246 EmitOrLink(on_mismatch); | |
| 247 } | |
| 248 | |
| 249 | |
| 250 void IrregexpAssembler::CheckNotRegistersEqual(int reg1, | |
| 251 int reg2, | |
| 252 Label* on_not_equal) { | |
| 253 Emit(BC_CHECK_NOT_REGS_EQUAL); | |
| 254 Emit(reg1); | |
| 255 Emit(reg2); | |
| 256 EmitOrLink(on_not_equal); | |
| 257 } | |
| 258 | |
| 259 | |
| 260 void IrregexpAssembler::CheckRegister(int byte_code, | |
| 261 int reg_index, | |
| 262 uint16_t vs, | |
| 263 Label* on_true) { | |
| 264 Emit(byte_code); | |
| 265 Emit(reg_index); | |
| 266 Emit16(vs); | |
| 267 EmitOrLink(on_true); | |
| 268 } | |
| 269 | |
| 270 | |
| 271 void IrregexpAssembler::CheckRegisterLT(int reg_index, | |
| 272 uint16_t vs, | |
| 273 Label* on_less_than) { | |
| 274 CheckRegister(BC_CHECK_REGISTER_LT, reg_index, vs, on_less_than); | |
| 275 } | |
| 276 | |
| 277 | |
| 278 void IrregexpAssembler::CheckRegisterGE(int reg_index, | |
| 279 uint16_t vs, | |
| 280 Label* on_greater_than_equal) { | |
| 281 CheckRegister(BC_CHECK_REGISTER_GE, reg_index, vs, on_greater_than_equal); | |
| 282 } | |
| 283 | |
| 284 | |
| 285 void IrregexpAssembler::LookupMap1(uc16 start, Label* bit_map, Label* on_zero) { | |
| 286 Emit(BC_LOOKUP_MAP1); | |
| 287 Emit16(start); | |
| 288 EmitOrLink(bit_map); | |
| 289 EmitOrLink(on_zero); | |
| 290 } | |
| 291 | |
| 292 | |
| 293 void IrregexpAssembler::LookupMap2(uc16 start, | |
| 294 Label* half_nibble_map, | |
| 295 const Vector<Label*>& table) { | |
| 296 Emit(BC_LOOKUP_MAP2); | |
| 297 Emit16(start); | |
| 298 EmitOrLink(half_nibble_map); | |
| 299 ASSERT(table.length() > 0); | |
| 300 ASSERT(table.length() <= 4); | |
| 301 for (int i = 0; i < table.length(); i++) { | |
| 302 EmitOrLink(table[i]); | |
| 303 } | |
| 304 } | |
| 305 | |
| 306 | |
| 307 void IrregexpAssembler::LookupMap8(uc16 start, | |
| 308 Label* byte_map, | |
| 309 const Vector<Label*>& table) { | |
| 310 Emit(BC_LOOKUP_MAP8); | |
| 311 Emit16(start); | |
| 312 EmitOrLink(byte_map); | |
| 313 ASSERT(table.length() > 0); | |
| 314 ASSERT(table.length() <= 256); | |
| 315 for (int i = 0; i < table.length(); i++) { | |
| 316 EmitOrLink(table[i]); | |
| 317 } | |
| 318 } | |
| 319 | |
| 320 | |
| 321 void IrregexpAssembler::LookupHighMap8(byte start, | |
| 322 Label* byte_map, | |
| 323 const Vector<Label*>& table) { | |
| 324 Emit(BC_LOOKUP_HI_MAP8); | |
| 325 Emit(start); | |
| 326 EmitOrLink(byte_map); | |
| 327 ASSERT(table.length() > 0); | |
| 328 ASSERT(table.length() <= 256); | |
| 329 for (int i = 0; i < table.length(); i++) { | |
| 330 EmitOrLink(table[i]); | |
| 331 } | |
| 332 } | |
| 333 | |
| 334 | |
| 335 int IrregexpAssembler::length() { | |
| 336 return pc_; | |
| 337 } | |
| 338 | |
| 339 | |
| 340 void IrregexpAssembler::Copy(Address a) { | |
| 341 memcpy(a, buffer_.start(), length()); | |
| 342 } | |
| 343 | |
| 344 | |
| 345 void IrregexpAssembler::Expand() { | |
| 346 bool old_buffer_was_our_own = own_buffer_; | |
| 347 Vector<byte> old_buffer = buffer_; | |
| 348 buffer_ = Vector<byte>::New(old_buffer.length() * 2); | |
| 349 own_buffer_ = true; | |
| 350 memcpy(buffer_.start(), old_buffer.start(), old_buffer.length()); | |
| 351 if (old_buffer_was_our_own) { | |
| 352 old_buffer.Dispose(); | |
| 353 } | |
| 354 } | |
| 355 | |
| 356 | |
| 357 } } // namespace v8::internal | |
| OLD | NEW |