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