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