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