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