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