OLD | NEW |
(Empty) | |
| 1 // Copyright 2012 the V8 project authors. All rights reserved. |
| 2 // |
| 3 // Copyright IBM Corp. 2012, 2013. All rights reserved. |
| 4 // |
| 5 // Use of this source code is governed by a BSD-style license that can be |
| 6 // found in the LICENSE file. |
| 7 |
| 8 #include "src/v8.h" |
| 9 |
| 10 #if V8_TARGET_ARCH_PPC |
| 11 |
| 12 #include "src/base/bits.h" |
| 13 #include "src/code-stubs.h" |
| 14 #include "src/cpu-profiler.h" |
| 15 #include "src/log.h" |
| 16 #include "src/macro-assembler.h" |
| 17 #include "src/regexp-macro-assembler.h" |
| 18 #include "src/regexp-stack.h" |
| 19 #include "src/unicode.h" |
| 20 |
| 21 #include "src/ppc/regexp-macro-assembler-ppc.h" |
| 22 |
| 23 namespace v8 { |
| 24 namespace internal { |
| 25 |
| 26 #ifndef V8_INTERPRETED_REGEXP |
| 27 /* |
| 28 * This assembler uses the following register assignment convention |
| 29 * - r25: Temporarily stores the index of capture start after a matching pass |
| 30 * for a global regexp. |
| 31 * - r26: Pointer to current code object (Code*) including heap object tag. |
| 32 * - r27: Current position in input, as negative offset from end of string. |
| 33 * Please notice that this is the byte offset, not the character offset! |
| 34 * - r28: Currently loaded character. Must be loaded using |
| 35 * LoadCurrentCharacter before using any of the dispatch methods. |
| 36 * - r29: Points to tip of backtrack stack |
| 37 * - r30: End of input (points to byte after last character in input). |
| 38 * - r31: Frame pointer. Used to access arguments, local variables and |
| 39 * RegExp registers. |
| 40 * - r12: IP register, used by assembler. Very volatile. |
| 41 * - r1/sp : Points to tip of C stack. |
| 42 * |
| 43 * The remaining registers are free for computations. |
| 44 * Each call to a public method should retain this convention. |
| 45 * |
| 46 * The stack will have the following structure: |
| 47 * - fp[44] Isolate* isolate (address of the current isolate) |
| 48 * - fp[40] secondary link/return address used by native call. |
| 49 * - fp[36] lr save area (currently unused) |
| 50 * - fp[32] backchain (currently unused) |
| 51 * --- sp when called --- |
| 52 * - fp[28] return address (lr). |
| 53 * - fp[24] old frame pointer (r31). |
| 54 * - fp[0..20] backup of registers r25..r30 |
| 55 * --- frame pointer ---- |
| 56 * - fp[-4] direct_call (if 1, direct call from JavaScript code, |
| 57 * if 0, call through the runtime system). |
| 58 * - fp[-8] stack_area_base (high end of the memory area to use as |
| 59 * backtracking stack). |
| 60 * - fp[-12] capture array size (may fit multiple sets of matches) |
| 61 * - fp[-16] int* capture_array (int[num_saved_registers_], for output). |
| 62 * - fp[-20] end of input (address of end of string). |
| 63 * - fp[-24] start of input (address of first character in string). |
| 64 * - fp[-28] start index (character index of start). |
| 65 * - fp[-32] void* input_string (location of a handle containing the string). |
| 66 * - fp[-36] success counter (only for global regexps to count matches). |
| 67 * - fp[-40] Offset of location before start of input (effectively character |
| 68 * position -1). Used to initialize capture registers to a |
| 69 * non-position. |
| 70 * - fp[-44] At start (if 1, we are starting at the start of the |
| 71 * string, otherwise 0) |
| 72 * - fp[-48] register 0 (Only positions must be stored in the first |
| 73 * - register 1 num_saved_registers_ registers) |
| 74 * - ... |
| 75 * - register num_registers-1 |
| 76 * --- sp --- |
| 77 * |
| 78 * The first num_saved_registers_ registers are initialized to point to |
| 79 * "character -1" in the string (i.e., char_size() bytes before the first |
| 80 * character of the string). The remaining registers start out as garbage. |
| 81 * |
| 82 * The data up to the return address must be placed there by the calling |
| 83 * code and the remaining arguments are passed in registers, e.g. by calling the |
| 84 * code entry as cast to a function with the signature: |
| 85 * int (*match)(String* input_string, |
| 86 * int start_index, |
| 87 * Address start, |
| 88 * Address end, |
| 89 * int* capture_output_array, |
| 90 * byte* stack_area_base, |
| 91 * Address secondary_return_address, // Only used by native call. |
| 92 * bool direct_call = false) |
| 93 * The call is performed by NativeRegExpMacroAssembler::Execute() |
| 94 * (in regexp-macro-assembler.cc) via the CALL_GENERATED_REGEXP_CODE macro |
| 95 * in ppc/simulator-ppc.h. |
| 96 * When calling as a non-direct call (i.e., from C++ code), the return address |
| 97 * area is overwritten with the LR register by the RegExp code. When doing a |
| 98 * direct call from generated code, the return address is placed there by |
| 99 * the calling code, as in a normal exit frame. |
| 100 */ |
| 101 |
| 102 #define __ ACCESS_MASM(masm_) |
| 103 |
| 104 RegExpMacroAssemblerPPC::RegExpMacroAssemblerPPC(Mode mode, |
| 105 int registers_to_save, |
| 106 Zone* zone) |
| 107 : NativeRegExpMacroAssembler(zone), |
| 108 masm_(new MacroAssembler(zone->isolate(), NULL, kRegExpCodeSize)), |
| 109 mode_(mode), |
| 110 num_registers_(registers_to_save), |
| 111 num_saved_registers_(registers_to_save), |
| 112 entry_label_(), |
| 113 start_label_(), |
| 114 success_label_(), |
| 115 backtrack_label_(), |
| 116 exit_label_(), |
| 117 internal_failure_label_() { |
| 118 DCHECK_EQ(0, registers_to_save % 2); |
| 119 |
| 120 // Called from C |
| 121 #if ABI_USES_FUNCTION_DESCRIPTORS |
| 122 __ function_descriptor(); |
| 123 #endif |
| 124 |
| 125 __ b(&entry_label_); // We'll write the entry code later. |
| 126 // If the code gets too big or corrupted, an internal exception will be |
| 127 // raised, and we will exit right away. |
| 128 __ bind(&internal_failure_label_); |
| 129 __ li(r3, Operand(FAILURE)); |
| 130 __ Ret(); |
| 131 __ bind(&start_label_); // And then continue from here. |
| 132 } |
| 133 |
| 134 |
| 135 RegExpMacroAssemblerPPC::~RegExpMacroAssemblerPPC() { |
| 136 delete masm_; |
| 137 // Unuse labels in case we throw away the assembler without calling GetCode. |
| 138 entry_label_.Unuse(); |
| 139 start_label_.Unuse(); |
| 140 success_label_.Unuse(); |
| 141 backtrack_label_.Unuse(); |
| 142 exit_label_.Unuse(); |
| 143 check_preempt_label_.Unuse(); |
| 144 stack_overflow_label_.Unuse(); |
| 145 internal_failure_label_.Unuse(); |
| 146 } |
| 147 |
| 148 |
| 149 int RegExpMacroAssemblerPPC::stack_limit_slack() { |
| 150 return RegExpStack::kStackLimitSlack; |
| 151 } |
| 152 |
| 153 |
| 154 void RegExpMacroAssemblerPPC::AdvanceCurrentPosition(int by) { |
| 155 if (by != 0) { |
| 156 __ addi(current_input_offset(), current_input_offset(), |
| 157 Operand(by * char_size())); |
| 158 } |
| 159 } |
| 160 |
| 161 |
| 162 void RegExpMacroAssemblerPPC::AdvanceRegister(int reg, int by) { |
| 163 DCHECK(reg >= 0); |
| 164 DCHECK(reg < num_registers_); |
| 165 if (by != 0) { |
| 166 __ LoadP(r3, register_location(reg), r0); |
| 167 __ mov(r0, Operand(by)); |
| 168 __ add(r3, r3, r0); |
| 169 __ StoreP(r3, register_location(reg), r0); |
| 170 } |
| 171 } |
| 172 |
| 173 |
| 174 void RegExpMacroAssemblerPPC::Backtrack() { |
| 175 CheckPreemption(); |
| 176 // Pop Code* offset from backtrack stack, add Code* and jump to location. |
| 177 Pop(r3); |
| 178 __ add(r3, r3, code_pointer()); |
| 179 __ mtctr(r3); |
| 180 __ bctr(); |
| 181 } |
| 182 |
| 183 |
| 184 void RegExpMacroAssemblerPPC::Bind(Label* label) { __ bind(label); } |
| 185 |
| 186 |
| 187 void RegExpMacroAssemblerPPC::CheckCharacter(uint32_t c, Label* on_equal) { |
| 188 __ Cmpli(current_character(), Operand(c), r0); |
| 189 BranchOrBacktrack(eq, on_equal); |
| 190 } |
| 191 |
| 192 |
| 193 void RegExpMacroAssemblerPPC::CheckCharacterGT(uc16 limit, Label* on_greater) { |
| 194 __ Cmpli(current_character(), Operand(limit), r0); |
| 195 BranchOrBacktrack(gt, on_greater); |
| 196 } |
| 197 |
| 198 |
| 199 void RegExpMacroAssemblerPPC::CheckAtStart(Label* on_at_start) { |
| 200 Label not_at_start; |
| 201 // Did we start the match at the start of the string at all? |
| 202 __ LoadP(r3, MemOperand(frame_pointer(), kStartIndex)); |
| 203 __ cmpi(r3, Operand::Zero()); |
| 204 BranchOrBacktrack(ne, ¬_at_start); |
| 205 |
| 206 // If we did, are we still at the start of the input? |
| 207 __ LoadP(r4, MemOperand(frame_pointer(), kInputStart)); |
| 208 __ mr(r0, current_input_offset()); |
| 209 __ add(r3, end_of_input_address(), r0); |
| 210 __ cmp(r4, r3); |
| 211 BranchOrBacktrack(eq, on_at_start); |
| 212 __ bind(¬_at_start); |
| 213 } |
| 214 |
| 215 |
| 216 void RegExpMacroAssemblerPPC::CheckNotAtStart(Label* on_not_at_start) { |
| 217 // Did we start the match at the start of the string at all? |
| 218 __ LoadP(r3, MemOperand(frame_pointer(), kStartIndex)); |
| 219 __ cmpi(r3, Operand::Zero()); |
| 220 BranchOrBacktrack(ne, on_not_at_start); |
| 221 // If we did, are we still at the start of the input? |
| 222 __ LoadP(r4, MemOperand(frame_pointer(), kInputStart)); |
| 223 __ add(r3, end_of_input_address(), current_input_offset()); |
| 224 __ cmp(r3, r4); |
| 225 BranchOrBacktrack(ne, on_not_at_start); |
| 226 } |
| 227 |
| 228 |
| 229 void RegExpMacroAssemblerPPC::CheckCharacterLT(uc16 limit, Label* on_less) { |
| 230 __ Cmpli(current_character(), Operand(limit), r0); |
| 231 BranchOrBacktrack(lt, on_less); |
| 232 } |
| 233 |
| 234 |
| 235 void RegExpMacroAssemblerPPC::CheckGreedyLoop(Label* on_equal) { |
| 236 Label backtrack_non_equal; |
| 237 __ LoadP(r3, MemOperand(backtrack_stackpointer(), 0)); |
| 238 __ cmp(current_input_offset(), r3); |
| 239 __ bne(&backtrack_non_equal); |
| 240 __ addi(backtrack_stackpointer(), backtrack_stackpointer(), |
| 241 Operand(kPointerSize)); |
| 242 |
| 243 __ bind(&backtrack_non_equal); |
| 244 BranchOrBacktrack(eq, on_equal); |
| 245 } |
| 246 |
| 247 |
| 248 void RegExpMacroAssemblerPPC::CheckNotBackReferenceIgnoreCase( |
| 249 int start_reg, Label* on_no_match) { |
| 250 Label fallthrough; |
| 251 __ LoadP(r3, register_location(start_reg), r0); // Index of start of capture |
| 252 __ LoadP(r4, register_location(start_reg + 1), r0); // Index of end |
| 253 __ sub(r4, r4, r3, LeaveOE, SetRC); // Length of capture. |
| 254 |
| 255 // If length is zero, either the capture is empty or it is not participating. |
| 256 // In either case succeed immediately. |
| 257 __ beq(&fallthrough, cr0); |
| 258 |
| 259 // Check that there are enough characters left in the input. |
| 260 __ add(r0, r4, current_input_offset(), LeaveOE, SetRC); |
| 261 // __ cmn(r1, Operand(current_input_offset())); |
| 262 BranchOrBacktrack(gt, on_no_match, cr0); |
| 263 |
| 264 if (mode_ == LATIN1) { |
| 265 Label success; |
| 266 Label fail; |
| 267 Label loop_check; |
| 268 |
| 269 // r3 - offset of start of capture |
| 270 // r4 - length of capture |
| 271 __ add(r3, r3, end_of_input_address()); |
| 272 __ add(r5, end_of_input_address(), current_input_offset()); |
| 273 __ add(r4, r3, r4); |
| 274 |
| 275 // r3 - Address of start of capture. |
| 276 // r4 - Address of end of capture |
| 277 // r5 - Address of current input position. |
| 278 |
| 279 Label loop; |
| 280 __ bind(&loop); |
| 281 __ lbz(r6, MemOperand(r3)); |
| 282 __ addi(r3, r3, Operand(char_size())); |
| 283 __ lbz(r25, MemOperand(r5)); |
| 284 __ addi(r5, r5, Operand(char_size())); |
| 285 __ cmp(r25, r6); |
| 286 __ beq(&loop_check); |
| 287 |
| 288 // Mismatch, try case-insensitive match (converting letters to lower-case). |
| 289 __ ori(r6, r6, Operand(0x20)); // Convert capture character to lower-case. |
| 290 __ ori(r25, r25, Operand(0x20)); // Also convert input character. |
| 291 __ cmp(r25, r6); |
| 292 __ bne(&fail); |
| 293 __ subi(r6, r6, Operand('a')); |
| 294 __ cmpli(r6, Operand('z' - 'a')); // Is r6 a lowercase letter? |
| 295 __ ble(&loop_check); // In range 'a'-'z'. |
| 296 // Latin-1: Check for values in range [224,254] but not 247. |
| 297 __ subi(r6, r6, Operand(224 - 'a')); |
| 298 __ cmpli(r6, Operand(254 - 224)); |
| 299 __ bgt(&fail); // Weren't Latin-1 letters. |
| 300 __ cmpi(r6, Operand(247 - 224)); // Check for 247. |
| 301 __ beq(&fail); |
| 302 |
| 303 __ bind(&loop_check); |
| 304 __ cmp(r3, r4); |
| 305 __ blt(&loop); |
| 306 __ b(&success); |
| 307 |
| 308 __ bind(&fail); |
| 309 BranchOrBacktrack(al, on_no_match); |
| 310 |
| 311 __ bind(&success); |
| 312 // Compute new value of character position after the matched part. |
| 313 __ sub(current_input_offset(), r5, end_of_input_address()); |
| 314 } else { |
| 315 DCHECK(mode_ == UC16); |
| 316 int argument_count = 4; |
| 317 __ PrepareCallCFunction(argument_count, r5); |
| 318 |
| 319 // r3 - offset of start of capture |
| 320 // r4 - length of capture |
| 321 |
| 322 // Put arguments into arguments registers. |
| 323 // Parameters are |
| 324 // r3: Address byte_offset1 - Address captured substring's start. |
| 325 // r4: Address byte_offset2 - Address of current character position. |
| 326 // r5: size_t byte_length - length of capture in bytes(!) |
| 327 // r6: Isolate* isolate |
| 328 |
| 329 // Address of start of capture. |
| 330 __ add(r3, r3, end_of_input_address()); |
| 331 // Length of capture. |
| 332 __ mr(r5, r4); |
| 333 // Save length in callee-save register for use on return. |
| 334 __ mr(r25, r4); |
| 335 // Address of current input position. |
| 336 __ add(r4, current_input_offset(), end_of_input_address()); |
| 337 // Isolate. |
| 338 __ mov(r6, Operand(ExternalReference::isolate_address(isolate()))); |
| 339 |
| 340 { |
| 341 AllowExternalCallThatCantCauseGC scope(masm_); |
| 342 ExternalReference function = |
| 343 ExternalReference::re_case_insensitive_compare_uc16(isolate()); |
| 344 __ CallCFunction(function, argument_count); |
| 345 } |
| 346 |
| 347 // Check if function returned non-zero for success or zero for failure. |
| 348 __ cmpi(r3, Operand::Zero()); |
| 349 BranchOrBacktrack(eq, on_no_match); |
| 350 // On success, increment position by length of capture. |
| 351 __ add(current_input_offset(), current_input_offset(), r25); |
| 352 } |
| 353 |
| 354 __ bind(&fallthrough); |
| 355 } |
| 356 |
| 357 |
| 358 void RegExpMacroAssemblerPPC::CheckNotBackReference(int start_reg, |
| 359 Label* on_no_match) { |
| 360 Label fallthrough; |
| 361 Label success; |
| 362 |
| 363 // Find length of back-referenced capture. |
| 364 __ LoadP(r3, register_location(start_reg), r0); |
| 365 __ LoadP(r4, register_location(start_reg + 1), r0); |
| 366 __ sub(r4, r4, r3, LeaveOE, SetRC); // Length to check. |
| 367 // Succeed on empty capture (including no capture). |
| 368 __ beq(&fallthrough, cr0); |
| 369 |
| 370 // Check that there are enough characters left in the input. |
| 371 __ add(r0, r4, current_input_offset(), LeaveOE, SetRC); |
| 372 BranchOrBacktrack(gt, on_no_match, cr0); |
| 373 |
| 374 // Compute pointers to match string and capture string |
| 375 __ add(r3, r3, end_of_input_address()); |
| 376 __ add(r5, end_of_input_address(), current_input_offset()); |
| 377 __ add(r4, r4, r3); |
| 378 |
| 379 Label loop; |
| 380 __ bind(&loop); |
| 381 if (mode_ == LATIN1) { |
| 382 __ lbz(r6, MemOperand(r3)); |
| 383 __ addi(r3, r3, Operand(char_size())); |
| 384 __ lbz(r25, MemOperand(r5)); |
| 385 __ addi(r5, r5, Operand(char_size())); |
| 386 } else { |
| 387 DCHECK(mode_ == UC16); |
| 388 __ lhz(r6, MemOperand(r3)); |
| 389 __ addi(r3, r3, Operand(char_size())); |
| 390 __ lhz(r25, MemOperand(r5)); |
| 391 __ addi(r5, r5, Operand(char_size())); |
| 392 } |
| 393 __ cmp(r6, r25); |
| 394 BranchOrBacktrack(ne, on_no_match); |
| 395 __ cmp(r3, r4); |
| 396 __ blt(&loop); |
| 397 |
| 398 // Move current character position to position after match. |
| 399 __ sub(current_input_offset(), r5, end_of_input_address()); |
| 400 __ bind(&fallthrough); |
| 401 } |
| 402 |
| 403 |
| 404 void RegExpMacroAssemblerPPC::CheckNotCharacter(unsigned c, |
| 405 Label* on_not_equal) { |
| 406 __ Cmpli(current_character(), Operand(c), r0); |
| 407 BranchOrBacktrack(ne, on_not_equal); |
| 408 } |
| 409 |
| 410 |
| 411 void RegExpMacroAssemblerPPC::CheckCharacterAfterAnd(uint32_t c, uint32_t mask, |
| 412 Label* on_equal) { |
| 413 __ mov(r0, Operand(mask)); |
| 414 if (c == 0) { |
| 415 __ and_(r3, current_character(), r0, SetRC); |
| 416 } else { |
| 417 __ and_(r3, current_character(), r0); |
| 418 __ Cmpli(r3, Operand(c), r0, cr0); |
| 419 } |
| 420 BranchOrBacktrack(eq, on_equal, cr0); |
| 421 } |
| 422 |
| 423 |
| 424 void RegExpMacroAssemblerPPC::CheckNotCharacterAfterAnd(unsigned c, |
| 425 unsigned mask, |
| 426 Label* on_not_equal) { |
| 427 __ mov(r0, Operand(mask)); |
| 428 if (c == 0) { |
| 429 __ and_(r3, current_character(), r0, SetRC); |
| 430 } else { |
| 431 __ and_(r3, current_character(), r0); |
| 432 __ Cmpli(r3, Operand(c), r0, cr0); |
| 433 } |
| 434 BranchOrBacktrack(ne, on_not_equal, cr0); |
| 435 } |
| 436 |
| 437 |
| 438 void RegExpMacroAssemblerPPC::CheckNotCharacterAfterMinusAnd( |
| 439 uc16 c, uc16 minus, uc16 mask, Label* on_not_equal) { |
| 440 DCHECK(minus < String::kMaxUtf16CodeUnit); |
| 441 __ subi(r3, current_character(), Operand(minus)); |
| 442 __ mov(r0, Operand(mask)); |
| 443 __ and_(r3, r3, r0); |
| 444 __ Cmpli(r3, Operand(c), r0); |
| 445 BranchOrBacktrack(ne, on_not_equal); |
| 446 } |
| 447 |
| 448 |
| 449 void RegExpMacroAssemblerPPC::CheckCharacterInRange(uc16 from, uc16 to, |
| 450 Label* on_in_range) { |
| 451 __ mov(r0, Operand(from)); |
| 452 __ sub(r3, current_character(), r0); |
| 453 __ Cmpli(r3, Operand(to - from), r0); |
| 454 BranchOrBacktrack(le, on_in_range); // Unsigned lower-or-same condition. |
| 455 } |
| 456 |
| 457 |
| 458 void RegExpMacroAssemblerPPC::CheckCharacterNotInRange(uc16 from, uc16 to, |
| 459 Label* on_not_in_range) { |
| 460 __ mov(r0, Operand(from)); |
| 461 __ sub(r3, current_character(), r0); |
| 462 __ Cmpli(r3, Operand(to - from), r0); |
| 463 BranchOrBacktrack(gt, on_not_in_range); // Unsigned higher condition. |
| 464 } |
| 465 |
| 466 |
| 467 void RegExpMacroAssemblerPPC::CheckBitInTable(Handle<ByteArray> table, |
| 468 Label* on_bit_set) { |
| 469 __ mov(r3, Operand(table)); |
| 470 if (mode_ != LATIN1 || kTableMask != String::kMaxOneByteCharCode) { |
| 471 __ andi(r4, current_character(), Operand(kTableSize - 1)); |
| 472 __ addi(r4, r4, Operand(ByteArray::kHeaderSize - kHeapObjectTag)); |
| 473 } else { |
| 474 __ addi(r4, current_character(), |
| 475 Operand(ByteArray::kHeaderSize - kHeapObjectTag)); |
| 476 } |
| 477 __ lbzx(r3, MemOperand(r3, r4)); |
| 478 __ cmpi(r3, Operand::Zero()); |
| 479 BranchOrBacktrack(ne, on_bit_set); |
| 480 } |
| 481 |
| 482 |
| 483 bool RegExpMacroAssemblerPPC::CheckSpecialCharacterClass(uc16 type, |
| 484 Label* on_no_match) { |
| 485 // Range checks (c in min..max) are generally implemented by an unsigned |
| 486 // (c - min) <= (max - min) check |
| 487 switch (type) { |
| 488 case 's': |
| 489 // Match space-characters |
| 490 if (mode_ == LATIN1) { |
| 491 // One byte space characters are '\t'..'\r', ' ' and \u00a0. |
| 492 Label success; |
| 493 __ cmpi(current_character(), Operand(' ')); |
| 494 __ beq(&success); |
| 495 // Check range 0x09..0x0d |
| 496 __ subi(r3, current_character(), Operand('\t')); |
| 497 __ cmpli(r3, Operand('\r' - '\t')); |
| 498 __ ble(&success); |
| 499 // \u00a0 (NBSP). |
| 500 __ cmpi(r3, Operand(0x00a0 - '\t')); |
| 501 BranchOrBacktrack(ne, on_no_match); |
| 502 __ bind(&success); |
| 503 return true; |
| 504 } |
| 505 return false; |
| 506 case 'S': |
| 507 // The emitted code for generic character classes is good enough. |
| 508 return false; |
| 509 case 'd': |
| 510 // Match ASCII digits ('0'..'9') |
| 511 __ subi(r3, current_character(), Operand('0')); |
| 512 __ cmpli(r3, Operand('9' - '0')); |
| 513 BranchOrBacktrack(gt, on_no_match); |
| 514 return true; |
| 515 case 'D': |
| 516 // Match non ASCII-digits |
| 517 __ subi(r3, current_character(), Operand('0')); |
| 518 __ cmpli(r3, Operand('9' - '0')); |
| 519 BranchOrBacktrack(le, on_no_match); |
| 520 return true; |
| 521 case '.': { |
| 522 // Match non-newlines (not 0x0a('\n'), 0x0d('\r'), 0x2028 and 0x2029) |
| 523 __ xori(r3, current_character(), Operand(0x01)); |
| 524 // See if current character is '\n'^1 or '\r'^1, i.e., 0x0b or 0x0c |
| 525 __ subi(r3, r3, Operand(0x0b)); |
| 526 __ cmpli(r3, Operand(0x0c - 0x0b)); |
| 527 BranchOrBacktrack(le, on_no_match); |
| 528 if (mode_ == UC16) { |
| 529 // Compare original value to 0x2028 and 0x2029, using the already |
| 530 // computed (current_char ^ 0x01 - 0x0b). I.e., check for |
| 531 // 0x201d (0x2028 - 0x0b) or 0x201e. |
| 532 __ subi(r3, r3, Operand(0x2028 - 0x0b)); |
| 533 __ cmpli(r3, Operand(1)); |
| 534 BranchOrBacktrack(le, on_no_match); |
| 535 } |
| 536 return true; |
| 537 } |
| 538 case 'n': { |
| 539 // Match newlines (0x0a('\n'), 0x0d('\r'), 0x2028 and 0x2029) |
| 540 __ xori(r3, current_character(), Operand(0x01)); |
| 541 // See if current character is '\n'^1 or '\r'^1, i.e., 0x0b or 0x0c |
| 542 __ subi(r3, r3, Operand(0x0b)); |
| 543 __ cmpli(r3, Operand(0x0c - 0x0b)); |
| 544 if (mode_ == LATIN1) { |
| 545 BranchOrBacktrack(gt, on_no_match); |
| 546 } else { |
| 547 Label done; |
| 548 __ ble(&done); |
| 549 // Compare original value to 0x2028 and 0x2029, using the already |
| 550 // computed (current_char ^ 0x01 - 0x0b). I.e., check for |
| 551 // 0x201d (0x2028 - 0x0b) or 0x201e. |
| 552 __ subi(r3, r3, Operand(0x2028 - 0x0b)); |
| 553 __ cmpli(r3, Operand(1)); |
| 554 BranchOrBacktrack(gt, on_no_match); |
| 555 __ bind(&done); |
| 556 } |
| 557 return true; |
| 558 } |
| 559 case 'w': { |
| 560 if (mode_ != LATIN1) { |
| 561 // Table is 256 entries, so all Latin1 characters can be tested. |
| 562 __ cmpi(current_character(), Operand('z')); |
| 563 BranchOrBacktrack(gt, on_no_match); |
| 564 } |
| 565 ExternalReference map = ExternalReference::re_word_character_map(); |
| 566 __ mov(r3, Operand(map)); |
| 567 __ lbzx(r3, MemOperand(r3, current_character())); |
| 568 __ cmpli(r3, Operand::Zero()); |
| 569 BranchOrBacktrack(eq, on_no_match); |
| 570 return true; |
| 571 } |
| 572 case 'W': { |
| 573 Label done; |
| 574 if (mode_ != LATIN1) { |
| 575 // Table is 256 entries, so all Latin1 characters can be tested. |
| 576 __ cmpli(current_character(), Operand('z')); |
| 577 __ bgt(&done); |
| 578 } |
| 579 ExternalReference map = ExternalReference::re_word_character_map(); |
| 580 __ mov(r3, Operand(map)); |
| 581 __ lbzx(r3, MemOperand(r3, current_character())); |
| 582 __ cmpli(r3, Operand::Zero()); |
| 583 BranchOrBacktrack(ne, on_no_match); |
| 584 if (mode_ != LATIN1) { |
| 585 __ bind(&done); |
| 586 } |
| 587 return true; |
| 588 } |
| 589 case '*': |
| 590 // Match any character. |
| 591 return true; |
| 592 // No custom implementation (yet): s(UC16), S(UC16). |
| 593 default: |
| 594 return false; |
| 595 } |
| 596 } |
| 597 |
| 598 |
| 599 void RegExpMacroAssemblerPPC::Fail() { |
| 600 __ li(r3, Operand(FAILURE)); |
| 601 __ b(&exit_label_); |
| 602 } |
| 603 |
| 604 |
| 605 Handle<HeapObject> RegExpMacroAssemblerPPC::GetCode(Handle<String> source) { |
| 606 Label return_r3; |
| 607 |
| 608 if (masm_->has_exception()) { |
| 609 // If the code gets corrupted due to long regular expressions and lack of |
| 610 // space on trampolines, an internal exception flag is set. If this case |
| 611 // is detected, we will jump into exit sequence right away. |
| 612 __ bind_to(&entry_label_, internal_failure_label_.pos()); |
| 613 } else { |
| 614 // Finalize code - write the entry point code now we know how many |
| 615 // registers we need. |
| 616 |
| 617 // Entry code: |
| 618 __ bind(&entry_label_); |
| 619 |
| 620 // Tell the system that we have a stack frame. Because the type |
| 621 // is MANUAL, no is generated. |
| 622 FrameScope scope(masm_, StackFrame::MANUAL); |
| 623 |
| 624 // Ensure register assigments are consistent with callee save mask |
| 625 DCHECK(r25.bit() & kRegExpCalleeSaved); |
| 626 DCHECK(code_pointer().bit() & kRegExpCalleeSaved); |
| 627 DCHECK(current_input_offset().bit() & kRegExpCalleeSaved); |
| 628 DCHECK(current_character().bit() & kRegExpCalleeSaved); |
| 629 DCHECK(backtrack_stackpointer().bit() & kRegExpCalleeSaved); |
| 630 DCHECK(end_of_input_address().bit() & kRegExpCalleeSaved); |
| 631 DCHECK(frame_pointer().bit() & kRegExpCalleeSaved); |
| 632 |
| 633 // Actually emit code to start a new stack frame. |
| 634 // Push arguments |
| 635 // Save callee-save registers. |
| 636 // Start new stack frame. |
| 637 // Store link register in existing stack-cell. |
| 638 // Order here should correspond to order of offset constants in header file. |
| 639 RegList registers_to_retain = kRegExpCalleeSaved; |
| 640 RegList argument_registers = r3.bit() | r4.bit() | r5.bit() | r6.bit() | |
| 641 r7.bit() | r8.bit() | r9.bit() | r10.bit(); |
| 642 __ mflr(r0); |
| 643 __ push(r0); |
| 644 __ MultiPush(argument_registers | registers_to_retain); |
| 645 // Set frame pointer in space for it if this is not a direct call |
| 646 // from generated code. |
| 647 __ addi(frame_pointer(), sp, Operand(8 * kPointerSize)); |
| 648 __ li(r3, Operand::Zero()); |
| 649 __ push(r3); // Make room for success counter and initialize it to 0. |
| 650 __ push(r3); // Make room for "position - 1" constant (value is irrelevant) |
| 651 // Check if we have space on the stack for registers. |
| 652 Label stack_limit_hit; |
| 653 Label stack_ok; |
| 654 |
| 655 ExternalReference stack_limit = |
| 656 ExternalReference::address_of_stack_limit(isolate()); |
| 657 __ mov(r3, Operand(stack_limit)); |
| 658 __ LoadP(r3, MemOperand(r3)); |
| 659 __ sub(r3, sp, r3, LeaveOE, SetRC); |
| 660 // Handle it if the stack pointer is already below the stack limit. |
| 661 __ ble(&stack_limit_hit, cr0); |
| 662 // Check if there is room for the variable number of registers above |
| 663 // the stack limit. |
| 664 __ Cmpli(r3, Operand(num_registers_ * kPointerSize), r0); |
| 665 __ bge(&stack_ok); |
| 666 // Exit with OutOfMemory exception. There is not enough space on the stack |
| 667 // for our working registers. |
| 668 __ li(r3, Operand(EXCEPTION)); |
| 669 __ b(&return_r3); |
| 670 |
| 671 __ bind(&stack_limit_hit); |
| 672 CallCheckStackGuardState(r3); |
| 673 __ cmpi(r3, Operand::Zero()); |
| 674 // If returned value is non-zero, we exit with the returned value as result. |
| 675 __ bne(&return_r3); |
| 676 |
| 677 __ bind(&stack_ok); |
| 678 |
| 679 // Allocate space on stack for registers. |
| 680 __ Add(sp, sp, -num_registers_ * kPointerSize, r0); |
| 681 // Load string end. |
| 682 __ LoadP(end_of_input_address(), MemOperand(frame_pointer(), kInputEnd)); |
| 683 // Load input start. |
| 684 __ LoadP(r3, MemOperand(frame_pointer(), kInputStart)); |
| 685 // Find negative length (offset of start relative to end). |
| 686 __ sub(current_input_offset(), r3, end_of_input_address()); |
| 687 // Set r3 to address of char before start of the input string |
| 688 // (effectively string position -1). |
| 689 __ LoadP(r4, MemOperand(frame_pointer(), kStartIndex)); |
| 690 __ subi(r3, current_input_offset(), Operand(char_size())); |
| 691 if (mode_ == UC16) { |
| 692 __ ShiftLeftImm(r0, r4, Operand(1)); |
| 693 __ sub(r3, r3, r0); |
| 694 } else { |
| 695 __ sub(r3, r3, r4); |
| 696 } |
| 697 // Store this value in a local variable, for use when clearing |
| 698 // position registers. |
| 699 __ StoreP(r3, MemOperand(frame_pointer(), kInputStartMinusOne)); |
| 700 |
| 701 // Initialize code pointer register |
| 702 __ mov(code_pointer(), Operand(masm_->CodeObject())); |
| 703 |
| 704 Label load_char_start_regexp, start_regexp; |
| 705 // Load newline if index is at start, previous character otherwise. |
| 706 __ cmpi(r4, Operand::Zero()); |
| 707 __ bne(&load_char_start_regexp); |
| 708 __ li(current_character(), Operand('\n')); |
| 709 __ b(&start_regexp); |
| 710 |
| 711 // Global regexp restarts matching here. |
| 712 __ bind(&load_char_start_regexp); |
| 713 // Load previous char as initial value of current character register. |
| 714 LoadCurrentCharacterUnchecked(-1, 1); |
| 715 __ bind(&start_regexp); |
| 716 |
| 717 // Initialize on-stack registers. |
| 718 if (num_saved_registers_ > 0) { // Always is, if generated from a regexp. |
| 719 // Fill saved registers with initial value = start offset - 1 |
| 720 if (num_saved_registers_ > 8) { |
| 721 // One slot beyond address of register 0. |
| 722 __ addi(r4, frame_pointer(), Operand(kRegisterZero + kPointerSize)); |
| 723 __ li(r5, Operand(num_saved_registers_)); |
| 724 __ mtctr(r5); |
| 725 Label init_loop; |
| 726 __ bind(&init_loop); |
| 727 __ StorePU(r3, MemOperand(r4, -kPointerSize)); |
| 728 __ bdnz(&init_loop); |
| 729 } else { |
| 730 for (int i = 0; i < num_saved_registers_; i++) { |
| 731 __ StoreP(r3, register_location(i), r0); |
| 732 } |
| 733 } |
| 734 } |
| 735 |
| 736 // Initialize backtrack stack pointer. |
| 737 __ LoadP(backtrack_stackpointer(), |
| 738 MemOperand(frame_pointer(), kStackHighEnd)); |
| 739 |
| 740 __ b(&start_label_); |
| 741 |
| 742 // Exit code: |
| 743 if (success_label_.is_linked()) { |
| 744 // Save captures when successful. |
| 745 __ bind(&success_label_); |
| 746 if (num_saved_registers_ > 0) { |
| 747 // copy captures to output |
| 748 __ LoadP(r4, MemOperand(frame_pointer(), kInputStart)); |
| 749 __ LoadP(r3, MemOperand(frame_pointer(), kRegisterOutput)); |
| 750 __ LoadP(r5, MemOperand(frame_pointer(), kStartIndex)); |
| 751 __ sub(r4, end_of_input_address(), r4); |
| 752 // r4 is length of input in bytes. |
| 753 if (mode_ == UC16) { |
| 754 __ ShiftRightImm(r4, r4, Operand(1)); |
| 755 } |
| 756 // r4 is length of input in characters. |
| 757 __ add(r4, r4, r5); |
| 758 // r4 is length of string in characters. |
| 759 |
| 760 DCHECK_EQ(0, num_saved_registers_ % 2); |
| 761 // Always an even number of capture registers. This allows us to |
| 762 // unroll the loop once to add an operation between a load of a register |
| 763 // and the following use of that register. |
| 764 for (int i = 0; i < num_saved_registers_; i += 2) { |
| 765 __ LoadP(r5, register_location(i), r0); |
| 766 __ LoadP(r6, register_location(i + 1), r0); |
| 767 if (i == 0 && global_with_zero_length_check()) { |
| 768 // Keep capture start in r25 for the zero-length check later. |
| 769 __ mr(r25, r5); |
| 770 } |
| 771 if (mode_ == UC16) { |
| 772 __ ShiftRightArithImm(r5, r5, 1); |
| 773 __ add(r5, r4, r5); |
| 774 __ ShiftRightArithImm(r6, r6, 1); |
| 775 __ add(r6, r4, r6); |
| 776 } else { |
| 777 __ add(r5, r4, r5); |
| 778 __ add(r6, r4, r6); |
| 779 } |
| 780 __ stw(r5, MemOperand(r3)); |
| 781 __ addi(r3, r3, Operand(kIntSize)); |
| 782 __ stw(r6, MemOperand(r3)); |
| 783 __ addi(r3, r3, Operand(kIntSize)); |
| 784 } |
| 785 } |
| 786 |
| 787 if (global()) { |
| 788 // Restart matching if the regular expression is flagged as global. |
| 789 __ LoadP(r3, MemOperand(frame_pointer(), kSuccessfulCaptures)); |
| 790 __ LoadP(r4, MemOperand(frame_pointer(), kNumOutputRegisters)); |
| 791 __ LoadP(r5, MemOperand(frame_pointer(), kRegisterOutput)); |
| 792 // Increment success counter. |
| 793 __ addi(r3, r3, Operand(1)); |
| 794 __ StoreP(r3, MemOperand(frame_pointer(), kSuccessfulCaptures)); |
| 795 // Capture results have been stored, so the number of remaining global |
| 796 // output registers is reduced by the number of stored captures. |
| 797 __ subi(r4, r4, Operand(num_saved_registers_)); |
| 798 // Check whether we have enough room for another set of capture results. |
| 799 __ cmpi(r4, Operand(num_saved_registers_)); |
| 800 __ blt(&return_r3); |
| 801 |
| 802 __ StoreP(r4, MemOperand(frame_pointer(), kNumOutputRegisters)); |
| 803 // Advance the location for output. |
| 804 __ addi(r5, r5, Operand(num_saved_registers_ * kIntSize)); |
| 805 __ StoreP(r5, MemOperand(frame_pointer(), kRegisterOutput)); |
| 806 |
| 807 // Prepare r3 to initialize registers with its value in the next run. |
| 808 __ LoadP(r3, MemOperand(frame_pointer(), kInputStartMinusOne)); |
| 809 |
| 810 if (global_with_zero_length_check()) { |
| 811 // Special case for zero-length matches. |
| 812 // r25: capture start index |
| 813 __ cmp(current_input_offset(), r25); |
| 814 // Not a zero-length match, restart. |
| 815 __ bne(&load_char_start_regexp); |
| 816 // Offset from the end is zero if we already reached the end. |
| 817 __ cmpi(current_input_offset(), Operand::Zero()); |
| 818 __ beq(&exit_label_); |
| 819 // Advance current position after a zero-length match. |
| 820 __ addi(current_input_offset(), current_input_offset(), |
| 821 Operand((mode_ == UC16) ? 2 : 1)); |
| 822 } |
| 823 |
| 824 __ b(&load_char_start_regexp); |
| 825 } else { |
| 826 __ li(r3, Operand(SUCCESS)); |
| 827 } |
| 828 } |
| 829 |
| 830 // Exit and return r3 |
| 831 __ bind(&exit_label_); |
| 832 if (global()) { |
| 833 __ LoadP(r3, MemOperand(frame_pointer(), kSuccessfulCaptures)); |
| 834 } |
| 835 |
| 836 __ bind(&return_r3); |
| 837 // Skip sp past regexp registers and local variables.. |
| 838 __ mr(sp, frame_pointer()); |
| 839 // Restore registers r25..r31 and return (restoring lr to pc). |
| 840 __ MultiPop(registers_to_retain); |
| 841 __ pop(r0); |
| 842 __ mtctr(r0); |
| 843 __ bctr(); |
| 844 |
| 845 // Backtrack code (branch target for conditional backtracks). |
| 846 if (backtrack_label_.is_linked()) { |
| 847 __ bind(&backtrack_label_); |
| 848 Backtrack(); |
| 849 } |
| 850 |
| 851 Label exit_with_exception; |
| 852 |
| 853 // Preempt-code |
| 854 if (check_preempt_label_.is_linked()) { |
| 855 SafeCallTarget(&check_preempt_label_); |
| 856 |
| 857 CallCheckStackGuardState(r3); |
| 858 __ cmpi(r3, Operand::Zero()); |
| 859 // If returning non-zero, we should end execution with the given |
| 860 // result as return value. |
| 861 __ bne(&return_r3); |
| 862 |
| 863 // String might have moved: Reload end of string from frame. |
| 864 __ LoadP(end_of_input_address(), MemOperand(frame_pointer(), kInputEnd)); |
| 865 SafeReturn(); |
| 866 } |
| 867 |
| 868 // Backtrack stack overflow code. |
| 869 if (stack_overflow_label_.is_linked()) { |
| 870 SafeCallTarget(&stack_overflow_label_); |
| 871 // Reached if the backtrack-stack limit has been hit. |
| 872 Label grow_failed; |
| 873 |
| 874 // Call GrowStack(backtrack_stackpointer(), &stack_base) |
| 875 static const int num_arguments = 3; |
| 876 __ PrepareCallCFunction(num_arguments, r3); |
| 877 __ mr(r3, backtrack_stackpointer()); |
| 878 __ addi(r4, frame_pointer(), Operand(kStackHighEnd)); |
| 879 __ mov(r5, Operand(ExternalReference::isolate_address(isolate()))); |
| 880 ExternalReference grow_stack = |
| 881 ExternalReference::re_grow_stack(isolate()); |
| 882 __ CallCFunction(grow_stack, num_arguments); |
| 883 // If return NULL, we have failed to grow the stack, and |
| 884 // must exit with a stack-overflow exception. |
| 885 __ cmpi(r3, Operand::Zero()); |
| 886 __ beq(&exit_with_exception); |
| 887 // Otherwise use return value as new stack pointer. |
| 888 __ mr(backtrack_stackpointer(), r3); |
| 889 // Restore saved registers and continue. |
| 890 SafeReturn(); |
| 891 } |
| 892 |
| 893 if (exit_with_exception.is_linked()) { |
| 894 // If any of the code above needed to exit with an exception. |
| 895 __ bind(&exit_with_exception); |
| 896 // Exit with Result EXCEPTION(-1) to signal thrown exception. |
| 897 __ li(r3, Operand(EXCEPTION)); |
| 898 __ b(&return_r3); |
| 899 } |
| 900 } |
| 901 |
| 902 CodeDesc code_desc; |
| 903 masm_->GetCode(&code_desc); |
| 904 Handle<Code> code = isolate()->factory()->NewCode( |
| 905 code_desc, Code::ComputeFlags(Code::REGEXP), masm_->CodeObject()); |
| 906 PROFILE(masm_->isolate(), RegExpCodeCreateEvent(*code, *source)); |
| 907 return Handle<HeapObject>::cast(code); |
| 908 } |
| 909 |
| 910 |
| 911 void RegExpMacroAssemblerPPC::GoTo(Label* to) { BranchOrBacktrack(al, to); } |
| 912 |
| 913 |
| 914 void RegExpMacroAssemblerPPC::IfRegisterGE(int reg, int comparand, |
| 915 Label* if_ge) { |
| 916 __ LoadP(r3, register_location(reg), r0); |
| 917 __ Cmpi(r3, Operand(comparand), r0); |
| 918 BranchOrBacktrack(ge, if_ge); |
| 919 } |
| 920 |
| 921 |
| 922 void RegExpMacroAssemblerPPC::IfRegisterLT(int reg, int comparand, |
| 923 Label* if_lt) { |
| 924 __ LoadP(r3, register_location(reg), r0); |
| 925 __ Cmpi(r3, Operand(comparand), r0); |
| 926 BranchOrBacktrack(lt, if_lt); |
| 927 } |
| 928 |
| 929 |
| 930 void RegExpMacroAssemblerPPC::IfRegisterEqPos(int reg, Label* if_eq) { |
| 931 __ LoadP(r3, register_location(reg), r0); |
| 932 __ cmp(r3, current_input_offset()); |
| 933 BranchOrBacktrack(eq, if_eq); |
| 934 } |
| 935 |
| 936 |
| 937 RegExpMacroAssembler::IrregexpImplementation |
| 938 RegExpMacroAssemblerPPC::Implementation() { |
| 939 return kPPCImplementation; |
| 940 } |
| 941 |
| 942 |
| 943 void RegExpMacroAssemblerPPC::LoadCurrentCharacter(int cp_offset, |
| 944 Label* on_end_of_input, |
| 945 bool check_bounds, |
| 946 int characters) { |
| 947 DCHECK(cp_offset >= -1); // ^ and \b can look behind one character. |
| 948 DCHECK(cp_offset < (1 << 30)); // Be sane! (And ensure negation works) |
| 949 if (check_bounds) { |
| 950 CheckPosition(cp_offset + characters - 1, on_end_of_input); |
| 951 } |
| 952 LoadCurrentCharacterUnchecked(cp_offset, characters); |
| 953 } |
| 954 |
| 955 |
| 956 void RegExpMacroAssemblerPPC::PopCurrentPosition() { |
| 957 Pop(current_input_offset()); |
| 958 } |
| 959 |
| 960 |
| 961 void RegExpMacroAssemblerPPC::PopRegister(int register_index) { |
| 962 Pop(r3); |
| 963 __ StoreP(r3, register_location(register_index), r0); |
| 964 } |
| 965 |
| 966 |
| 967 void RegExpMacroAssemblerPPC::PushBacktrack(Label* label) { |
| 968 __ mov_label_offset(r3, label); |
| 969 Push(r3); |
| 970 CheckStackLimit(); |
| 971 } |
| 972 |
| 973 |
| 974 void RegExpMacroAssemblerPPC::PushCurrentPosition() { |
| 975 Push(current_input_offset()); |
| 976 } |
| 977 |
| 978 |
| 979 void RegExpMacroAssemblerPPC::PushRegister(int register_index, |
| 980 StackCheckFlag check_stack_limit) { |
| 981 __ LoadP(r3, register_location(register_index), r0); |
| 982 Push(r3); |
| 983 if (check_stack_limit) CheckStackLimit(); |
| 984 } |
| 985 |
| 986 |
| 987 void RegExpMacroAssemblerPPC::ReadCurrentPositionFromRegister(int reg) { |
| 988 __ LoadP(current_input_offset(), register_location(reg), r0); |
| 989 } |
| 990 |
| 991 |
| 992 void RegExpMacroAssemblerPPC::ReadStackPointerFromRegister(int reg) { |
| 993 __ LoadP(backtrack_stackpointer(), register_location(reg), r0); |
| 994 __ LoadP(r3, MemOperand(frame_pointer(), kStackHighEnd)); |
| 995 __ add(backtrack_stackpointer(), backtrack_stackpointer(), r3); |
| 996 } |
| 997 |
| 998 |
| 999 void RegExpMacroAssemblerPPC::SetCurrentPositionFromEnd(int by) { |
| 1000 Label after_position; |
| 1001 __ Cmpi(current_input_offset(), Operand(-by * char_size()), r0); |
| 1002 __ bge(&after_position); |
| 1003 __ mov(current_input_offset(), Operand(-by * char_size())); |
| 1004 // On RegExp code entry (where this operation is used), the character before |
| 1005 // the current position is expected to be already loaded. |
| 1006 // We have advanced the position, so it's safe to read backwards. |
| 1007 LoadCurrentCharacterUnchecked(-1, 1); |
| 1008 __ bind(&after_position); |
| 1009 } |
| 1010 |
| 1011 |
| 1012 void RegExpMacroAssemblerPPC::SetRegister(int register_index, int to) { |
| 1013 DCHECK(register_index >= num_saved_registers_); // Reserved for positions! |
| 1014 __ mov(r3, Operand(to)); |
| 1015 __ StoreP(r3, register_location(register_index), r0); |
| 1016 } |
| 1017 |
| 1018 |
| 1019 bool RegExpMacroAssemblerPPC::Succeed() { |
| 1020 __ b(&success_label_); |
| 1021 return global(); |
| 1022 } |
| 1023 |
| 1024 |
| 1025 void RegExpMacroAssemblerPPC::WriteCurrentPositionToRegister(int reg, |
| 1026 int cp_offset) { |
| 1027 if (cp_offset == 0) { |
| 1028 __ StoreP(current_input_offset(), register_location(reg), r0); |
| 1029 } else { |
| 1030 __ mov(r0, Operand(cp_offset * char_size())); |
| 1031 __ add(r3, current_input_offset(), r0); |
| 1032 __ StoreP(r3, register_location(reg), r0); |
| 1033 } |
| 1034 } |
| 1035 |
| 1036 |
| 1037 void RegExpMacroAssemblerPPC::ClearRegisters(int reg_from, int reg_to) { |
| 1038 DCHECK(reg_from <= reg_to); |
| 1039 __ LoadP(r3, MemOperand(frame_pointer(), kInputStartMinusOne)); |
| 1040 for (int reg = reg_from; reg <= reg_to; reg++) { |
| 1041 __ StoreP(r3, register_location(reg), r0); |
| 1042 } |
| 1043 } |
| 1044 |
| 1045 |
| 1046 void RegExpMacroAssemblerPPC::WriteStackPointerToRegister(int reg) { |
| 1047 __ LoadP(r4, MemOperand(frame_pointer(), kStackHighEnd)); |
| 1048 __ sub(r3, backtrack_stackpointer(), r4); |
| 1049 __ StoreP(r3, register_location(reg), r0); |
| 1050 } |
| 1051 |
| 1052 |
| 1053 // Private methods: |
| 1054 |
| 1055 void RegExpMacroAssemblerPPC::CallCheckStackGuardState(Register scratch) { |
| 1056 int frame_alignment = masm_->ActivationFrameAlignment(); |
| 1057 int stack_space = kNumRequiredStackFrameSlots; |
| 1058 int stack_passed_arguments = 1; // space for return address pointer |
| 1059 |
| 1060 // The following stack manipulation logic is similar to |
| 1061 // PrepareCallCFunction. However, we need an extra slot on the |
| 1062 // stack to house the return address parameter. |
| 1063 if (frame_alignment > kPointerSize) { |
| 1064 // Make stack end at alignment and make room for stack arguments |
| 1065 // -- preserving original value of sp. |
| 1066 __ mr(scratch, sp); |
| 1067 __ addi(sp, sp, Operand(-(stack_passed_arguments + 1) * kPointerSize)); |
| 1068 DCHECK(base::bits::IsPowerOfTwo32(frame_alignment)); |
| 1069 __ ClearRightImm(sp, sp, Operand(WhichPowerOf2(frame_alignment))); |
| 1070 __ StoreP(scratch, MemOperand(sp, stack_passed_arguments * kPointerSize)); |
| 1071 } else { |
| 1072 // Make room for stack arguments |
| 1073 stack_space += stack_passed_arguments; |
| 1074 } |
| 1075 |
| 1076 // Allocate frame with required slots to make ABI work. |
| 1077 __ li(r0, Operand::Zero()); |
| 1078 __ StorePU(r0, MemOperand(sp, -stack_space * kPointerSize)); |
| 1079 |
| 1080 // RegExp code frame pointer. |
| 1081 __ mr(r5, frame_pointer()); |
| 1082 // Code* of self. |
| 1083 __ mov(r4, Operand(masm_->CodeObject())); |
| 1084 // r3 will point to the return address, placed by DirectCEntry. |
| 1085 __ addi(r3, sp, Operand(kStackFrameExtraParamSlot * kPointerSize)); |
| 1086 |
| 1087 ExternalReference stack_guard_check = |
| 1088 ExternalReference::re_check_stack_guard_state(isolate()); |
| 1089 __ mov(ip, Operand(stack_guard_check)); |
| 1090 DirectCEntryStub stub(isolate()); |
| 1091 stub.GenerateCall(masm_, ip); |
| 1092 |
| 1093 // Restore the stack pointer |
| 1094 stack_space = kNumRequiredStackFrameSlots + stack_passed_arguments; |
| 1095 if (frame_alignment > kPointerSize) { |
| 1096 __ LoadP(sp, MemOperand(sp, stack_space * kPointerSize)); |
| 1097 } else { |
| 1098 __ addi(sp, sp, Operand(stack_space * kPointerSize)); |
| 1099 } |
| 1100 |
| 1101 __ mov(code_pointer(), Operand(masm_->CodeObject())); |
| 1102 } |
| 1103 |
| 1104 |
| 1105 // Helper function for reading a value out of a stack frame. |
| 1106 template <typename T> |
| 1107 static T& frame_entry(Address re_frame, int frame_offset) { |
| 1108 return reinterpret_cast<T&>(Memory::int32_at(re_frame + frame_offset)); |
| 1109 } |
| 1110 |
| 1111 |
| 1112 int RegExpMacroAssemblerPPC::CheckStackGuardState(Address* return_address, |
| 1113 Code* re_code, |
| 1114 Address re_frame) { |
| 1115 Isolate* isolate = frame_entry<Isolate*>(re_frame, kIsolate); |
| 1116 StackLimitCheck check(isolate); |
| 1117 if (check.JsHasOverflowed()) { |
| 1118 isolate->StackOverflow(); |
| 1119 return EXCEPTION; |
| 1120 } |
| 1121 |
| 1122 // If not real stack overflow the stack guard was used to interrupt |
| 1123 // execution for another purpose. |
| 1124 |
| 1125 // If this is a direct call from JavaScript retry the RegExp forcing the call |
| 1126 // through the runtime system. Currently the direct call cannot handle a GC. |
| 1127 if (frame_entry<int>(re_frame, kDirectCall) == 1) { |
| 1128 return RETRY; |
| 1129 } |
| 1130 |
| 1131 // Prepare for possible GC. |
| 1132 HandleScope handles(isolate); |
| 1133 Handle<Code> code_handle(re_code); |
| 1134 |
| 1135 Handle<String> subject(frame_entry<String*>(re_frame, kInputString)); |
| 1136 |
| 1137 // Current string. |
| 1138 bool is_one_byte = subject->IsOneByteRepresentationUnderneath(); |
| 1139 |
| 1140 DCHECK(re_code->instruction_start() <= *return_address); |
| 1141 DCHECK(*return_address <= |
| 1142 re_code->instruction_start() + re_code->instruction_size()); |
| 1143 |
| 1144 Object* result = isolate->stack_guard()->HandleInterrupts(); |
| 1145 |
| 1146 if (*code_handle != re_code) { // Return address no longer valid |
| 1147 intptr_t delta = code_handle->address() - re_code->address(); |
| 1148 // Overwrite the return address on the stack. |
| 1149 *return_address += delta; |
| 1150 } |
| 1151 |
| 1152 if (result->IsException()) { |
| 1153 return EXCEPTION; |
| 1154 } |
| 1155 |
| 1156 Handle<String> subject_tmp = subject; |
| 1157 int slice_offset = 0; |
| 1158 |
| 1159 // Extract the underlying string and the slice offset. |
| 1160 if (StringShape(*subject_tmp).IsCons()) { |
| 1161 subject_tmp = Handle<String>(ConsString::cast(*subject_tmp)->first()); |
| 1162 } else if (StringShape(*subject_tmp).IsSliced()) { |
| 1163 SlicedString* slice = SlicedString::cast(*subject_tmp); |
| 1164 subject_tmp = Handle<String>(slice->parent()); |
| 1165 slice_offset = slice->offset(); |
| 1166 } |
| 1167 |
| 1168 // String might have changed. |
| 1169 if (subject_tmp->IsOneByteRepresentation() != is_one_byte) { |
| 1170 // If we changed between an Latin1 and an UC16 string, the specialized |
| 1171 // code cannot be used, and we need to restart regexp matching from |
| 1172 // scratch (including, potentially, compiling a new version of the code). |
| 1173 return RETRY; |
| 1174 } |
| 1175 |
| 1176 // Otherwise, the content of the string might have moved. It must still |
| 1177 // be a sequential or external string with the same content. |
| 1178 // Update the start and end pointers in the stack frame to the current |
| 1179 // location (whether it has actually moved or not). |
| 1180 DCHECK(StringShape(*subject_tmp).IsSequential() || |
| 1181 StringShape(*subject_tmp).IsExternal()); |
| 1182 |
| 1183 // The original start address of the characters to match. |
| 1184 const byte* start_address = frame_entry<const byte*>(re_frame, kInputStart); |
| 1185 |
| 1186 // Find the current start address of the same character at the current string |
| 1187 // position. |
| 1188 int start_index = frame_entry<intptr_t>(re_frame, kStartIndex); |
| 1189 const byte* new_address = |
| 1190 StringCharacterPosition(*subject_tmp, start_index + slice_offset); |
| 1191 |
| 1192 if (start_address != new_address) { |
| 1193 // If there is a difference, update the object pointer and start and end |
| 1194 // addresses in the RegExp stack frame to match the new value. |
| 1195 const byte* end_address = frame_entry<const byte*>(re_frame, kInputEnd); |
| 1196 int byte_length = static_cast<int>(end_address - start_address); |
| 1197 frame_entry<const String*>(re_frame, kInputString) = *subject; |
| 1198 frame_entry<const byte*>(re_frame, kInputStart) = new_address; |
| 1199 frame_entry<const byte*>(re_frame, kInputEnd) = new_address + byte_length; |
| 1200 } else if (frame_entry<const String*>(re_frame, kInputString) != *subject) { |
| 1201 // Subject string might have been a ConsString that underwent |
| 1202 // short-circuiting during GC. That will not change start_address but |
| 1203 // will change pointer inside the subject handle. |
| 1204 frame_entry<const String*>(re_frame, kInputString) = *subject; |
| 1205 } |
| 1206 |
| 1207 return 0; |
| 1208 } |
| 1209 |
| 1210 |
| 1211 MemOperand RegExpMacroAssemblerPPC::register_location(int register_index) { |
| 1212 DCHECK(register_index < (1 << 30)); |
| 1213 if (num_registers_ <= register_index) { |
| 1214 num_registers_ = register_index + 1; |
| 1215 } |
| 1216 return MemOperand(frame_pointer(), |
| 1217 kRegisterZero - register_index * kPointerSize); |
| 1218 } |
| 1219 |
| 1220 |
| 1221 void RegExpMacroAssemblerPPC::CheckPosition(int cp_offset, |
| 1222 Label* on_outside_input) { |
| 1223 __ Cmpi(current_input_offset(), Operand(-cp_offset * char_size()), r0); |
| 1224 BranchOrBacktrack(ge, on_outside_input); |
| 1225 } |
| 1226 |
| 1227 |
| 1228 void RegExpMacroAssemblerPPC::BranchOrBacktrack(Condition condition, Label* to, |
| 1229 CRegister cr) { |
| 1230 if (condition == al) { // Unconditional. |
| 1231 if (to == NULL) { |
| 1232 Backtrack(); |
| 1233 return; |
| 1234 } |
| 1235 __ b(to); |
| 1236 return; |
| 1237 } |
| 1238 if (to == NULL) { |
| 1239 __ b(condition, &backtrack_label_, cr); |
| 1240 return; |
| 1241 } |
| 1242 __ b(condition, to, cr); |
| 1243 } |
| 1244 |
| 1245 |
| 1246 void RegExpMacroAssemblerPPC::SafeCall(Label* to, Condition cond, |
| 1247 CRegister cr) { |
| 1248 __ b(cond, to, cr, SetLK); |
| 1249 } |
| 1250 |
| 1251 |
| 1252 void RegExpMacroAssemblerPPC::SafeReturn() { |
| 1253 __ pop(r0); |
| 1254 __ mov(ip, Operand(masm_->CodeObject())); |
| 1255 __ add(r0, r0, ip); |
| 1256 __ mtlr(r0); |
| 1257 __ blr(); |
| 1258 } |
| 1259 |
| 1260 |
| 1261 void RegExpMacroAssemblerPPC::SafeCallTarget(Label* name) { |
| 1262 __ bind(name); |
| 1263 __ mflr(r0); |
| 1264 __ mov(ip, Operand(masm_->CodeObject())); |
| 1265 __ sub(r0, r0, ip); |
| 1266 __ push(r0); |
| 1267 } |
| 1268 |
| 1269 |
| 1270 void RegExpMacroAssemblerPPC::Push(Register source) { |
| 1271 DCHECK(!source.is(backtrack_stackpointer())); |
| 1272 __ StorePU(source, MemOperand(backtrack_stackpointer(), -kPointerSize)); |
| 1273 } |
| 1274 |
| 1275 |
| 1276 void RegExpMacroAssemblerPPC::Pop(Register target) { |
| 1277 DCHECK(!target.is(backtrack_stackpointer())); |
| 1278 __ LoadP(target, MemOperand(backtrack_stackpointer())); |
| 1279 __ addi(backtrack_stackpointer(), backtrack_stackpointer(), |
| 1280 Operand(kPointerSize)); |
| 1281 } |
| 1282 |
| 1283 |
| 1284 void RegExpMacroAssemblerPPC::CheckPreemption() { |
| 1285 // Check for preemption. |
| 1286 ExternalReference stack_limit = |
| 1287 ExternalReference::address_of_stack_limit(isolate()); |
| 1288 __ mov(r3, Operand(stack_limit)); |
| 1289 __ LoadP(r3, MemOperand(r3)); |
| 1290 __ cmpl(sp, r3); |
| 1291 SafeCall(&check_preempt_label_, le); |
| 1292 } |
| 1293 |
| 1294 |
| 1295 void RegExpMacroAssemblerPPC::CheckStackLimit() { |
| 1296 ExternalReference stack_limit = |
| 1297 ExternalReference::address_of_regexp_stack_limit(isolate()); |
| 1298 __ mov(r3, Operand(stack_limit)); |
| 1299 __ LoadP(r3, MemOperand(r3)); |
| 1300 __ cmpl(backtrack_stackpointer(), r3); |
| 1301 SafeCall(&stack_overflow_label_, le); |
| 1302 } |
| 1303 |
| 1304 |
| 1305 bool RegExpMacroAssemblerPPC::CanReadUnaligned() { |
| 1306 return CpuFeatures::IsSupported(UNALIGNED_ACCESSES) && !slow_safe(); |
| 1307 } |
| 1308 |
| 1309 |
| 1310 void RegExpMacroAssemblerPPC::LoadCurrentCharacterUnchecked(int cp_offset, |
| 1311 int characters) { |
| 1312 Register offset = current_input_offset(); |
| 1313 if (cp_offset != 0) { |
| 1314 // r25 is not being used to store the capture start index at this point. |
| 1315 __ addi(r25, current_input_offset(), Operand(cp_offset * char_size())); |
| 1316 offset = r25; |
| 1317 } |
| 1318 // The lwz, stw, lhz, sth instructions can do unaligned accesses, if the CPU |
| 1319 // and the operating system running on the target allow it. |
| 1320 // We assume we don't want to do unaligned loads on PPC, so this function |
| 1321 // must only be used to load a single character at a time. |
| 1322 |
| 1323 DCHECK(characters == 1); |
| 1324 __ add(current_character(), end_of_input_address(), offset); |
| 1325 if (mode_ == LATIN1) { |
| 1326 __ lbz(current_character(), MemOperand(current_character())); |
| 1327 } else { |
| 1328 DCHECK(mode_ == UC16); |
| 1329 __ lhz(current_character(), MemOperand(current_character())); |
| 1330 } |
| 1331 } |
| 1332 |
| 1333 |
| 1334 #undef __ |
| 1335 |
| 1336 #endif // V8_INTERPRETED_REGEXP |
| 1337 } |
| 1338 } // namespace v8::internal |
| 1339 |
| 1340 #endif // V8_TARGET_ARCH_PPC |
OLD | NEW |