blob: 27c1300cedc09766b2f703320af13f4ff83ef52d [file] [log] [blame]
Ben Murdoch014dc512016-03-22 12:00:34 +00001// 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
7#include "src/regexp/ia32/regexp-macro-assembler-ia32.h"
8
Rubin Xu6e1e26a2021-02-10 00:04:48 +00009#include "src/codegen/assembler-inl.h"
10#include "src/codegen/macro-assembler.h"
11#include "src/logging/log.h"
12#include "src/objects/objects-inl.h"
Ben Murdoch014dc512016-03-22 12:00:34 +000013#include "src/regexp/regexp-macro-assembler.h"
14#include "src/regexp/regexp-stack.h"
Rubin Xu6e1e26a2021-02-10 00:04:48 +000015#include "src/strings/unicode.h"
Ben Murdoch014dc512016-03-22 12:00:34 +000016
17namespace v8 {
18namespace internal {
19
Ben Murdoch014dc512016-03-22 12:00:34 +000020/*
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:
Rubin Xu6e1e26a2021-02-10 00:04:48 +000037 * - Address regexp (address of the JSRegExp object; unused in
38 * native code, passed to match signature of
39 * the interpreter)
Ben Murdoch014dc512016-03-22 12:00:34 +000040 * - 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 * - stack_area_base (high end of the memory area to use as
44 * backtracking stack)
45 * - capture array size (may fit multiple sets of matches)
46 * - int* capture_array (int[num_saved_registers_], for output).
47 * - end of input (address of end of string)
48 * - start of input (address of first character in string)
49 * - start index (character index of start)
Rubin Xu6e1e26a2021-02-10 00:04:48 +000050 * - String input_string (location of a handle containing the string)
Ben Murdoch014dc512016-03-22 12:00:34 +000051 * --- frame alignment (if applicable) ---
52 * - return address
53 * ebp-> - old ebp
54 * - backup of caller esi
55 * - backup of caller edi
56 * - backup of caller ebx
57 * - success counter (only for global regexps to count matches).
58 * - Offset of location before start of input (effectively character
59 * string start - 1). Used to initialize capture registers to a
60 * non-position.
61 * - register 0 ebp[-4] (only positions must be stored in the first
62 * - register 1 ebp[-8] num_saved_registers_ registers)
63 * - ...
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 starts out as garbage.
68 *
69 * The data up to the return address must be placed there by the calling
70 * code, by calling the code entry as cast to a function with the signature:
Rubin Xu6e1e26a2021-02-10 00:04:48 +000071 * int (*match)(String input_string,
Ben Murdoch014dc512016-03-22 12:00:34 +000072 * int start_index,
73 * Address start,
74 * Address end,
75 * int* capture_output_array,
Rubin Xu2894c6a2019-02-07 16:01:35 +000076 * int num_capture_registers,
Ben Murdoch014dc512016-03-22 12:00:34 +000077 * byte* stack_area_base,
Rubin Xu2894c6a2019-02-07 16:01:35 +000078 * bool direct_call = false,
Rubin Xu6e1e26a2021-02-10 00:04:48 +000079 * Isolate* isolate
80 * Address regexp);
Ben Murdoch014dc512016-03-22 12:00:34 +000081 */
82
83#define __ ACCESS_MASM(masm_)
84
Rubin Xu6e1e26a2021-02-10 00:04:48 +000085const int RegExpMacroAssemblerIA32::kRegExpCodeSize;
86
Ben Murdoch014dc512016-03-22 12:00:34 +000087RegExpMacroAssemblerIA32::RegExpMacroAssemblerIA32(Isolate* isolate, Zone* zone,
88 Mode mode,
89 int registers_to_save)
90 : NativeRegExpMacroAssembler(isolate, zone),
Rubin Xu6e1e26a2021-02-10 00:04:48 +000091 masm_(new MacroAssembler(isolate, CodeObjectRequired::kYes,
92 NewAssemblerBuffer(kRegExpCodeSize))),
Ben Murdoch014dc512016-03-22 12:00:34 +000093 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_() {
Rubin Xu6e1e26a2021-02-10 00:04:48 +0000101 // Irregexp code clobbers ebx and spills/restores it at all boundaries.
102 masm_->set_root_array_available(false);
103
Ben Murdoch014dc512016-03-22 12:00:34 +0000104 DCHECK_EQ(0, registers_to_save % 2);
105 __ jmp(&entry_label_); // We'll write the entry code later.
106 __ bind(&start_label_); // And then continue from here.
107}
108
Ben Murdoch014dc512016-03-22 12:00:34 +0000109RegExpMacroAssemblerIA32::~RegExpMacroAssemblerIA32() {
110 delete masm_;
111 // Unuse labels in case we throw away the assembler without calling GetCode.
112 entry_label_.Unuse();
113 start_label_.Unuse();
114 success_label_.Unuse();
115 backtrack_label_.Unuse();
116 exit_label_.Unuse();
117 check_preempt_label_.Unuse();
118 stack_overflow_label_.Unuse();
Rubin Xu6e1e26a2021-02-10 00:04:48 +0000119 fallback_label_.Unuse();
Ben Murdoch014dc512016-03-22 12:00:34 +0000120}
121
122
123int RegExpMacroAssemblerIA32::stack_limit_slack() {
124 return RegExpStack::kStackLimitSlack;
125}
126
127
128void RegExpMacroAssemblerIA32::AdvanceCurrentPosition(int by) {
129 if (by != 0) {
130 __ add(edi, Immediate(by * char_size()));
131 }
132}
133
134
135void RegExpMacroAssemblerIA32::AdvanceRegister(int reg, int by) {
Rubin Xu2894c6a2019-02-07 16:01:35 +0000136 DCHECK_LE(0, reg);
137 DCHECK_GT(num_registers_, reg);
Ben Murdoch014dc512016-03-22 12:00:34 +0000138 if (by != 0) {
139 __ add(register_location(reg), Immediate(by));
140 }
141}
142
143
144void RegExpMacroAssemblerIA32::Backtrack() {
145 CheckPreemption();
Rubin Xu6e1e26a2021-02-10 00:04:48 +0000146 if (has_backtrack_limit()) {
147 Label next;
148 __ inc(Operand(ebp, kBacktrackCount));
149 __ cmp(Operand(ebp, kBacktrackCount), Immediate(backtrack_limit()));
150 __ j(not_equal, &next);
151
152 // Backtrack limit exceeded.
153 if (can_fallback()) {
154 __ jmp(&fallback_label_);
155 } else {
156 // Can't fallback, so we treat it as a failed match.
157 Fail();
158 }
159
160 __ bind(&next);
161 }
162 // Pop Code offset from backtrack stack, add Code and jump to location.
Ben Murdoch014dc512016-03-22 12:00:34 +0000163 Pop(ebx);
164 __ add(ebx, Immediate(masm_->CodeObject()));
165 __ jmp(ebx);
166}
167
168
169void RegExpMacroAssemblerIA32::Bind(Label* label) {
170 __ bind(label);
171}
172
173
174void RegExpMacroAssemblerIA32::CheckCharacter(uint32_t c, Label* on_equal) {
175 __ cmp(current_character(), c);
176 BranchOrBacktrack(equal, on_equal);
177}
178
179
180void RegExpMacroAssemblerIA32::CheckCharacterGT(uc16 limit, Label* on_greater) {
181 __ cmp(current_character(), limit);
182 BranchOrBacktrack(greater, on_greater);
183}
184
Rubin Xu6e1e26a2021-02-10 00:04:48 +0000185void RegExpMacroAssemblerIA32::CheckAtStart(int cp_offset, Label* on_at_start) {
186 __ lea(eax, Operand(edi, -char_size() + cp_offset * char_size()));
Ben Murdoch014dc512016-03-22 12:00:34 +0000187 __ cmp(eax, Operand(ebp, kStringStartMinusOne));
188 BranchOrBacktrack(equal, on_at_start);
189}
190
Ben Murdoch014dc512016-03-22 12:00:34 +0000191void RegExpMacroAssemblerIA32::CheckNotAtStart(int cp_offset,
192 Label* on_not_at_start) {
193 __ lea(eax, Operand(edi, -char_size() + cp_offset * char_size()));
194 __ cmp(eax, Operand(ebp, kStringStartMinusOne));
195 BranchOrBacktrack(not_equal, on_not_at_start);
196}
197
198
199void RegExpMacroAssemblerIA32::CheckCharacterLT(uc16 limit, Label* on_less) {
200 __ cmp(current_character(), limit);
201 BranchOrBacktrack(less, on_less);
202}
203
204
205void RegExpMacroAssemblerIA32::CheckGreedyLoop(Label* on_equal) {
206 Label fallthrough;
207 __ cmp(edi, Operand(backtrack_stackpointer(), 0));
208 __ j(not_equal, &fallthrough);
Rubin Xu6e1e26a2021-02-10 00:04:48 +0000209 __ add(backtrack_stackpointer(), Immediate(kSystemPointerSize)); // Pop.
Ben Murdoch014dc512016-03-22 12:00:34 +0000210 BranchOrBacktrack(no_condition, on_equal);
211 __ bind(&fallthrough);
212}
213
Ben Murdoch014dc512016-03-22 12:00:34 +0000214void RegExpMacroAssemblerIA32::CheckNotBackReferenceIgnoreCase(
Ben Murdoch109988c2016-05-18 11:27:45 +0100215 int start_reg, bool read_backward, bool unicode, Label* on_no_match) {
Ben Murdoch014dc512016-03-22 12:00:34 +0000216 Label fallthrough;
217 __ mov(edx, register_location(start_reg)); // Index of start of capture
218 __ mov(ebx, register_location(start_reg + 1)); // Index of end of capture
219 __ sub(ebx, edx); // Length of capture.
220
221 // At this point, the capture registers are either both set or both cleared.
222 // If the capture length is zero, then the capture is either empty or cleared.
223 // Fall through in both cases.
224 __ j(equal, &fallthrough);
225
226 // Check that there are sufficient characters left in the input.
227 if (read_backward) {
228 __ mov(eax, Operand(ebp, kStringStartMinusOne));
229 __ add(eax, ebx);
230 __ cmp(edi, eax);
231 BranchOrBacktrack(less_equal, on_no_match);
232 } else {
233 __ mov(eax, edi);
234 __ add(eax, ebx);
235 BranchOrBacktrack(greater, on_no_match);
236 }
237
238 if (mode_ == LATIN1) {
239 Label success;
240 Label fail;
241 Label loop_increment;
242 // Save register contents to make the registers available below.
243 __ push(edi);
244 __ push(backtrack_stackpointer());
245 // After this, the eax, ecx, and edi registers are available.
246
247 __ add(edx, esi); // Start of capture
248 __ add(edi, esi); // Start of text to match against capture.
249 if (read_backward) {
250 __ sub(edi, ebx); // Offset by length when matching backwards.
251 }
252 __ add(ebx, edi); // End of text to match against capture.
253
254 Label loop;
255 __ bind(&loop);
256 __ movzx_b(eax, Operand(edi, 0));
257 __ cmpb_al(Operand(edx, 0));
258 __ j(equal, &loop_increment);
259
260 // Mismatch, try case-insensitive match (converting letters to lower-case).
261 __ or_(eax, 0x20); // Convert match character to lower-case.
262 __ lea(ecx, Operand(eax, -'a'));
263 __ cmp(ecx, static_cast<int32_t>('z' - 'a')); // Is eax a lowercase letter?
264 Label convert_capture;
265 __ j(below_equal, &convert_capture); // In range 'a'-'z'.
266 // Latin-1: Check for values in range [224,254] but not 247.
267 __ sub(ecx, Immediate(224 - 'a'));
268 __ cmp(ecx, Immediate(254 - 224));
269 __ j(above, &fail); // Weren't Latin-1 letters.
270 __ cmp(ecx, Immediate(247 - 224)); // Check for 247.
271 __ j(equal, &fail);
272 __ bind(&convert_capture);
273 // Also convert capture character.
274 __ movzx_b(ecx, Operand(edx, 0));
275 __ or_(ecx, 0x20);
276
277 __ cmp(eax, ecx);
278 __ j(not_equal, &fail);
279
280 __ bind(&loop_increment);
281 // Increment pointers into match and capture strings.
282 __ add(edx, Immediate(1));
283 __ add(edi, Immediate(1));
284 // Compare to end of match, and loop if not done.
285 __ cmp(edi, ebx);
286 __ j(below, &loop);
287 __ jmp(&success);
288
289 __ bind(&fail);
290 // Restore original values before failing.
291 __ pop(backtrack_stackpointer());
292 __ pop(edi);
293 BranchOrBacktrack(no_condition, on_no_match);
294
295 __ bind(&success);
296 // Restore original value before continuing.
297 __ pop(backtrack_stackpointer());
298 // Drop original value of character position.
Rubin Xu6e1e26a2021-02-10 00:04:48 +0000299 __ add(esp, Immediate(kSystemPointerSize));
Ben Murdoch014dc512016-03-22 12:00:34 +0000300 // Compute new value of character position after the matched part.
301 __ sub(edi, esi);
302 if (read_backward) {
303 // Subtract match length if we matched backward.
304 __ add(edi, register_location(start_reg));
305 __ sub(edi, register_location(start_reg + 1));
306 }
307 } else {
308 DCHECK(mode_ == UC16);
309 // Save registers before calling C function.
310 __ push(esi);
311 __ push(edi);
312 __ push(backtrack_stackpointer());
313 __ push(ebx);
314
315 static const int argument_count = 4;
316 __ PrepareCallCFunction(argument_count, ecx);
317 // Put arguments into allocated stack area, last argument highest on stack.
318 // Parameters are
319 // Address byte_offset1 - Address captured substring's start.
320 // Address byte_offset2 - Address of current character position.
321 // size_t byte_length - length of capture in bytes(!)
Rubin Xu6e1e26a2021-02-10 00:04:48 +0000322 // Isolate* isolate.
Ben Murdoch014dc512016-03-22 12:00:34 +0000323
324 // Set isolate.
Rubin Xu6e1e26a2021-02-10 00:04:48 +0000325 __ mov(Operand(esp, 3 * kSystemPointerSize),
326 Immediate(ExternalReference::isolate_address(isolate())));
Ben Murdoch014dc512016-03-22 12:00:34 +0000327 // Set byte_length.
Rubin Xu6e1e26a2021-02-10 00:04:48 +0000328 __ mov(Operand(esp, 2 * kSystemPointerSize), ebx);
Ben Murdoch014dc512016-03-22 12:00:34 +0000329 // Set byte_offset2.
330 // Found by adding negative string-end offset of current position (edi)
331 // to end of string.
332 __ add(edi, esi);
333 if (read_backward) {
334 __ sub(edi, ebx); // Offset by length when matching backwards.
335 }
Rubin Xu6e1e26a2021-02-10 00:04:48 +0000336 __ mov(Operand(esp, 1 * kSystemPointerSize), edi);
Ben Murdoch014dc512016-03-22 12:00:34 +0000337 // Set byte_offset1.
338 // Start of capture, where edx already holds string-end negative offset.
339 __ add(edx, esi);
Rubin Xu6e1e26a2021-02-10 00:04:48 +0000340 __ mov(Operand(esp, 0 * kSystemPointerSize), edx);
Ben Murdoch014dc512016-03-22 12:00:34 +0000341
342 {
343 AllowExternalCallThatCantCauseGC scope(masm_);
344 ExternalReference compare =
Rubin Xu6e1e26a2021-02-10 00:04:48 +0000345 unicode ? ExternalReference::re_case_insensitive_compare_unicode(
346 isolate())
347 : ExternalReference::re_case_insensitive_compare_non_unicode(
348 isolate());
Ben Murdoch014dc512016-03-22 12:00:34 +0000349 __ CallCFunction(compare, argument_count);
350 }
351 // Pop original values before reacting on result value.
352 __ pop(ebx);
353 __ pop(backtrack_stackpointer());
354 __ pop(edi);
355 __ pop(esi);
356
357 // Check if function returned non-zero for success or zero for failure.
358 __ or_(eax, eax);
359 BranchOrBacktrack(zero, on_no_match);
360 // On success, advance position by length of capture.
361 if (read_backward) {
362 __ sub(edi, ebx);
363 } else {
364 __ add(edi, ebx);
365 }
366 }
367 __ bind(&fallthrough);
368}
369
Ben Murdoch014dc512016-03-22 12:00:34 +0000370void RegExpMacroAssemblerIA32::CheckNotBackReference(int start_reg,
371 bool read_backward,
372 Label* on_no_match) {
373 Label fallthrough;
374 Label success;
375 Label fail;
376
377 // Find length of back-referenced capture.
378 __ mov(edx, register_location(start_reg));
379 __ mov(eax, register_location(start_reg + 1));
380 __ sub(eax, edx); // Length to check.
381
382 // At this point, the capture registers are either both set or both cleared.
383 // If the capture length is zero, then the capture is either empty or cleared.
384 // Fall through in both cases.
385 __ j(equal, &fallthrough);
386
387 // Check that there are sufficient characters left in the input.
388 if (read_backward) {
389 __ mov(ebx, Operand(ebp, kStringStartMinusOne));
390 __ add(ebx, eax);
391 __ cmp(edi, ebx);
392 BranchOrBacktrack(less_equal, on_no_match);
393 } else {
394 __ mov(ebx, edi);
395 __ add(ebx, eax);
396 BranchOrBacktrack(greater, on_no_match);
397 }
398
399 // Save register to make it available below.
400 __ push(backtrack_stackpointer());
401
402 // Compute pointers to match string and capture string
403 __ add(edx, esi); // Start of capture.
404 __ lea(ebx, Operand(esi, edi, times_1, 0)); // Start of match.
405 if (read_backward) {
406 __ sub(ebx, eax); // Offset by length when matching backwards.
407 }
408 __ lea(ecx, Operand(eax, ebx, times_1, 0)); // End of match
409
410 Label loop;
411 __ bind(&loop);
412 if (mode_ == LATIN1) {
413 __ movzx_b(eax, Operand(edx, 0));
414 __ cmpb_al(Operand(ebx, 0));
415 } else {
416 DCHECK(mode_ == UC16);
417 __ movzx_w(eax, Operand(edx, 0));
418 __ cmpw_ax(Operand(ebx, 0));
419 }
420 __ j(not_equal, &fail);
421 // Increment pointers into capture and match string.
422 __ add(edx, Immediate(char_size()));
423 __ add(ebx, Immediate(char_size()));
424 // Check if we have reached end of match area.
425 __ cmp(ebx, ecx);
426 __ j(below, &loop);
427 __ jmp(&success);
428
429 __ bind(&fail);
430 // Restore backtrack stackpointer.
431 __ pop(backtrack_stackpointer());
432 BranchOrBacktrack(no_condition, on_no_match);
433
434 __ bind(&success);
435 // Move current character position to position after match.
436 __ mov(edi, ecx);
437 __ sub(edi, esi);
438 if (read_backward) {
439 // Subtract match length if we matched backward.
440 __ add(edi, register_location(start_reg));
441 __ sub(edi, register_location(start_reg + 1));
442 }
443 // Restore backtrack stackpointer.
444 __ pop(backtrack_stackpointer());
445
446 __ bind(&fallthrough);
447}
448
449
450void RegExpMacroAssemblerIA32::CheckNotCharacter(uint32_t c,
451 Label* on_not_equal) {
452 __ cmp(current_character(), c);
453 BranchOrBacktrack(not_equal, on_not_equal);
454}
455
456
457void RegExpMacroAssemblerIA32::CheckCharacterAfterAnd(uint32_t c,
458 uint32_t mask,
459 Label* on_equal) {
460 if (c == 0) {
461 __ test(current_character(), Immediate(mask));
462 } else {
463 __ mov(eax, mask);
464 __ and_(eax, current_character());
465 __ cmp(eax, c);
466 }
467 BranchOrBacktrack(equal, on_equal);
468}
469
470
471void RegExpMacroAssemblerIA32::CheckNotCharacterAfterAnd(uint32_t c,
472 uint32_t mask,
473 Label* on_not_equal) {
474 if (c == 0) {
475 __ test(current_character(), Immediate(mask));
476 } else {
477 __ mov(eax, mask);
478 __ and_(eax, current_character());
479 __ cmp(eax, c);
480 }
481 BranchOrBacktrack(not_equal, on_not_equal);
482}
483
484
485void RegExpMacroAssemblerIA32::CheckNotCharacterAfterMinusAnd(
486 uc16 c,
487 uc16 minus,
488 uc16 mask,
489 Label* on_not_equal) {
Rubin Xu2894c6a2019-02-07 16:01:35 +0000490 DCHECK_GT(String::kMaxUtf16CodeUnit, minus);
Ben Murdoch014dc512016-03-22 12:00:34 +0000491 __ lea(eax, Operand(current_character(), -minus));
492 if (c == 0) {
493 __ test(eax, Immediate(mask));
494 } else {
495 __ and_(eax, mask);
496 __ cmp(eax, c);
497 }
498 BranchOrBacktrack(not_equal, on_not_equal);
499}
500
501
502void RegExpMacroAssemblerIA32::CheckCharacterInRange(
503 uc16 from,
504 uc16 to,
505 Label* on_in_range) {
506 __ lea(eax, Operand(current_character(), -from));
507 __ cmp(eax, to - from);
508 BranchOrBacktrack(below_equal, on_in_range);
509}
510
511
512void RegExpMacroAssemblerIA32::CheckCharacterNotInRange(
513 uc16 from,
514 uc16 to,
515 Label* on_not_in_range) {
516 __ lea(eax, Operand(current_character(), -from));
517 __ cmp(eax, to - from);
518 BranchOrBacktrack(above, on_not_in_range);
519}
520
521
522void RegExpMacroAssemblerIA32::CheckBitInTable(
523 Handle<ByteArray> table,
524 Label* on_bit_set) {
525 __ mov(eax, Immediate(table));
526 Register index = current_character();
527 if (mode_ != LATIN1 || kTableMask != String::kMaxOneByteCharCode) {
528 __ mov(ebx, kTableSize - 1);
529 __ and_(ebx, current_character());
530 index = ebx;
531 }
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100532 __ cmpb(FieldOperand(eax, index, times_1, ByteArray::kHeaderSize),
533 Immediate(0));
Ben Murdoch014dc512016-03-22 12:00:34 +0000534 BranchOrBacktrack(not_equal, on_bit_set);
535}
536
537
538bool RegExpMacroAssemblerIA32::CheckSpecialCharacterClass(uc16 type,
539 Label* on_no_match) {
540 // Range checks (c in min..max) are generally implemented by an unsigned
541 // (c - min) <= (max - min) check
542 switch (type) {
543 case 's':
544 // Match space-characters
545 if (mode_ == LATIN1) {
546 // One byte space characters are '\t'..'\r', ' ' and \u00a0.
547 Label success;
548 __ cmp(current_character(), ' ');
549 __ j(equal, &success, Label::kNear);
Rubin Xu2894c6a2019-02-07 16:01:35 +0000550 // Check range 0x09..0x0D
Ben Murdoch014dc512016-03-22 12:00:34 +0000551 __ lea(eax, Operand(current_character(), -'\t'));
552 __ cmp(eax, '\r' - '\t');
553 __ j(below_equal, &success, Label::kNear);
554 // \u00a0 (NBSP).
Rubin Xu2894c6a2019-02-07 16:01:35 +0000555 __ cmp(eax, 0x00A0 - '\t');
Ben Murdoch014dc512016-03-22 12:00:34 +0000556 BranchOrBacktrack(not_equal, on_no_match);
557 __ bind(&success);
558 return true;
559 }
560 return false;
561 case 'S':
562 // The emitted code for generic character classes is good enough.
563 return false;
564 case 'd':
565 // Match ASCII digits ('0'..'9')
566 __ lea(eax, Operand(current_character(), -'0'));
567 __ cmp(eax, '9' - '0');
568 BranchOrBacktrack(above, on_no_match);
569 return true;
570 case 'D':
571 // Match non ASCII-digits
572 __ lea(eax, Operand(current_character(), -'0'));
573 __ cmp(eax, '9' - '0');
574 BranchOrBacktrack(below_equal, on_no_match);
575 return true;
576 case '.': {
Rubin Xu2894c6a2019-02-07 16:01:35 +0000577 // Match non-newlines (not 0x0A('\n'), 0x0D('\r'), 0x2028 and 0x2029)
Ben Murdoch014dc512016-03-22 12:00:34 +0000578 __ mov(eax, current_character());
579 __ xor_(eax, Immediate(0x01));
Rubin Xu2894c6a2019-02-07 16:01:35 +0000580 // See if current character is '\n'^1 or '\r'^1, i.e., 0x0B or 0x0C
581 __ sub(eax, Immediate(0x0B));
582 __ cmp(eax, 0x0C - 0x0B);
Ben Murdoch014dc512016-03-22 12:00:34 +0000583 BranchOrBacktrack(below_equal, on_no_match);
584 if (mode_ == UC16) {
585 // Compare original value to 0x2028 and 0x2029, using the already
Rubin Xu2894c6a2019-02-07 16:01:35 +0000586 // computed (current_char ^ 0x01 - 0x0B). I.e., check for
587 // 0x201D (0x2028 - 0x0B) or 0x201E.
588 __ sub(eax, Immediate(0x2028 - 0x0B));
Ben Murdoch014dc512016-03-22 12:00:34 +0000589 __ cmp(eax, 0x2029 - 0x2028);
590 BranchOrBacktrack(below_equal, on_no_match);
591 }
592 return true;
593 }
594 case 'w': {
595 if (mode_ != LATIN1) {
596 // Table is 256 entries, so all Latin1 characters can be tested.
597 __ cmp(current_character(), Immediate('z'));
598 BranchOrBacktrack(above, on_no_match);
599 }
600 DCHECK_EQ(0, word_character_map[0]); // Character '\0' is not a word char.
Rubin Xu2894c6a2019-02-07 16:01:35 +0000601 ExternalReference word_map =
602 ExternalReference::re_word_character_map(isolate());
Ben Murdoch014dc512016-03-22 12:00:34 +0000603 __ test_b(current_character(),
Rubin Xu6e1e26a2021-02-10 00:04:48 +0000604 Operand(current_character(), times_1, word_map.address(),
605 RelocInfo::EXTERNAL_REFERENCE));
Ben Murdoch014dc512016-03-22 12:00:34 +0000606 BranchOrBacktrack(zero, on_no_match);
607 return true;
608 }
609 case 'W': {
610 Label done;
611 if (mode_ != LATIN1) {
612 // Table is 256 entries, so all Latin1 characters can be tested.
613 __ cmp(current_character(), Immediate('z'));
614 __ j(above, &done);
615 }
616 DCHECK_EQ(0, word_character_map[0]); // Character '\0' is not a word char.
Rubin Xu2894c6a2019-02-07 16:01:35 +0000617 ExternalReference word_map =
618 ExternalReference::re_word_character_map(isolate());
Ben Murdoch014dc512016-03-22 12:00:34 +0000619 __ test_b(current_character(),
Rubin Xu6e1e26a2021-02-10 00:04:48 +0000620 Operand(current_character(), times_1, word_map.address(),
621 RelocInfo::EXTERNAL_REFERENCE));
Ben Murdoch014dc512016-03-22 12:00:34 +0000622 BranchOrBacktrack(not_zero, on_no_match);
623 if (mode_ != LATIN1) {
624 __ bind(&done);
625 }
626 return true;
627 }
628 // Non-standard classes (with no syntactic shorthand) used internally.
629 case '*':
630 // Match any character.
631 return true;
632 case 'n': {
Rubin Xu2894c6a2019-02-07 16:01:35 +0000633 // Match newlines (0x0A('\n'), 0x0D('\r'), 0x2028 or 0x2029).
Ben Murdoch014dc512016-03-22 12:00:34 +0000634 // The opposite of '.'.
635 __ mov(eax, current_character());
636 __ xor_(eax, Immediate(0x01));
Rubin Xu2894c6a2019-02-07 16:01:35 +0000637 // See if current character is '\n'^1 or '\r'^1, i.e., 0x0B or 0x0C
638 __ sub(eax, Immediate(0x0B));
639 __ cmp(eax, 0x0C - 0x0B);
Ben Murdoch014dc512016-03-22 12:00:34 +0000640 if (mode_ == LATIN1) {
641 BranchOrBacktrack(above, on_no_match);
642 } else {
643 Label done;
644 BranchOrBacktrack(below_equal, &done);
645 DCHECK_EQ(UC16, mode_);
646 // Compare original value to 0x2028 and 0x2029, using the already
Rubin Xu2894c6a2019-02-07 16:01:35 +0000647 // computed (current_char ^ 0x01 - 0x0B). I.e., check for
648 // 0x201D (0x2028 - 0x0B) or 0x201E.
649 __ sub(eax, Immediate(0x2028 - 0x0B));
Ben Murdoch014dc512016-03-22 12:00:34 +0000650 __ cmp(eax, 1);
651 BranchOrBacktrack(above, on_no_match);
652 __ bind(&done);
653 }
654 return true;
655 }
656 // No custom implementation (yet): s(UC16), S(UC16).
657 default:
658 return false;
659 }
660}
661
662
663void RegExpMacroAssemblerIA32::Fail() {
664 STATIC_ASSERT(FAILURE == 0); // Return value for failure is zero.
665 if (!global()) {
666 __ Move(eax, Immediate(FAILURE));
667 }
668 __ jmp(&exit_label_);
669}
670
671
672Handle<HeapObject> RegExpMacroAssemblerIA32::GetCode(Handle<String> source) {
673 Label return_eax;
674 // Finalize code - write the entry point code now we know how many
675 // registers we need.
676
677 // Entry code:
678 __ bind(&entry_label_);
679
680 // Tell the system that we have a stack frame. Because the type is MANUAL, no
681 // code is generated.
682 FrameScope scope(masm_, StackFrame::MANUAL);
683
684 // Actually emit code to start a new stack frame.
685 __ push(ebp);
686 __ mov(ebp, esp);
687 // Save callee-save registers. Order here should correspond to order of
688 // kBackup_ebx etc.
689 __ push(esi);
690 __ push(edi);
691 __ push(ebx); // Callee-save on MacOS.
Rubin Xu6e1e26a2021-02-10 00:04:48 +0000692
693 STATIC_ASSERT(kSuccessfulCaptures == kBackup_ebx - kSystemPointerSize);
Ben Murdoch014dc512016-03-22 12:00:34 +0000694 __ push(Immediate(0)); // Number of successful matches in a global regexp.
Rubin Xu6e1e26a2021-02-10 00:04:48 +0000695 STATIC_ASSERT(kStringStartMinusOne ==
696 kSuccessfulCaptures - kSystemPointerSize);
Ben Murdoch014dc512016-03-22 12:00:34 +0000697 __ push(Immediate(0)); // Make room for "string start - 1" constant.
Rubin Xu6e1e26a2021-02-10 00:04:48 +0000698 STATIC_ASSERT(kBacktrackCount == kStringStartMinusOne - kSystemPointerSize);
699 __ push(Immediate(0)); // The backtrack counter.
Ben Murdoch014dc512016-03-22 12:00:34 +0000700
701 // Check if we have space on the stack for registers.
702 Label stack_limit_hit;
703 Label stack_ok;
704
705 ExternalReference stack_limit =
Rubin Xu6e1e26a2021-02-10 00:04:48 +0000706 ExternalReference::address_of_jslimit(isolate());
Ben Murdoch014dc512016-03-22 12:00:34 +0000707 __ mov(ecx, esp);
Rubin Xu6e1e26a2021-02-10 00:04:48 +0000708 __ sub(ecx, StaticVariable(stack_limit));
Ben Murdoch014dc512016-03-22 12:00:34 +0000709 // Handle it if the stack pointer is already below the stack limit.
710 __ j(below_equal, &stack_limit_hit);
711 // Check if there is room for the variable number of registers above
712 // the stack limit.
Rubin Xu6e1e26a2021-02-10 00:04:48 +0000713 __ cmp(ecx, num_registers_ * kSystemPointerSize);
Ben Murdoch014dc512016-03-22 12:00:34 +0000714 __ j(above_equal, &stack_ok);
715 // Exit with OutOfMemory exception. There is not enough space on the stack
716 // for our working registers.
717 __ mov(eax, EXCEPTION);
718 __ jmp(&return_eax);
719
720 __ bind(&stack_limit_hit);
721 CallCheckStackGuardState(ebx);
722 __ or_(eax, eax);
723 // If returned value is non-zero, we exit with the returned value as result.
724 __ j(not_zero, &return_eax);
725
726 __ bind(&stack_ok);
727 // Load start index for later use.
728 __ mov(ebx, Operand(ebp, kStartIndex));
729
730 // Allocate space on stack for registers.
Rubin Xu6e1e26a2021-02-10 00:04:48 +0000731 __ AllocateStackSpace(num_registers_ * kSystemPointerSize);
Ben Murdoch014dc512016-03-22 12:00:34 +0000732 // Load string length.
733 __ mov(esi, Operand(ebp, kInputEnd));
734 // Load input position.
735 __ mov(edi, Operand(ebp, kInputStart));
736 // Set up edi to be negative offset from string end.
737 __ sub(edi, esi);
738
739 // Set eax to address of char before start of the string.
740 // (effectively string position -1).
741 __ neg(ebx);
742 if (mode_ == UC16) {
743 __ lea(eax, Operand(edi, ebx, times_2, -char_size()));
744 } else {
745 __ lea(eax, Operand(edi, ebx, times_1, -char_size()));
746 }
747 // Store this value in a local variable, for use when clearing
748 // position registers.
749 __ mov(Operand(ebp, kStringStartMinusOne), eax);
750
Ben Murdoch014dc512016-03-22 12:00:34 +0000751 Label load_char_start_regexp, start_regexp;
752 // Load newline if index is at start, previous character otherwise.
753 __ cmp(Operand(ebp, kStartIndex), Immediate(0));
754 __ j(not_equal, &load_char_start_regexp, Label::kNear);
755 __ mov(current_character(), '\n');
756 __ jmp(&start_regexp, Label::kNear);
757
758 // Global regexp restarts matching here.
759 __ bind(&load_char_start_regexp);
760 // Load previous char as initial value of current character register.
761 LoadCurrentCharacterUnchecked(-1, 1);
762 __ bind(&start_regexp);
763
764 // Initialize on-stack registers.
765 if (num_saved_registers_ > 0) { // Always is, if generated from a regexp.
766 // Fill saved registers with initial value = start offset - 1
767 // Fill in stack push order, to avoid accessing across an unwritten
768 // page (a problem on Windows).
769 if (num_saved_registers_ > 8) {
770 __ mov(ecx, kRegisterZero);
771 Label init_loop;
772 __ bind(&init_loop);
773 __ mov(Operand(ebp, ecx, times_1, 0), eax);
Rubin Xu6e1e26a2021-02-10 00:04:48 +0000774 __ sub(ecx, Immediate(kSystemPointerSize));
775 __ cmp(ecx, kRegisterZero - num_saved_registers_ * kSystemPointerSize);
Ben Murdoch014dc512016-03-22 12:00:34 +0000776 __ j(greater, &init_loop);
777 } else { // Unroll the loop.
778 for (int i = 0; i < num_saved_registers_; i++) {
779 __ mov(register_location(i), eax);
780 }
781 }
782 }
783
784 // Initialize backtrack stack pointer.
785 __ mov(backtrack_stackpointer(), Operand(ebp, kStackHighEnd));
786
787 __ jmp(&start_label_);
788
789 // Exit code:
790 if (success_label_.is_linked()) {
791 // Save captures when successful.
792 __ bind(&success_label_);
793 if (num_saved_registers_ > 0) {
794 // copy captures to output
795 __ mov(ebx, Operand(ebp, kRegisterOutput));
796 __ mov(ecx, Operand(ebp, kInputEnd));
797 __ mov(edx, Operand(ebp, kStartIndex));
798 __ sub(ecx, Operand(ebp, kInputStart));
799 if (mode_ == UC16) {
800 __ lea(ecx, Operand(ecx, edx, times_2, 0));
801 } else {
802 __ add(ecx, edx);
803 }
804 for (int i = 0; i < num_saved_registers_; i++) {
805 __ mov(eax, register_location(i));
806 if (i == 0 && global_with_zero_length_check()) {
807 // Keep capture start in edx for the zero-length check later.
808 __ mov(edx, eax);
809 }
810 // Convert to index from start of string, not end.
811 __ add(eax, ecx);
812 if (mode_ == UC16) {
813 __ sar(eax, 1); // Convert byte index to character index.
814 }
Rubin Xu6e1e26a2021-02-10 00:04:48 +0000815 __ mov(Operand(ebx, i * kSystemPointerSize), eax);
Ben Murdoch014dc512016-03-22 12:00:34 +0000816 }
817 }
818
819 if (global()) {
820 // Restart matching if the regular expression is flagged as global.
821 // Increment success counter.
822 __ inc(Operand(ebp, kSuccessfulCaptures));
823 // Capture results have been stored, so the number of remaining global
824 // output registers is reduced by the number of stored captures.
825 __ mov(ecx, Operand(ebp, kNumOutputRegisters));
826 __ sub(ecx, Immediate(num_saved_registers_));
827 // Check whether we have enough room for another set of capture results.
828 __ cmp(ecx, Immediate(num_saved_registers_));
829 __ j(less, &exit_label_);
830
831 __ mov(Operand(ebp, kNumOutputRegisters), ecx);
832 // Advance the location for output.
833 __ add(Operand(ebp, kRegisterOutput),
Rubin Xu6e1e26a2021-02-10 00:04:48 +0000834 Immediate(num_saved_registers_ * kSystemPointerSize));
Ben Murdoch014dc512016-03-22 12:00:34 +0000835
836 // Prepare eax to initialize registers with its value in the next run.
837 __ mov(eax, Operand(ebp, kStringStartMinusOne));
838
839 if (global_with_zero_length_check()) {
840 // Special case for zero-length matches.
841 // edx: capture start index
842 __ cmp(edi, edx);
843 // Not a zero-length match, restart.
844 __ j(not_equal, &load_char_start_regexp);
845 // edi (offset from the end) is zero if we already reached the end.
846 __ test(edi, edi);
847 __ j(zero, &exit_label_, Label::kNear);
848 // Advance current position after a zero-length match.
Ben Murdoch109988c2016-05-18 11:27:45 +0100849 Label advance;
850 __ bind(&advance);
Ben Murdoch014dc512016-03-22 12:00:34 +0000851 if (mode_ == UC16) {
852 __ add(edi, Immediate(2));
853 } else {
854 __ inc(edi);
855 }
Ben Murdoch109988c2016-05-18 11:27:45 +0100856 if (global_unicode()) CheckNotInSurrogatePair(0, &advance);
Ben Murdoch014dc512016-03-22 12:00:34 +0000857 }
Ben Murdoch014dc512016-03-22 12:00:34 +0000858 __ jmp(&load_char_start_regexp);
859 } else {
860 __ mov(eax, Immediate(SUCCESS));
861 }
862 }
863
864 __ bind(&exit_label_);
865 if (global()) {
866 // Return the number of successful captures.
867 __ mov(eax, Operand(ebp, kSuccessfulCaptures));
868 }
869
870 __ bind(&return_eax);
871 // Skip esp past regexp registers.
872 __ lea(esp, Operand(ebp, kBackup_ebx));
873 // Restore callee-save registers.
874 __ pop(ebx);
875 __ pop(edi);
876 __ pop(esi);
877 // Exit function frame, restore previous one.
878 __ pop(ebp);
879 __ ret(0);
880
881 // Backtrack code (branch target for conditional backtracks).
882 if (backtrack_label_.is_linked()) {
883 __ bind(&backtrack_label_);
884 Backtrack();
885 }
886
887 Label exit_with_exception;
888
889 // Preempt-code
890 if (check_preempt_label_.is_linked()) {
891 SafeCallTarget(&check_preempt_label_);
892
893 __ push(backtrack_stackpointer());
894 __ push(edi);
895
896 CallCheckStackGuardState(ebx);
897 __ or_(eax, eax);
898 // If returning non-zero, we should end execution with the given
899 // result as return value.
900 __ j(not_zero, &return_eax);
901
902 __ pop(edi);
903 __ pop(backtrack_stackpointer());
904 // String might have moved: Reload esi from frame.
905 __ mov(esi, Operand(ebp, kInputEnd));
906 SafeReturn();
907 }
908
909 // Backtrack stack overflow code.
910 if (stack_overflow_label_.is_linked()) {
911 SafeCallTarget(&stack_overflow_label_);
912 // Reached if the backtrack-stack limit has been hit.
913
Ben Murdoch014dc512016-03-22 12:00:34 +0000914 // Save registers before calling C function
915 __ push(esi);
916 __ push(edi);
917
918 // Call GrowStack(backtrack_stackpointer())
919 static const int num_arguments = 3;
920 __ PrepareCallCFunction(num_arguments, ebx);
Rubin Xu6e1e26a2021-02-10 00:04:48 +0000921 __ mov(Operand(esp, 2 * kSystemPointerSize),
Ben Murdoch014dc512016-03-22 12:00:34 +0000922 Immediate(ExternalReference::isolate_address(isolate())));
923 __ lea(eax, Operand(ebp, kStackHighEnd));
Rubin Xu6e1e26a2021-02-10 00:04:48 +0000924 __ mov(Operand(esp, 1 * kSystemPointerSize), eax);
925 __ mov(Operand(esp, 0 * kSystemPointerSize), backtrack_stackpointer());
Ben Murdoch014dc512016-03-22 12:00:34 +0000926 ExternalReference grow_stack =
927 ExternalReference::re_grow_stack(isolate());
928 __ CallCFunction(grow_stack, num_arguments);
Rubin Xu2894c6a2019-02-07 16:01:35 +0000929 // If return nullptr, we have failed to grow the stack, and
Ben Murdoch014dc512016-03-22 12:00:34 +0000930 // must exit with a stack-overflow exception.
931 __ or_(eax, eax);
932 __ j(equal, &exit_with_exception);
933 // Otherwise use return value as new stack pointer.
934 __ mov(backtrack_stackpointer(), eax);
935 // Restore saved registers and continue.
936 __ pop(edi);
937 __ pop(esi);
938 SafeReturn();
939 }
940
941 if (exit_with_exception.is_linked()) {
942 // If any of the code above needed to exit with an exception.
943 __ bind(&exit_with_exception);
944 // Exit with Result EXCEPTION(-1) to signal thrown exception.
945 __ mov(eax, EXCEPTION);
946 __ jmp(&return_eax);
947 }
948
Rubin Xu6e1e26a2021-02-10 00:04:48 +0000949 if (fallback_label_.is_linked()) {
950 __ bind(&fallback_label_);
951 __ mov(eax, FALLBACK_TO_EXPERIMENTAL);
952 __ jmp(&return_eax);
953 }
954
Ben Murdoch014dc512016-03-22 12:00:34 +0000955 CodeDesc code_desc;
Rubin Xu2894c6a2019-02-07 16:01:35 +0000956 masm_->GetCode(masm_->isolate(), &code_desc);
Rubin Xu6e1e26a2021-02-10 00:04:48 +0000957 Handle<Code> code =
958 Factory::CodeBuilder(isolate(), code_desc, CodeKind::REGEXP)
959 .set_self_reference(masm_->CodeObject())
960 .Build();
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100961 PROFILE(masm_->isolate(),
Rubin Xu6e1e26a2021-02-10 00:04:48 +0000962 RegExpCodeCreateEvent(Handle<AbstractCode>::cast(code), source));
Ben Murdoch014dc512016-03-22 12:00:34 +0000963 return Handle<HeapObject>::cast(code);
964}
965
966
967void RegExpMacroAssemblerIA32::GoTo(Label* to) {
968 BranchOrBacktrack(no_condition, to);
969}
970
971
972void RegExpMacroAssemblerIA32::IfRegisterGE(int reg,
973 int comparand,
974 Label* if_ge) {
975 __ cmp(register_location(reg), Immediate(comparand));
976 BranchOrBacktrack(greater_equal, if_ge);
977}
978
979
980void RegExpMacroAssemblerIA32::IfRegisterLT(int reg,
981 int comparand,
982 Label* if_lt) {
983 __ cmp(register_location(reg), Immediate(comparand));
984 BranchOrBacktrack(less, if_lt);
985}
986
987
988void RegExpMacroAssemblerIA32::IfRegisterEqPos(int reg,
989 Label* if_eq) {
990 __ cmp(edi, register_location(reg));
991 BranchOrBacktrack(equal, if_eq);
992}
993
994
995RegExpMacroAssembler::IrregexpImplementation
996 RegExpMacroAssemblerIA32::Implementation() {
997 return kIA32Implementation;
998}
999
1000
Ben Murdoch014dc512016-03-22 12:00:34 +00001001void RegExpMacroAssemblerIA32::PopCurrentPosition() {
1002 Pop(edi);
1003}
1004
1005
1006void RegExpMacroAssemblerIA32::PopRegister(int register_index) {
1007 Pop(eax);
1008 __ mov(register_location(register_index), eax);
1009}
1010
1011
1012void RegExpMacroAssemblerIA32::PushBacktrack(Label* label) {
1013 Push(Immediate::CodeRelativeOffset(label));
1014 CheckStackLimit();
1015}
1016
1017
1018void RegExpMacroAssemblerIA32::PushCurrentPosition() {
1019 Push(edi);
1020}
1021
1022
1023void RegExpMacroAssemblerIA32::PushRegister(int register_index,
1024 StackCheckFlag check_stack_limit) {
1025 __ mov(eax, register_location(register_index));
1026 Push(eax);
1027 if (check_stack_limit) CheckStackLimit();
1028}
1029
1030
1031void RegExpMacroAssemblerIA32::ReadCurrentPositionFromRegister(int reg) {
1032 __ mov(edi, register_location(reg));
1033}
1034
1035
1036void RegExpMacroAssemblerIA32::ReadStackPointerFromRegister(int reg) {
1037 __ mov(backtrack_stackpointer(), register_location(reg));
1038 __ add(backtrack_stackpointer(), Operand(ebp, kStackHighEnd));
1039}
1040
1041void RegExpMacroAssemblerIA32::SetCurrentPositionFromEnd(int by) {
1042 Label after_position;
1043 __ cmp(edi, -by * char_size());
1044 __ j(greater_equal, &after_position, Label::kNear);
1045 __ mov(edi, -by * char_size());
1046 // On RegExp code entry (where this operation is used), the character before
1047 // the current position is expected to be already loaded.
1048 // We have advanced the position, so it's safe to read backwards.
1049 LoadCurrentCharacterUnchecked(-1, 1);
1050 __ bind(&after_position);
1051}
1052
1053
1054void RegExpMacroAssemblerIA32::SetRegister(int register_index, int to) {
1055 DCHECK(register_index >= num_saved_registers_); // Reserved for positions!
1056 __ mov(register_location(register_index), Immediate(to));
1057}
1058
1059
1060bool RegExpMacroAssemblerIA32::Succeed() {
1061 __ jmp(&success_label_);
1062 return global();
1063}
1064
1065
1066void RegExpMacroAssemblerIA32::WriteCurrentPositionToRegister(int reg,
1067 int cp_offset) {
1068 if (cp_offset == 0) {
1069 __ mov(register_location(reg), edi);
1070 } else {
1071 __ lea(eax, Operand(edi, cp_offset * char_size()));
1072 __ mov(register_location(reg), eax);
1073 }
1074}
1075
1076
1077void RegExpMacroAssemblerIA32::ClearRegisters(int reg_from, int reg_to) {
1078 DCHECK(reg_from <= reg_to);
1079 __ mov(eax, Operand(ebp, kStringStartMinusOne));
1080 for (int reg = reg_from; reg <= reg_to; reg++) {
1081 __ mov(register_location(reg), eax);
1082 }
1083}
1084
1085
1086void RegExpMacroAssemblerIA32::WriteStackPointerToRegister(int reg) {
1087 __ mov(eax, backtrack_stackpointer());
1088 __ sub(eax, Operand(ebp, kStackHighEnd));
1089 __ mov(register_location(reg), eax);
1090}
1091
1092
1093// Private methods:
1094
1095void RegExpMacroAssemblerIA32::CallCheckStackGuardState(Register scratch) {
1096 static const int num_arguments = 3;
1097 __ PrepareCallCFunction(num_arguments, scratch);
1098 // RegExp code frame pointer.
Rubin Xu6e1e26a2021-02-10 00:04:48 +00001099 __ mov(Operand(esp, 2 * kSystemPointerSize), ebp);
1100 // Code of self.
1101 __ mov(Operand(esp, 1 * kSystemPointerSize), Immediate(masm_->CodeObject()));
Ben Murdoch014dc512016-03-22 12:00:34 +00001102 // Next address on the stack (will be address of return address).
Rubin Xu6e1e26a2021-02-10 00:04:48 +00001103 __ lea(eax, Operand(esp, -kSystemPointerSize));
1104 __ mov(Operand(esp, 0 * kSystemPointerSize), eax);
Ben Murdoch014dc512016-03-22 12:00:34 +00001105 ExternalReference check_stack_guard =
1106 ExternalReference::re_check_stack_guard_state(isolate());
1107 __ CallCFunction(check_stack_guard, num_arguments);
1108}
1109
Rubin Xu6e1e26a2021-02-10 00:04:48 +00001110Operand RegExpMacroAssemblerIA32::StaticVariable(const ExternalReference& ext) {
1111 return Operand(ext.address(), RelocInfo::EXTERNAL_REFERENCE);
1112}
Ben Murdoch014dc512016-03-22 12:00:34 +00001113
1114// Helper function for reading a value out of a stack frame.
1115template <typename T>
1116static T& frame_entry(Address re_frame, int frame_offset) {
Rubin Xu2894c6a2019-02-07 16:01:35 +00001117 return reinterpret_cast<T&>(Memory<int32_t>(re_frame + frame_offset));
Ben Murdoch014dc512016-03-22 12:00:34 +00001118}
1119
1120
1121template <typename T>
1122static T* frame_entry_address(Address re_frame, int frame_offset) {
1123 return reinterpret_cast<T*>(re_frame + frame_offset);
1124}
1125
Ben Murdoch014dc512016-03-22 12:00:34 +00001126int RegExpMacroAssemblerIA32::CheckStackGuardState(Address* return_address,
Rubin Xu6e1e26a2021-02-10 00:04:48 +00001127 Address raw_code,
Ben Murdoch014dc512016-03-22 12:00:34 +00001128 Address re_frame) {
Rubin Xu6e1e26a2021-02-10 00:04:48 +00001129 Code re_code = Code::cast(Object(raw_code));
Ben Murdoch014dc512016-03-22 12:00:34 +00001130 return NativeRegExpMacroAssembler::CheckStackGuardState(
1131 frame_entry<Isolate*>(re_frame, kIsolate),
1132 frame_entry<int>(re_frame, kStartIndex),
Rubin Xu6e1e26a2021-02-10 00:04:48 +00001133 static_cast<RegExp::CallOrigin>(frame_entry<int>(re_frame, kDirectCall)),
1134 return_address, re_code,
1135 frame_entry_address<Address>(re_frame, kInputString),
Ben Murdoch014dc512016-03-22 12:00:34 +00001136 frame_entry_address<const byte*>(re_frame, kInputStart),
1137 frame_entry_address<const byte*>(re_frame, kInputEnd));
1138}
1139
1140
1141Operand RegExpMacroAssemblerIA32::register_location(int register_index) {
1142 DCHECK(register_index < (1<<30));
1143 if (num_registers_ <= register_index) {
1144 num_registers_ = register_index + 1;
1145 }
Rubin Xu6e1e26a2021-02-10 00:04:48 +00001146 return Operand(ebp, kRegisterZero - register_index * kSystemPointerSize);
Ben Murdoch014dc512016-03-22 12:00:34 +00001147}
1148
1149
1150void RegExpMacroAssemblerIA32::CheckPosition(int cp_offset,
1151 Label* on_outside_input) {
1152 if (cp_offset >= 0) {
1153 __ cmp(edi, -cp_offset * char_size());
1154 BranchOrBacktrack(greater_equal, on_outside_input);
1155 } else {
1156 __ lea(eax, Operand(edi, cp_offset * char_size()));
1157 __ cmp(eax, Operand(ebp, kStringStartMinusOne));
1158 BranchOrBacktrack(less_equal, on_outside_input);
1159 }
1160}
1161
1162
1163void RegExpMacroAssemblerIA32::BranchOrBacktrack(Condition condition,
1164 Label* to) {
1165 if (condition < 0) { // No condition
Rubin Xu2894c6a2019-02-07 16:01:35 +00001166 if (to == nullptr) {
Ben Murdoch014dc512016-03-22 12:00:34 +00001167 Backtrack();
1168 return;
1169 }
1170 __ jmp(to);
1171 return;
1172 }
Rubin Xu2894c6a2019-02-07 16:01:35 +00001173 if (to == nullptr) {
Ben Murdoch014dc512016-03-22 12:00:34 +00001174 __ j(condition, &backtrack_label_);
1175 return;
1176 }
1177 __ j(condition, to);
1178}
1179
1180
1181void RegExpMacroAssemblerIA32::SafeCall(Label* to) {
1182 Label return_to;
1183 __ push(Immediate::CodeRelativeOffset(&return_to));
1184 __ jmp(to);
1185 __ bind(&return_to);
1186}
1187
1188
1189void RegExpMacroAssemblerIA32::SafeReturn() {
1190 __ pop(ebx);
1191 __ add(ebx, Immediate(masm_->CodeObject()));
1192 __ jmp(ebx);
1193}
1194
1195
1196void RegExpMacroAssemblerIA32::SafeCallTarget(Label* name) {
1197 __ bind(name);
1198}
1199
1200
1201void RegExpMacroAssemblerIA32::Push(Register source) {
Rubin Xu2894c6a2019-02-07 16:01:35 +00001202 DCHECK(source != backtrack_stackpointer());
Ben Murdoch014dc512016-03-22 12:00:34 +00001203 // Notice: This updates flags, unlike normal Push.
Rubin Xu6e1e26a2021-02-10 00:04:48 +00001204 __ sub(backtrack_stackpointer(), Immediate(kSystemPointerSize));
Ben Murdoch014dc512016-03-22 12:00:34 +00001205 __ mov(Operand(backtrack_stackpointer(), 0), source);
1206}
1207
1208
1209void RegExpMacroAssemblerIA32::Push(Immediate value) {
1210 // Notice: This updates flags, unlike normal Push.
Rubin Xu6e1e26a2021-02-10 00:04:48 +00001211 __ sub(backtrack_stackpointer(), Immediate(kSystemPointerSize));
Ben Murdoch014dc512016-03-22 12:00:34 +00001212 __ mov(Operand(backtrack_stackpointer(), 0), value);
1213}
1214
1215
1216void RegExpMacroAssemblerIA32::Pop(Register target) {
Rubin Xu2894c6a2019-02-07 16:01:35 +00001217 DCHECK(target != backtrack_stackpointer());
Ben Murdoch014dc512016-03-22 12:00:34 +00001218 __ mov(target, Operand(backtrack_stackpointer(), 0));
1219 // Notice: This updates flags, unlike normal Pop.
Rubin Xu6e1e26a2021-02-10 00:04:48 +00001220 __ add(backtrack_stackpointer(), Immediate(kSystemPointerSize));
Ben Murdoch014dc512016-03-22 12:00:34 +00001221}
1222
1223
1224void RegExpMacroAssemblerIA32::CheckPreemption() {
1225 // Check for preemption.
1226 Label no_preempt;
1227 ExternalReference stack_limit =
Rubin Xu6e1e26a2021-02-10 00:04:48 +00001228 ExternalReference::address_of_jslimit(isolate());
1229 __ cmp(esp, StaticVariable(stack_limit));
Ben Murdoch014dc512016-03-22 12:00:34 +00001230 __ j(above, &no_preempt);
1231
1232 SafeCall(&check_preempt_label_);
1233
1234 __ bind(&no_preempt);
1235}
1236
1237
1238void RegExpMacroAssemblerIA32::CheckStackLimit() {
1239 Label no_stack_overflow;
1240 ExternalReference stack_limit =
Rubin Xu6e1e26a2021-02-10 00:04:48 +00001241 ExternalReference::address_of_regexp_stack_limit_address(isolate());
1242 __ cmp(backtrack_stackpointer(), StaticVariable(stack_limit));
Ben Murdoch014dc512016-03-22 12:00:34 +00001243 __ j(above, &no_stack_overflow);
1244
1245 SafeCall(&stack_overflow_label_);
1246
1247 __ bind(&no_stack_overflow);
1248}
1249
1250
1251void RegExpMacroAssemblerIA32::LoadCurrentCharacterUnchecked(int cp_offset,
1252 int characters) {
1253 if (mode_ == LATIN1) {
1254 if (characters == 4) {
1255 __ mov(current_character(), Operand(esi, edi, times_1, cp_offset));
1256 } else if (characters == 2) {
1257 __ movzx_w(current_character(), Operand(esi, edi, times_1, cp_offset));
1258 } else {
Rubin Xu2894c6a2019-02-07 16:01:35 +00001259 DCHECK_EQ(1, characters);
Ben Murdoch014dc512016-03-22 12:00:34 +00001260 __ movzx_b(current_character(), Operand(esi, edi, times_1, cp_offset));
1261 }
1262 } else {
1263 DCHECK(mode_ == UC16);
1264 if (characters == 2) {
1265 __ mov(current_character(),
1266 Operand(esi, edi, times_1, cp_offset * sizeof(uc16)));
1267 } else {
Rubin Xu2894c6a2019-02-07 16:01:35 +00001268 DCHECK_EQ(1, characters);
Ben Murdoch014dc512016-03-22 12:00:34 +00001269 __ movzx_w(current_character(),
1270 Operand(esi, edi, times_1, cp_offset * sizeof(uc16)));
1271 }
1272 }
1273}
1274
1275
1276#undef __
1277
Ben Murdoch014dc512016-03-22 12:00:34 +00001278} // namespace internal
1279} // namespace v8
1280
1281#endif // V8_TARGET_ARCH_IA32