blob: fdf3b9febb53faaa536a2d5a7e8401115eac0c3d [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2008-2009 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29#include "unicode.h"
30#include "log.h"
31#include "ast.h"
32#include "regexp-stack.h"
33#include "macro-assembler.h"
34#include "regexp-macro-assembler.h"
35#include "ia32/macro-assembler-ia32.h"
36#include "ia32/regexp-macro-assembler-ia32.h"
37
38namespace v8 {
39namespace internal {
40
Steve Block6ded16b2010-05-10 14:33:55 +010041#ifndef V8_INTERPRETED_REGEXP
Steve Blocka7e24c12009-10-30 11:49:00 +000042/*
43 * This assembler uses the following register assignment convention
44 * - edx : current character. Must be loaded using LoadCurrentCharacter
45 * before using any of the dispatch methods.
46 * - edi : current position in input, as negative offset from end of string.
47 * Please notice that this is the byte offset, not the character offset!
48 * - esi : end of input (points to byte after last character in input).
49 * - ebp : frame pointer. Used to access arguments, local variables and
50 * RegExp registers.
51 * - esp : points to tip of C stack.
52 * - ecx : points to tip of backtrack stack
53 *
54 * The registers eax, ebx and ecx are free to use for computations.
55 *
56 * Each call to a public method should retain this convention.
57 * The stack will have the following structure:
Leon Clarkee46be812010-01-19 14:06:41 +000058 * - direct_call (if 1, direct call from JavaScript code, if 0
59 * call through the runtime system)
60 * - stack_area_base (High end of the memory area to use as
61 * backtracking stack)
Leon Clarkee46be812010-01-19 14:06:41 +000062 * - int* capture_array (int[num_saved_registers_], for output).
63 * - end of input (Address of end of string)
64 * - start of input (Address of first character in string)
65 * - start index (character index of start)
66 * - String* input_string (location of a handle containing the string)
Steve Blocka7e24c12009-10-30 11:49:00 +000067 * --- frame alignment (if applicable) ---
68 * - return address
69 * ebp-> - old ebp
70 * - backup of caller esi
71 * - backup of caller edi
72 * - backup of caller ebx
73 * - Offset of location before start of input (effectively character
74 * position -1). Used to initialize capture registers to a non-position.
Leon Clarked91b9f72010-01-27 17:25:45 +000075 * - Boolean at start (if 1, we are starting at the start of the string,
76 * otherwise 0)
Steve Blocka7e24c12009-10-30 11:49:00 +000077 * - register 0 ebp[-4] (Only positions must be stored in the first
78 * - register 1 ebp[-8] num_saved_registers_ registers)
79 * - ...
80 *
81 * The first num_saved_registers_ registers are initialized to point to
82 * "character -1" in the string (i.e., char_size() bytes before the first
83 * character of the string). The remaining registers starts out as garbage.
84 *
85 * The data up to the return address must be placed there by the calling
86 * code, by calling the code entry as cast to a function with the signature:
87 * int (*match)(String* input_string,
Leon Clarkee46be812010-01-19 14:06:41 +000088 * int start_index,
Steve Blocka7e24c12009-10-30 11:49:00 +000089 * Address start,
90 * Address end,
91 * int* capture_output_array,
92 * bool at_start,
Leon Clarkee46be812010-01-19 14:06:41 +000093 * byte* stack_area_base,
94 * bool direct_call)
Steve Blocka7e24c12009-10-30 11:49:00 +000095 */
96
97#define __ ACCESS_MASM(masm_)
98
99RegExpMacroAssemblerIA32::RegExpMacroAssemblerIA32(
100 Mode mode,
101 int registers_to_save)
102 : masm_(new MacroAssembler(NULL, kRegExpCodeSize)),
103 mode_(mode),
104 num_registers_(registers_to_save),
105 num_saved_registers_(registers_to_save),
106 entry_label_(),
107 start_label_(),
108 success_label_(),
109 backtrack_label_(),
110 exit_label_() {
111 ASSERT_EQ(0, registers_to_save % 2);
112 __ jmp(&entry_label_); // We'll write the entry code later.
113 __ bind(&start_label_); // And then continue from here.
114}
115
116
117RegExpMacroAssemblerIA32::~RegExpMacroAssemblerIA32() {
118 delete masm_;
119 // Unuse labels in case we throw away the assembler without calling GetCode.
120 entry_label_.Unuse();
121 start_label_.Unuse();
122 success_label_.Unuse();
123 backtrack_label_.Unuse();
124 exit_label_.Unuse();
125 check_preempt_label_.Unuse();
126 stack_overflow_label_.Unuse();
127}
128
129
130int RegExpMacroAssemblerIA32::stack_limit_slack() {
131 return RegExpStack::kStackLimitSlack;
132}
133
134
135void RegExpMacroAssemblerIA32::AdvanceCurrentPosition(int by) {
136 if (by != 0) {
137 Label inside_string;
138 __ add(Operand(edi), Immediate(by * char_size()));
139 }
140}
141
142
143void RegExpMacroAssemblerIA32::AdvanceRegister(int reg, int by) {
144 ASSERT(reg >= 0);
145 ASSERT(reg < num_registers_);
146 if (by != 0) {
147 __ add(register_location(reg), Immediate(by));
148 }
149}
150
151
152void RegExpMacroAssemblerIA32::Backtrack() {
153 CheckPreemption();
154 // Pop Code* offset from backtrack stack, add Code* and jump to location.
155 Pop(ebx);
156 __ add(Operand(ebx), Immediate(masm_->CodeObject()));
157 __ jmp(Operand(ebx));
158}
159
160
161void RegExpMacroAssemblerIA32::Bind(Label* label) {
162 __ bind(label);
163}
164
165
166void RegExpMacroAssemblerIA32::CheckCharacter(uint32_t c, Label* on_equal) {
167 __ cmp(current_character(), c);
168 BranchOrBacktrack(equal, on_equal);
169}
170
171
172void RegExpMacroAssemblerIA32::CheckCharacterGT(uc16 limit, Label* on_greater) {
173 __ cmp(current_character(), limit);
174 BranchOrBacktrack(greater, on_greater);
175}
176
177
178void RegExpMacroAssemblerIA32::CheckAtStart(Label* on_at_start) {
179 Label not_at_start;
180 // Did we start the match at the start of the string at all?
181 __ cmp(Operand(ebp, kAtStart), Immediate(0));
182 BranchOrBacktrack(equal, &not_at_start);
183 // If we did, are we still at the start of the input?
184 __ lea(eax, Operand(esi, edi, times_1, 0));
185 __ cmp(eax, Operand(ebp, kInputStart));
186 BranchOrBacktrack(equal, on_at_start);
187 __ bind(&not_at_start);
188}
189
190
191void RegExpMacroAssemblerIA32::CheckNotAtStart(Label* on_not_at_start) {
192 // Did we start the match at the start of the string at all?
193 __ cmp(Operand(ebp, kAtStart), Immediate(0));
194 BranchOrBacktrack(equal, on_not_at_start);
195 // If we did, are we still at the start of the input?
196 __ lea(eax, Operand(esi, edi, times_1, 0));
197 __ cmp(eax, Operand(ebp, kInputStart));
198 BranchOrBacktrack(not_equal, on_not_at_start);
199}
200
201
202void RegExpMacroAssemblerIA32::CheckCharacterLT(uc16 limit, Label* on_less) {
203 __ cmp(current_character(), limit);
204 BranchOrBacktrack(less, on_less);
205}
206
207
208void RegExpMacroAssemblerIA32::CheckCharacters(Vector<const uc16> str,
209 int cp_offset,
210 Label* on_failure,
211 bool check_end_of_string) {
212 int byte_length = str.length() * char_size();
213 int byte_offset = cp_offset * char_size();
214 if (check_end_of_string) {
215 // Check that there are at least str.length() characters left in the input.
216 __ cmp(Operand(edi), Immediate(-(byte_offset + byte_length)));
217 BranchOrBacktrack(greater, on_failure);
218 }
219
220 if (on_failure == NULL) {
221 // Instead of inlining a backtrack, (re)use the global backtrack target.
222 on_failure = &backtrack_label_;
223 }
224
225 for (int i = 0; i < str.length(); i++) {
226 if (mode_ == ASCII) {
227 __ cmpb(Operand(esi, edi, times_1, byte_offset + i),
228 static_cast<int8_t>(str[i]));
229 } else {
230 ASSERT(mode_ == UC16);
231 __ cmpw(Operand(esi, edi, times_1, byte_offset + i * sizeof(uc16)),
232 Immediate(str[i]));
233 }
234 BranchOrBacktrack(not_equal, on_failure);
235 }
236}
237
238
239void RegExpMacroAssemblerIA32::CheckGreedyLoop(Label* on_equal) {
240 Label fallthrough;
241 __ cmp(edi, Operand(backtrack_stackpointer(), 0));
242 __ j(not_equal, &fallthrough);
243 __ add(Operand(backtrack_stackpointer()), Immediate(kPointerSize)); // Pop.
244 BranchOrBacktrack(no_condition, on_equal);
245 __ bind(&fallthrough);
246}
247
248
249void RegExpMacroAssemblerIA32::CheckNotBackReferenceIgnoreCase(
250 int start_reg,
251 Label* on_no_match) {
252 Label fallthrough;
253 __ mov(edx, register_location(start_reg)); // Index of start of capture
254 __ mov(ebx, register_location(start_reg + 1)); // Index of end of capture
255 __ sub(ebx, Operand(edx)); // Length of capture.
256
257 // The length of a capture should not be negative. This can only happen
258 // if the end of the capture is unrecorded, or at a point earlier than
259 // the start of the capture.
260 BranchOrBacktrack(less, on_no_match, not_taken);
261
262 // If length is zero, either the capture is empty or it is completely
263 // uncaptured. In either case succeed immediately.
264 __ j(equal, &fallthrough);
265
266 if (mode_ == ASCII) {
267 Label success;
268 Label fail;
269 Label loop_increment;
270 // Save register contents to make the registers available below.
271 __ push(edi);
272 __ push(backtrack_stackpointer());
273 // After this, the eax, ecx, and edi registers are available.
274
275 __ add(edx, Operand(esi)); // Start of capture
276 __ add(edi, Operand(esi)); // Start of text to match against capture.
277 __ add(ebx, Operand(edi)); // End of text to match against capture.
278
279 Label loop;
280 __ bind(&loop);
281 __ movzx_b(eax, Operand(edi, 0));
282 __ cmpb_al(Operand(edx, 0));
283 __ j(equal, &loop_increment);
284
285 // Mismatch, try case-insensitive match (converting letters to lower-case).
286 __ or_(eax, 0x20); // Convert match character to lower-case.
287 __ lea(ecx, Operand(eax, -'a'));
288 __ cmp(ecx, static_cast<int32_t>('z' - 'a')); // Is eax a lowercase letter?
289 __ j(above, &fail);
290 // Also convert capture character.
291 __ movzx_b(ecx, Operand(edx, 0));
292 __ or_(ecx, 0x20);
293
294 __ cmp(eax, Operand(ecx));
295 __ j(not_equal, &fail);
296
297 __ bind(&loop_increment);
298 // Increment pointers into match and capture strings.
299 __ add(Operand(edx), Immediate(1));
300 __ add(Operand(edi), Immediate(1));
301 // Compare to end of match, and loop if not done.
302 __ cmp(edi, Operand(ebx));
303 __ j(below, &loop, taken);
304 __ jmp(&success);
305
306 __ bind(&fail);
307 // Restore original values before failing.
308 __ pop(backtrack_stackpointer());
309 __ pop(edi);
310 BranchOrBacktrack(no_condition, on_no_match);
311
312 __ bind(&success);
313 // Restore original value before continuing.
314 __ pop(backtrack_stackpointer());
315 // Drop original value of character position.
316 __ add(Operand(esp), Immediate(kPointerSize));
317 // Compute new value of character position after the matched part.
318 __ sub(edi, Operand(esi));
319 } else {
320 ASSERT(mode_ == UC16);
321 // Save registers before calling C function.
322 __ push(esi);
323 __ push(edi);
324 __ push(backtrack_stackpointer());
325 __ push(ebx);
326
Steve Block6ded16b2010-05-10 14:33:55 +0100327 static const int argument_count = 3;
328 __ PrepareCallCFunction(argument_count, ecx);
Steve Blocka7e24c12009-10-30 11:49:00 +0000329 // Put arguments into allocated stack area, last argument highest on stack.
330 // Parameters are
331 // Address byte_offset1 - Address captured substring's start.
332 // Address byte_offset2 - Address of current character position.
333 // size_t byte_length - length of capture in bytes(!)
334
335 // Set byte_length.
336 __ mov(Operand(esp, 2 * kPointerSize), ebx);
337 // Set byte_offset2.
338 // Found by adding negative string-end offset of current position (edi)
339 // to end of string.
340 __ add(edi, Operand(esi));
341 __ mov(Operand(esp, 1 * kPointerSize), edi);
342 // Set byte_offset1.
343 // Start of capture, where edx already holds string-end negative offset.
344 __ add(edx, Operand(esi));
345 __ mov(Operand(esp, 0 * kPointerSize), edx);
346
347 ExternalReference compare =
348 ExternalReference::re_case_insensitive_compare_uc16();
Steve Block6ded16b2010-05-10 14:33:55 +0100349 __ CallCFunction(compare, argument_count);
Steve Blocka7e24c12009-10-30 11:49:00 +0000350 // Pop original values before reacting on result value.
351 __ pop(ebx);
352 __ pop(backtrack_stackpointer());
353 __ pop(edi);
354 __ pop(esi);
355
356 // Check if function returned non-zero for success or zero for failure.
357 __ or_(eax, Operand(eax));
358 BranchOrBacktrack(zero, on_no_match);
359 // On success, increment position by length of capture.
360 __ add(edi, Operand(ebx));
361 }
362 __ bind(&fallthrough);
363}
364
365
366void RegExpMacroAssemblerIA32::CheckNotBackReference(
367 int start_reg,
368 Label* on_no_match) {
369 Label fallthrough;
370 Label success;
371 Label fail;
372
373 // Find length of back-referenced capture.
374 __ mov(edx, register_location(start_reg));
375 __ mov(eax, register_location(start_reg + 1));
376 __ sub(eax, Operand(edx)); // Length to check.
377 // Fail on partial or illegal capture (start of capture after end of capture).
378 BranchOrBacktrack(less, on_no_match);
379 // Succeed on empty capture (including no capture)
380 __ j(equal, &fallthrough);
381
382 // Check that there are sufficient characters left in the input.
383 __ mov(ebx, edi);
384 __ add(ebx, Operand(eax));
385 BranchOrBacktrack(greater, on_no_match);
386
387 // Save register to make it available below.
388 __ push(backtrack_stackpointer());
389
390 // Compute pointers to match string and capture string
391 __ lea(ebx, Operand(esi, edi, times_1, 0)); // Start of match.
392 __ add(edx, Operand(esi)); // Start of capture.
393 __ lea(ecx, Operand(eax, ebx, times_1, 0)); // End of match
394
395 Label loop;
396 __ bind(&loop);
397 if (mode_ == ASCII) {
398 __ movzx_b(eax, Operand(edx, 0));
399 __ cmpb_al(Operand(ebx, 0));
400 } else {
401 ASSERT(mode_ == UC16);
402 __ movzx_w(eax, Operand(edx, 0));
403 __ cmpw_ax(Operand(ebx, 0));
404 }
405 __ j(not_equal, &fail);
406 // Increment pointers into capture and match string.
407 __ add(Operand(edx), Immediate(char_size()));
408 __ add(Operand(ebx), Immediate(char_size()));
409 // Check if we have reached end of match area.
410 __ cmp(ebx, Operand(ecx));
411 __ j(below, &loop);
412 __ jmp(&success);
413
414 __ bind(&fail);
415 // Restore backtrack stackpointer.
416 __ pop(backtrack_stackpointer());
417 BranchOrBacktrack(no_condition, on_no_match);
418
419 __ bind(&success);
420 // Move current character position to position after match.
421 __ mov(edi, ecx);
422 __ sub(Operand(edi), esi);
423 // Restore backtrack stackpointer.
424 __ pop(backtrack_stackpointer());
425
426 __ bind(&fallthrough);
427}
428
429
430void RegExpMacroAssemblerIA32::CheckNotRegistersEqual(int reg1,
431 int reg2,
432 Label* on_not_equal) {
433 __ mov(eax, register_location(reg1));
434 __ cmp(eax, register_location(reg2));
435 BranchOrBacktrack(not_equal, on_not_equal);
436}
437
438
439void RegExpMacroAssemblerIA32::CheckNotCharacter(uint32_t c,
440 Label* on_not_equal) {
441 __ cmp(current_character(), c);
442 BranchOrBacktrack(not_equal, on_not_equal);
443}
444
445
446void RegExpMacroAssemblerIA32::CheckCharacterAfterAnd(uint32_t c,
447 uint32_t mask,
448 Label* on_equal) {
449 __ mov(eax, current_character());
450 __ and_(eax, mask);
451 __ cmp(eax, c);
452 BranchOrBacktrack(equal, on_equal);
453}
454
455
456void RegExpMacroAssemblerIA32::CheckNotCharacterAfterAnd(uint32_t c,
457 uint32_t mask,
458 Label* on_not_equal) {
459 __ mov(eax, current_character());
460 __ and_(eax, mask);
461 __ cmp(eax, c);
462 BranchOrBacktrack(not_equal, on_not_equal);
463}
464
465
466void RegExpMacroAssemblerIA32::CheckNotCharacterAfterMinusAnd(
467 uc16 c,
468 uc16 minus,
469 uc16 mask,
470 Label* on_not_equal) {
471 ASSERT(minus < String::kMaxUC16CharCode);
472 __ lea(eax, Operand(current_character(), -minus));
473 __ and_(eax, mask);
474 __ cmp(eax, c);
475 BranchOrBacktrack(not_equal, on_not_equal);
476}
477
478
479bool RegExpMacroAssemblerIA32::CheckSpecialCharacterClass(uc16 type,
Steve Blocka7e24c12009-10-30 11:49:00 +0000480 Label* on_no_match) {
481 // Range checks (c in min..max) are generally implemented by an unsigned
482 // (c - min) <= (max - min) check
483 switch (type) {
484 case 's':
485 // Match space-characters
486 if (mode_ == ASCII) {
487 // ASCII space characters are '\t'..'\r' and ' '.
Steve Blocka7e24c12009-10-30 11:49:00 +0000488 Label success;
489 __ cmp(current_character(), ' ');
490 __ j(equal, &success);
491 // Check range 0x09..0x0d
Leon Clarkee46be812010-01-19 14:06:41 +0000492 __ lea(eax, Operand(current_character(), -'\t'));
493 __ cmp(eax, '\r' - '\t');
Steve Blocka7e24c12009-10-30 11:49:00 +0000494 BranchOrBacktrack(above, on_no_match);
495 __ bind(&success);
496 return true;
497 }
498 return false;
499 case 'S':
500 // Match non-space characters.
Steve Blocka7e24c12009-10-30 11:49:00 +0000501 if (mode_ == ASCII) {
502 // ASCII space characters are '\t'..'\r' and ' '.
503 __ cmp(current_character(), ' ');
504 BranchOrBacktrack(equal, on_no_match);
Leon Clarkee46be812010-01-19 14:06:41 +0000505 __ lea(eax, Operand(current_character(), -'\t'));
506 __ cmp(eax, '\r' - '\t');
Steve Blocka7e24c12009-10-30 11:49:00 +0000507 BranchOrBacktrack(below_equal, on_no_match);
508 return true;
509 }
510 return false;
511 case 'd':
512 // Match ASCII digits ('0'..'9')
Leon Clarkee46be812010-01-19 14:06:41 +0000513 __ lea(eax, Operand(current_character(), -'0'));
514 __ cmp(eax, '9' - '0');
Steve Blocka7e24c12009-10-30 11:49:00 +0000515 BranchOrBacktrack(above, on_no_match);
516 return true;
517 case 'D':
518 // Match non ASCII-digits
Leon Clarkee46be812010-01-19 14:06:41 +0000519 __ lea(eax, Operand(current_character(), -'0'));
520 __ cmp(eax, '9' - '0');
Steve Blocka7e24c12009-10-30 11:49:00 +0000521 BranchOrBacktrack(below_equal, on_no_match);
522 return true;
523 case '.': {
524 // Match non-newlines (not 0x0a('\n'), 0x0d('\r'), 0x2028 and 0x2029)
Leon Clarkee46be812010-01-19 14:06:41 +0000525 __ mov(Operand(eax), current_character());
526 __ xor_(Operand(eax), Immediate(0x01));
Steve Blocka7e24c12009-10-30 11:49:00 +0000527 // See if current character is '\n'^1 or '\r'^1, i.e., 0x0b or 0x0c
Leon Clarkee46be812010-01-19 14:06:41 +0000528 __ sub(Operand(eax), Immediate(0x0b));
529 __ cmp(eax, 0x0c - 0x0b);
Steve Blocka7e24c12009-10-30 11:49:00 +0000530 BranchOrBacktrack(below_equal, on_no_match);
531 if (mode_ == UC16) {
532 // Compare original value to 0x2028 and 0x2029, using the already
533 // computed (current_char ^ 0x01 - 0x0b). I.e., check for
534 // 0x201d (0x2028 - 0x0b) or 0x201e.
Leon Clarkee46be812010-01-19 14:06:41 +0000535 __ sub(Operand(eax), Immediate(0x2028 - 0x0b));
536 __ cmp(eax, 0x2029 - 0x2028);
Steve Blocka7e24c12009-10-30 11:49:00 +0000537 BranchOrBacktrack(below_equal, on_no_match);
538 }
539 return true;
540 }
Leon Clarkee46be812010-01-19 14:06:41 +0000541 case 'w': {
542 if (mode_ != ASCII) {
543 // Table is 128 entries, so all ASCII characters can be tested.
544 __ cmp(Operand(current_character()), Immediate('z'));
545 BranchOrBacktrack(above, on_no_match);
546 }
547 ASSERT_EQ(0, word_character_map[0]); // Character '\0' is not a word char.
548 ExternalReference word_map = ExternalReference::re_word_character_map();
549 __ test_b(current_character(),
550 Operand::StaticArray(current_character(), times_1, word_map));
551 BranchOrBacktrack(zero, on_no_match);
552 return true;
553 }
554 case 'W': {
555 Label done;
556 if (mode_ != ASCII) {
557 // Table is 128 entries, so all ASCII characters can be tested.
558 __ cmp(Operand(current_character()), Immediate('z'));
559 __ j(above, &done);
560 }
561 ASSERT_EQ(0, word_character_map[0]); // Character '\0' is not a word char.
562 ExternalReference word_map = ExternalReference::re_word_character_map();
563 __ test_b(current_character(),
564 Operand::StaticArray(current_character(), times_1, word_map));
565 BranchOrBacktrack(not_zero, on_no_match);
566 if (mode_ != ASCII) {
567 __ bind(&done);
Steve Blocka7e24c12009-10-30 11:49:00 +0000568 }
569 return true;
Leon Clarkee46be812010-01-19 14:06:41 +0000570 }
571 // Non-standard classes (with no syntactic shorthand) used internally.
572 case '*':
573 // Match any character.
574 return true;
575 case 'n': {
576 // Match newlines (0x0a('\n'), 0x0d('\r'), 0x2028 or 0x2029).
577 // The opposite of '.'.
578 __ mov(Operand(eax), current_character());
579 __ xor_(Operand(eax), Immediate(0x01));
580 // See if current character is '\n'^1 or '\r'^1, i.e., 0x0b or 0x0c
581 __ sub(Operand(eax), Immediate(0x0b));
582 __ cmp(eax, 0x0c - 0x0b);
583 if (mode_ == ASCII) {
584 BranchOrBacktrack(above, on_no_match);
585 } else {
586 Label done;
587 BranchOrBacktrack(below_equal, &done);
588 ASSERT_EQ(UC16, mode_);
589 // Compare original value to 0x2028 and 0x2029, using the already
590 // computed (current_char ^ 0x01 - 0x0b). I.e., check for
591 // 0x201d (0x2028 - 0x0b) or 0x201e.
592 __ sub(Operand(eax), Immediate(0x2028 - 0x0b));
593 __ cmp(eax, 1);
594 BranchOrBacktrack(above, on_no_match);
595 __ bind(&done);
596 }
597 return true;
598 }
599 // No custom implementation (yet): s(UC16), S(UC16).
Steve Blocka7e24c12009-10-30 11:49:00 +0000600 default:
601 return false;
602 }
603}
604
605
606void RegExpMacroAssemblerIA32::Fail() {
607 ASSERT(FAILURE == 0); // Return value for failure is zero.
608 __ xor_(eax, Operand(eax)); // zero eax.
609 __ jmp(&exit_label_);
610}
611
612
613Handle<Object> RegExpMacroAssemblerIA32::GetCode(Handle<String> source) {
614 // Finalize code - write the entry point code now we know how many
615 // registers we need.
616
617 // Entry code:
618 __ bind(&entry_label_);
619 // Start new stack frame.
620 __ push(ebp);
621 __ mov(ebp, esp);
622 // Save callee-save registers. Order here should correspond to order of
623 // kBackup_ebx etc.
624 __ push(esi);
625 __ push(edi);
626 __ push(ebx); // Callee-save on MacOS.
627 __ push(Immediate(0)); // Make room for "input start - 1" constant.
Leon Clarked91b9f72010-01-27 17:25:45 +0000628 __ push(Immediate(0)); // Make room for "at start" constant.
Steve Blocka7e24c12009-10-30 11:49:00 +0000629
630 // Check if we have space on the stack for registers.
631 Label stack_limit_hit;
632 Label stack_ok;
633
Steve Blockd0582a62009-12-15 09:54:21 +0000634 ExternalReference stack_limit =
635 ExternalReference::address_of_stack_limit();
Steve Blocka7e24c12009-10-30 11:49:00 +0000636 __ mov(ecx, esp);
Steve Blockd0582a62009-12-15 09:54:21 +0000637 __ sub(ecx, Operand::StaticVariable(stack_limit));
Steve Blocka7e24c12009-10-30 11:49:00 +0000638 // Handle it if the stack pointer is already below the stack limit.
639 __ j(below_equal, &stack_limit_hit, not_taken);
640 // Check if there is room for the variable number of registers above
641 // the stack limit.
642 __ cmp(ecx, num_registers_ * kPointerSize);
643 __ j(above_equal, &stack_ok, taken);
644 // Exit with OutOfMemory exception. There is not enough space on the stack
645 // for our working registers.
646 __ mov(eax, EXCEPTION);
647 __ jmp(&exit_label_);
648
649 __ bind(&stack_limit_hit);
650 CallCheckStackGuardState(ebx);
651 __ or_(eax, Operand(eax));
652 // If returned value is non-zero, we exit with the returned value as result.
653 __ j(not_zero, &exit_label_);
654
655 __ bind(&stack_ok);
Steve Block6ded16b2010-05-10 14:33:55 +0100656 // Load start index for later use.
657 __ mov(ebx, Operand(ebp, kStartIndex));
Steve Blocka7e24c12009-10-30 11:49:00 +0000658
659 // Allocate space on stack for registers.
660 __ sub(Operand(esp), Immediate(num_registers_ * kPointerSize));
661 // Load string length.
662 __ mov(esi, Operand(ebp, kInputEnd));
663 // Load input position.
664 __ mov(edi, Operand(ebp, kInputStart));
665 // Set up edi to be negative offset from string end.
666 __ sub(edi, Operand(esi));
Steve Block6ded16b2010-05-10 14:33:55 +0100667
668 // Set eax to address of char before start of the string.
Steve Blocka7e24c12009-10-30 11:49:00 +0000669 // (effectively string position -1).
Steve Block6ded16b2010-05-10 14:33:55 +0100670 __ neg(ebx);
671 if (mode_ == UC16) {
672 __ lea(eax, Operand(edi, ebx, times_2, -char_size()));
673 } else {
674 __ lea(eax, Operand(edi, ebx, times_1, -char_size()));
675 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000676 // Store this value in a local variable, for use when clearing
677 // position registers.
678 __ mov(Operand(ebp, kInputStartMinusOne), eax);
Leon Clarked91b9f72010-01-27 17:25:45 +0000679
680 // Determine whether the start index is zero, that is at the start of the
681 // string, and store that value in a local variable.
Leon Clarked91b9f72010-01-27 17:25:45 +0000682 __ xor_(Operand(ecx), ecx); // setcc only operates on cl (lower byte of ecx).
Steve Block6ded16b2010-05-10 14:33:55 +0100683 // Register ebx still holds -stringIndex.
Leon Clarked91b9f72010-01-27 17:25:45 +0000684 __ test(ebx, Operand(ebx));
685 __ setcc(zero, ecx); // 1 if 0 (start of string), 0 if positive.
686 __ mov(Operand(ebp, kAtStart), ecx);
687
Steve Blocka7e24c12009-10-30 11:49:00 +0000688 if (num_saved_registers_ > 0) { // Always is, if generated from a regexp.
689 // Fill saved registers with initial value = start offset - 1
690 // Fill in stack push order, to avoid accessing across an unwritten
691 // page (a problem on Windows).
692 __ mov(ecx, kRegisterZero);
693 Label init_loop;
694 __ bind(&init_loop);
695 __ mov(Operand(ebp, ecx, times_1, +0), eax);
696 __ sub(Operand(ecx), Immediate(kPointerSize));
697 __ cmp(ecx, kRegisterZero - num_saved_registers_ * kPointerSize);
698 __ j(greater, &init_loop);
699 }
700 // Ensure that we have written to each stack page, in order. Skipping a page
701 // on Windows can cause segmentation faults. Assuming page size is 4k.
702 const int kPageSize = 4096;
703 const int kRegistersPerPage = kPageSize / kPointerSize;
704 for (int i = num_saved_registers_ + kRegistersPerPage - 1;
705 i < num_registers_;
706 i += kRegistersPerPage) {
707 __ mov(register_location(i), eax); // One write every page.
708 }
709
710
711 // Initialize backtrack stack pointer.
712 __ mov(backtrack_stackpointer(), Operand(ebp, kStackHighEnd));
713 // Load previous char as initial value of current-character.
714 Label at_start;
715 __ cmp(Operand(ebp, kAtStart), Immediate(0));
716 __ j(not_equal, &at_start);
717 LoadCurrentCharacterUnchecked(-1, 1); // Load previous char.
718 __ jmp(&start_label_);
719 __ bind(&at_start);
720 __ mov(current_character(), '\n');
721 __ jmp(&start_label_);
722
723
724 // Exit code:
725 if (success_label_.is_linked()) {
726 // Save captures when successful.
727 __ bind(&success_label_);
728 if (num_saved_registers_ > 0) {
729 // copy captures to output
730 __ mov(ebx, Operand(ebp, kRegisterOutput));
731 __ mov(ecx, Operand(ebp, kInputEnd));
Steve Block6ded16b2010-05-10 14:33:55 +0100732 __ mov(edx, Operand(ebp, kStartIndex));
Steve Blocka7e24c12009-10-30 11:49:00 +0000733 __ sub(ecx, Operand(ebp, kInputStart));
Steve Block6ded16b2010-05-10 14:33:55 +0100734 if (mode_ == UC16) {
735 __ lea(ecx, Operand(ecx, edx, times_2, 0));
736 } else {
737 __ add(ecx, Operand(edx));
738 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000739 for (int i = 0; i < num_saved_registers_; i++) {
740 __ mov(eax, register_location(i));
Steve Block6ded16b2010-05-10 14:33:55 +0100741 // Convert to index from start of string, not end.
742 __ add(eax, Operand(ecx));
Steve Blocka7e24c12009-10-30 11:49:00 +0000743 if (mode_ == UC16) {
744 __ sar(eax, 1); // Convert byte index to character index.
745 }
746 __ mov(Operand(ebx, i * kPointerSize), eax);
747 }
748 }
749 __ mov(eax, Immediate(SUCCESS));
750 }
751 // Exit and return eax
752 __ bind(&exit_label_);
753 // Skip esp past regexp registers.
754 __ lea(esp, Operand(ebp, kBackup_ebx));
755 // Restore callee-save registers.
756 __ pop(ebx);
757 __ pop(edi);
758 __ pop(esi);
759 // Exit function frame, restore previous one.
760 __ pop(ebp);
761 __ ret(0);
762
763 // Backtrack code (branch target for conditional backtracks).
764 if (backtrack_label_.is_linked()) {
765 __ bind(&backtrack_label_);
766 Backtrack();
767 }
768
769 Label exit_with_exception;
770
771 // Preempt-code
772 if (check_preempt_label_.is_linked()) {
773 SafeCallTarget(&check_preempt_label_);
774
775 __ push(backtrack_stackpointer());
776 __ push(edi);
777
778 CallCheckStackGuardState(ebx);
779 __ or_(eax, Operand(eax));
780 // If returning non-zero, we should end execution with the given
781 // result as return value.
782 __ j(not_zero, &exit_label_);
783
784 __ pop(edi);
785 __ pop(backtrack_stackpointer());
786 // String might have moved: Reload esi from frame.
787 __ mov(esi, Operand(ebp, kInputEnd));
788 SafeReturn();
789 }
790
791 // Backtrack stack overflow code.
792 if (stack_overflow_label_.is_linked()) {
793 SafeCallTarget(&stack_overflow_label_);
794 // Reached if the backtrack-stack limit has been hit.
795
796 Label grow_failed;
797 // Save registers before calling C function
798 __ push(esi);
799 __ push(edi);
800
801 // Call GrowStack(backtrack_stackpointer())
Steve Block6ded16b2010-05-10 14:33:55 +0100802 static const int num_arguments = 2;
803 __ PrepareCallCFunction(num_arguments, ebx);
Steve Blocka7e24c12009-10-30 11:49:00 +0000804 __ lea(eax, Operand(ebp, kStackHighEnd));
805 __ mov(Operand(esp, 1 * kPointerSize), eax);
806 __ mov(Operand(esp, 0 * kPointerSize), backtrack_stackpointer());
807 ExternalReference grow_stack = ExternalReference::re_grow_stack();
Steve Block6ded16b2010-05-10 14:33:55 +0100808 __ CallCFunction(grow_stack, num_arguments);
Steve Blocka7e24c12009-10-30 11:49:00 +0000809 // If return NULL, we have failed to grow the stack, and
810 // must exit with a stack-overflow exception.
811 __ or_(eax, Operand(eax));
812 __ j(equal, &exit_with_exception);
813 // Otherwise use return value as new stack pointer.
814 __ mov(backtrack_stackpointer(), eax);
815 // Restore saved registers and continue.
816 __ pop(edi);
817 __ pop(esi);
818 SafeReturn();
819 }
820
821 if (exit_with_exception.is_linked()) {
822 // If any of the code above needed to exit with an exception.
823 __ bind(&exit_with_exception);
824 // Exit with Result EXCEPTION(-1) to signal thrown exception.
825 __ mov(eax, EXCEPTION);
826 __ jmp(&exit_label_);
827 }
828
829 CodeDesc code_desc;
830 masm_->GetCode(&code_desc);
831 Handle<Code> code = Factory::NewCode(code_desc,
832 NULL,
833 Code::ComputeFlags(Code::REGEXP),
834 masm_->CodeObject());
Steve Block6ded16b2010-05-10 14:33:55 +0100835 PROFILE(RegExpCodeCreateEvent(*code, *source));
Steve Blocka7e24c12009-10-30 11:49:00 +0000836 return Handle<Object>::cast(code);
837}
838
839
840void RegExpMacroAssemblerIA32::GoTo(Label* to) {
841 BranchOrBacktrack(no_condition, to);
842}
843
844
845void RegExpMacroAssemblerIA32::IfRegisterGE(int reg,
846 int comparand,
847 Label* if_ge) {
848 __ cmp(register_location(reg), Immediate(comparand));
849 BranchOrBacktrack(greater_equal, if_ge);
850}
851
852
853void RegExpMacroAssemblerIA32::IfRegisterLT(int reg,
854 int comparand,
855 Label* if_lt) {
856 __ cmp(register_location(reg), Immediate(comparand));
857 BranchOrBacktrack(less, if_lt);
858}
859
860
861void RegExpMacroAssemblerIA32::IfRegisterEqPos(int reg,
862 Label* if_eq) {
863 __ cmp(edi, register_location(reg));
864 BranchOrBacktrack(equal, if_eq);
865}
866
867
868RegExpMacroAssembler::IrregexpImplementation
869 RegExpMacroAssemblerIA32::Implementation() {
870 return kIA32Implementation;
871}
872
873
874void RegExpMacroAssemblerIA32::LoadCurrentCharacter(int cp_offset,
875 Label* on_end_of_input,
876 bool check_bounds,
877 int characters) {
878 ASSERT(cp_offset >= -1); // ^ and \b can look behind one character.
879 ASSERT(cp_offset < (1<<30)); // Be sane! (And ensure negation works)
880 if (check_bounds) {
881 CheckPosition(cp_offset + characters - 1, on_end_of_input);
882 }
883 LoadCurrentCharacterUnchecked(cp_offset, characters);
884}
885
886
887void RegExpMacroAssemblerIA32::PopCurrentPosition() {
888 Pop(edi);
889}
890
891
892void RegExpMacroAssemblerIA32::PopRegister(int register_index) {
893 Pop(eax);
894 __ mov(register_location(register_index), eax);
895}
896
897
898void RegExpMacroAssemblerIA32::PushBacktrack(Label* label) {
899 Push(Immediate::CodeRelativeOffset(label));
900 CheckStackLimit();
901}
902
903
904void RegExpMacroAssemblerIA32::PushCurrentPosition() {
905 Push(edi);
906}
907
908
909void RegExpMacroAssemblerIA32::PushRegister(int register_index,
910 StackCheckFlag check_stack_limit) {
911 __ mov(eax, register_location(register_index));
912 Push(eax);
913 if (check_stack_limit) CheckStackLimit();
914}
915
916
917void RegExpMacroAssemblerIA32::ReadCurrentPositionFromRegister(int reg) {
918 __ mov(edi, register_location(reg));
919}
920
921
922void RegExpMacroAssemblerIA32::ReadStackPointerFromRegister(int reg) {
923 __ mov(backtrack_stackpointer(), register_location(reg));
924 __ add(backtrack_stackpointer(), Operand(ebp, kStackHighEnd));
925}
926
927
928void RegExpMacroAssemblerIA32::SetRegister(int register_index, int to) {
929 ASSERT(register_index >= num_saved_registers_); // Reserved for positions!
930 __ mov(register_location(register_index), Immediate(to));
931}
932
933
934void RegExpMacroAssemblerIA32::Succeed() {
935 __ jmp(&success_label_);
936}
937
938
939void RegExpMacroAssemblerIA32::WriteCurrentPositionToRegister(int reg,
940 int cp_offset) {
941 if (cp_offset == 0) {
942 __ mov(register_location(reg), edi);
943 } else {
944 __ lea(eax, Operand(edi, cp_offset * char_size()));
945 __ mov(register_location(reg), eax);
946 }
947}
948
949
950void RegExpMacroAssemblerIA32::ClearRegisters(int reg_from, int reg_to) {
951 ASSERT(reg_from <= reg_to);
952 __ mov(eax, Operand(ebp, kInputStartMinusOne));
953 for (int reg = reg_from; reg <= reg_to; reg++) {
954 __ mov(register_location(reg), eax);
955 }
956}
957
958
959void RegExpMacroAssemblerIA32::WriteStackPointerToRegister(int reg) {
960 __ mov(eax, backtrack_stackpointer());
961 __ sub(eax, Operand(ebp, kStackHighEnd));
962 __ mov(register_location(reg), eax);
963}
964
965
966// Private methods:
967
968void RegExpMacroAssemblerIA32::CallCheckStackGuardState(Register scratch) {
Steve Block6ded16b2010-05-10 14:33:55 +0100969 static const int num_arguments = 3;
970 __ PrepareCallCFunction(num_arguments, scratch);
Steve Blocka7e24c12009-10-30 11:49:00 +0000971 // RegExp code frame pointer.
972 __ mov(Operand(esp, 2 * kPointerSize), ebp);
973 // Code* of self.
974 __ mov(Operand(esp, 1 * kPointerSize), Immediate(masm_->CodeObject()));
975 // Next address on the stack (will be address of return address).
976 __ lea(eax, Operand(esp, -kPointerSize));
977 __ mov(Operand(esp, 0 * kPointerSize), eax);
978 ExternalReference check_stack_guard =
979 ExternalReference::re_check_stack_guard_state();
Steve Block6ded16b2010-05-10 14:33:55 +0100980 __ CallCFunction(check_stack_guard, num_arguments);
Steve Blocka7e24c12009-10-30 11:49:00 +0000981}
982
983
984// Helper function for reading a value out of a stack frame.
985template <typename T>
986static T& frame_entry(Address re_frame, int frame_offset) {
987 return reinterpret_cast<T&>(Memory::int32_at(re_frame + frame_offset));
988}
989
990
991int RegExpMacroAssemblerIA32::CheckStackGuardState(Address* return_address,
992 Code* re_code,
993 Address re_frame) {
994 if (StackGuard::IsStackOverflow()) {
995 Top::StackOverflow();
996 return EXCEPTION;
997 }
998
999 // If not real stack overflow the stack guard was used to interrupt
1000 // execution for another purpose.
1001
Leon Clarkee46be812010-01-19 14:06:41 +00001002 // If this is a direct call from JavaScript retry the RegExp forcing the call
1003 // through the runtime system. Currently the direct call cannot handle a GC.
1004 if (frame_entry<int>(re_frame, kDirectCall) == 1) {
1005 return RETRY;
1006 }
1007
Steve Blocka7e24c12009-10-30 11:49:00 +00001008 // Prepare for possible GC.
1009 HandleScope handles;
1010 Handle<Code> code_handle(re_code);
1011
1012 Handle<String> subject(frame_entry<String*>(re_frame, kInputString));
1013 // Current string.
1014 bool is_ascii = subject->IsAsciiRepresentation();
1015
1016 ASSERT(re_code->instruction_start() <= *return_address);
1017 ASSERT(*return_address <=
1018 re_code->instruction_start() + re_code->instruction_size());
1019
1020 Object* result = Execution::HandleStackGuardInterrupt();
1021
1022 if (*code_handle != re_code) { // Return address no longer valid
1023 int delta = *code_handle - re_code;
1024 // Overwrite the return address on the stack.
1025 *return_address += delta;
1026 }
1027
1028 if (result->IsException()) {
1029 return EXCEPTION;
1030 }
1031
1032 // String might have changed.
1033 if (subject->IsAsciiRepresentation() != is_ascii) {
1034 // If we changed between an ASCII and an UC16 string, the specialized
1035 // code cannot be used, and we need to restart regexp matching from
1036 // scratch (including, potentially, compiling a new version of the code).
1037 return RETRY;
1038 }
1039
1040 // Otherwise, the content of the string might have moved. It must still
1041 // be a sequential or external string with the same content.
1042 // Update the start and end pointers in the stack frame to the current
1043 // location (whether it has actually moved or not).
1044 ASSERT(StringShape(*subject).IsSequential() ||
1045 StringShape(*subject).IsExternal());
1046
1047 // The original start address of the characters to match.
1048 const byte* start_address = frame_entry<const byte*>(re_frame, kInputStart);
1049
1050 // Find the current start address of the same character at the current string
1051 // position.
1052 int start_index = frame_entry<int>(re_frame, kStartIndex);
1053 const byte* new_address = StringCharacterPosition(*subject, start_index);
1054
1055 if (start_address != new_address) {
1056 // If there is a difference, update the object pointer and start and end
1057 // addresses in the RegExp stack frame to match the new value.
1058 const byte* end_address = frame_entry<const byte* >(re_frame, kInputEnd);
1059 int byte_length = end_address - start_address;
1060 frame_entry<const String*>(re_frame, kInputString) = *subject;
1061 frame_entry<const byte*>(re_frame, kInputStart) = new_address;
1062 frame_entry<const byte*>(re_frame, kInputEnd) = new_address + byte_length;
1063 }
1064
1065 return 0;
1066}
1067
1068
1069Operand RegExpMacroAssemblerIA32::register_location(int register_index) {
1070 ASSERT(register_index < (1<<30));
1071 if (num_registers_ <= register_index) {
1072 num_registers_ = register_index + 1;
1073 }
1074 return Operand(ebp, kRegisterZero - register_index * kPointerSize);
1075}
1076
1077
1078void RegExpMacroAssemblerIA32::CheckPosition(int cp_offset,
1079 Label* on_outside_input) {
1080 __ cmp(edi, -cp_offset * char_size());
1081 BranchOrBacktrack(greater_equal, on_outside_input);
1082}
1083
1084
1085void RegExpMacroAssemblerIA32::BranchOrBacktrack(Condition condition,
1086 Label* to,
1087 Hint hint) {
1088 if (condition < 0) { // No condition
1089 if (to == NULL) {
1090 Backtrack();
1091 return;
1092 }
1093 __ jmp(to);
1094 return;
1095 }
1096 if (to == NULL) {
1097 __ j(condition, &backtrack_label_, hint);
1098 return;
1099 }
1100 __ j(condition, to, hint);
1101}
1102
1103
1104void RegExpMacroAssemblerIA32::SafeCall(Label* to) {
Steve Block6ded16b2010-05-10 14:33:55 +01001105 Label return_to;
1106 __ push(Immediate::CodeRelativeOffset(&return_to));
1107 __ jmp(to);
1108 __ bind(&return_to);
Steve Blocka7e24c12009-10-30 11:49:00 +00001109}
1110
1111
1112void RegExpMacroAssemblerIA32::SafeReturn() {
Steve Block6ded16b2010-05-10 14:33:55 +01001113 __ pop(ebx);
1114 __ add(Operand(ebx), Immediate(masm_->CodeObject()));
1115 __ jmp(Operand(ebx));
Steve Blocka7e24c12009-10-30 11:49:00 +00001116}
1117
1118
1119void RegExpMacroAssemblerIA32::SafeCallTarget(Label* name) {
1120 __ bind(name);
Steve Blocka7e24c12009-10-30 11:49:00 +00001121}
1122
1123
1124void RegExpMacroAssemblerIA32::Push(Register source) {
1125 ASSERT(!source.is(backtrack_stackpointer()));
1126 // Notice: This updates flags, unlike normal Push.
1127 __ sub(Operand(backtrack_stackpointer()), Immediate(kPointerSize));
1128 __ mov(Operand(backtrack_stackpointer(), 0), source);
1129}
1130
1131
1132void RegExpMacroAssemblerIA32::Push(Immediate value) {
1133 // Notice: This updates flags, unlike normal Push.
1134 __ sub(Operand(backtrack_stackpointer()), Immediate(kPointerSize));
1135 __ mov(Operand(backtrack_stackpointer(), 0), value);
1136}
1137
1138
1139void RegExpMacroAssemblerIA32::Pop(Register target) {
1140 ASSERT(!target.is(backtrack_stackpointer()));
1141 __ mov(target, Operand(backtrack_stackpointer(), 0));
1142 // Notice: This updates flags, unlike normal Pop.
1143 __ add(Operand(backtrack_stackpointer()), Immediate(kPointerSize));
1144}
1145
1146
1147void RegExpMacroAssemblerIA32::CheckPreemption() {
1148 // Check for preemption.
1149 Label no_preempt;
Steve Blockd0582a62009-12-15 09:54:21 +00001150 ExternalReference stack_limit =
1151 ExternalReference::address_of_stack_limit();
1152 __ cmp(esp, Operand::StaticVariable(stack_limit));
Steve Blocka7e24c12009-10-30 11:49:00 +00001153 __ j(above, &no_preempt, taken);
1154
1155 SafeCall(&check_preempt_label_);
1156
1157 __ bind(&no_preempt);
1158}
1159
1160
1161void RegExpMacroAssemblerIA32::CheckStackLimit() {
Steve Blockd0582a62009-12-15 09:54:21 +00001162 Label no_stack_overflow;
1163 ExternalReference stack_limit =
1164 ExternalReference::address_of_regexp_stack_limit();
1165 __ cmp(backtrack_stackpointer(), Operand::StaticVariable(stack_limit));
1166 __ j(above, &no_stack_overflow);
Steve Blocka7e24c12009-10-30 11:49:00 +00001167
Steve Blockd0582a62009-12-15 09:54:21 +00001168 SafeCall(&stack_overflow_label_);
Steve Blocka7e24c12009-10-30 11:49:00 +00001169
Steve Blockd0582a62009-12-15 09:54:21 +00001170 __ bind(&no_stack_overflow);
Steve Blocka7e24c12009-10-30 11:49:00 +00001171}
1172
1173
Steve Blocka7e24c12009-10-30 11:49:00 +00001174void RegExpMacroAssemblerIA32::LoadCurrentCharacterUnchecked(int cp_offset,
1175 int characters) {
1176 if (mode_ == ASCII) {
1177 if (characters == 4) {
1178 __ mov(current_character(), Operand(esi, edi, times_1, cp_offset));
1179 } else if (characters == 2) {
1180 __ movzx_w(current_character(), Operand(esi, edi, times_1, cp_offset));
1181 } else {
1182 ASSERT(characters == 1);
1183 __ movzx_b(current_character(), Operand(esi, edi, times_1, cp_offset));
1184 }
1185 } else {
1186 ASSERT(mode_ == UC16);
1187 if (characters == 2) {
1188 __ mov(current_character(),
1189 Operand(esi, edi, times_1, cp_offset * sizeof(uc16)));
1190 } else {
1191 ASSERT(characters == 1);
1192 __ movzx_w(current_character(),
1193 Operand(esi, edi, times_1, cp_offset * sizeof(uc16)));
1194 }
1195 }
1196}
1197
1198
Steve Blocka7e24c12009-10-30 11:49:00 +00001199#undef __
1200
Steve Block6ded16b2010-05-10 14:33:55 +01001201#endif // V8_INTERPRETED_REGEXP
Steve Blocka7e24c12009-10-30 11:49:00 +00001202
1203}} // namespace v8::internal