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