blob: 83dac5469f49e6b56d2e419764c3e2a03e9d712b [file] [log] [blame]
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001// Copyright 2008-2009 the V8 project authors. All rights reserved.
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002// 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
ager@chromium.orga74f0da2008-12-03 16:05:52 +000028#include "v8.h"
ager@chromium.org8bb60582008-12-11 12:02:20 +000029#include "unicode.h"
ager@chromium.orga74f0da2008-12-03 16:05:52 +000030#include "log.h"
31#include "ast.h"
ager@chromium.org32912102009-01-16 10:38:43 +000032#include "regexp-stack.h"
ager@chromium.orga74f0da2008-12-03 16:05:52 +000033#include "macro-assembler.h"
34#include "regexp-macro-assembler.h"
ager@chromium.org3a37e9b2009-04-27 09:26:21 +000035#include "ia32/macro-assembler-ia32.h"
36#include "ia32/regexp-macro-assembler-ia32.h"
ager@chromium.orga74f0da2008-12-03 16:05:52 +000037
38namespace v8 { namespace internal {
39
40/*
41 * This assembler uses the following register assignment convention
42 * - edx : current character. Must be loaded using LoadCurrentCharacter
43 * before using any of the dispatch methods.
44 * - edi : current position in input, as negative offset from end of string.
45 * Please notice that this is the byte offset, not the character offset!
46 * - esi : end of input (points to byte after last character in input).
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000047 * - ebp : frame pointer. Used to access arguments, local variables and
48 * RegExp registers.
ager@chromium.org32912102009-01-16 10:38:43 +000049 * - esp : points to tip of C stack.
50 * - ecx : points to tip of backtrack stack
ager@chromium.orga74f0da2008-12-03 16:05:52 +000051 *
52 * The registers eax, ebx and ecx are free to use for computations.
53 *
54 * Each call to a public method should retain this convention.
55 * The stack will have the following structure:
ager@chromium.org32912102009-01-16 10:38:43 +000056 * - stack_area_top (High end of the memory area to use as
57 * backtracking stack)
ager@chromium.orga74f0da2008-12-03 16:05:52 +000058 * - at_start (if 1, start at start of string, if 0, don't)
59 * - int* capture_array (int[num_saved_registers_], for output).
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000060 * - end of input (Address of end of string)
61 * - start of input (Address of first character in string)
62 * - void* input_string (location of a handle containing the string)
63 * --- frame alignment (if applicable) ---
ager@chromium.orga74f0da2008-12-03 16:05:52 +000064 * - return address
ager@chromium.orga74f0da2008-12-03 16:05:52 +000065 * ebp-> - old ebp
ager@chromium.org32912102009-01-16 10:38:43 +000066 * - backup of caller esi
67 * - backup of caller edi
68 * - backup of caller ebx
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000069 * - Offset of location before start of input (effectively character
70 * position -1). Used to initialize capture registers to a non-position.
ager@chromium.orga74f0da2008-12-03 16:05:52 +000071 * - register 0 ebp[-4] (Only positions must be stored in the first
72 * - register 1 ebp[-8] num_saved_registers_ registers)
73 * - ...
74 *
75 * The first num_saved_registers_ registers are initialized to point to
76 * "character -1" in the string (i.e., char_size() bytes before the first
77 * character of the string). The remaining registers starts out as garbage.
78 *
79 * The data up to the return address must be placed there by the calling
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000080 * code, e.g., by calling the code entry as cast to:
81 * int (*match)(String* input_string,
82 * Address start,
83 * Address end,
84 * int* capture_output_array,
85 * bool at_start,
86 * byte* stack_area_top)
ager@chromium.orga74f0da2008-12-03 16:05:52 +000087 */
88
ager@chromium.org65dad4b2009-04-23 08:48:43 +000089#define __ ACCESS_MASM(masm_)
ager@chromium.orga74f0da2008-12-03 16:05:52 +000090
91RegExpMacroAssemblerIA32::RegExpMacroAssemblerIA32(
92 Mode mode,
93 int registers_to_save)
94 : masm_(new MacroAssembler(NULL, kRegExpCodeSize)),
95 constants_(kRegExpConstantsSize),
96 mode_(mode),
97 num_registers_(registers_to_save),
98 num_saved_registers_(registers_to_save),
99 entry_label_(),
100 start_label_(),
101 success_label_(),
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000102 backtrack_label_(),
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000103 exit_label_() {
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000104 __ jmp(&entry_label_); // We'll write the entry code later.
105 __ bind(&start_label_); // And then continue from here.
106}
107
108
109RegExpMacroAssemblerIA32::~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();
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000115 backtrack_label_.Unuse();
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000116 exit_label_.Unuse();
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000117 check_preempt_label_.Unuse();
ager@chromium.org32912102009-01-16 10:38:43 +0000118 stack_overflow_label_.Unuse();
119}
120
121
122int RegExpMacroAssemblerIA32::stack_limit_slack() {
123 return RegExpStack::kStackLimitSlack;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000124}
125
126
127void RegExpMacroAssemblerIA32::AdvanceCurrentPosition(int by) {
ager@chromium.org8bb60582008-12-11 12:02:20 +0000128 if (by != 0) {
129 Label inside_string;
130 __ add(Operand(edi), Immediate(by * char_size()));
131 }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000132}
133
134
135void RegExpMacroAssemblerIA32::AdvanceRegister(int reg, int by) {
136 ASSERT(reg >= 0);
137 ASSERT(reg < num_registers_);
ager@chromium.org32912102009-01-16 10:38:43 +0000138 if (by != 0) {
139 __ add(register_location(reg), Immediate(by));
140 }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000141}
142
143
144void RegExpMacroAssemblerIA32::Backtrack() {
ager@chromium.org32912102009-01-16 10:38:43 +0000145 CheckPreemption();
146 // Pop Code* offset from backtrack stack, add Code* and jump to location.
147 Pop(ebx);
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000148 __ add(Operand(ebx), Immediate(masm_->CodeObject()));
ager@chromium.org32912102009-01-16 10:38:43 +0000149 __ jmp(Operand(ebx));
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000150}
151
152
153void RegExpMacroAssemblerIA32::Bind(Label* label) {
154 __ bind(label);
155}
156
ager@chromium.org32912102009-01-16 10:38:43 +0000157
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000158void RegExpMacroAssemblerIA32::CheckBitmap(uc16 start,
159 Label* bitmap,
160 Label* on_zero) {
ager@chromium.org8bb60582008-12-11 12:02:20 +0000161 UNIMPLEMENTED();
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000162}
163
164
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000165void RegExpMacroAssemblerIA32::CheckCharacter(uint32_t c, Label* on_equal) {
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000166 __ cmp(current_character(), c);
167 BranchOrBacktrack(equal, on_equal);
168}
169
170
171void RegExpMacroAssemblerIA32::CheckCharacterGT(uc16 limit, Label* on_greater) {
172 __ cmp(current_character(), limit);
173 BranchOrBacktrack(greater, on_greater);
174}
175
176
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000177void RegExpMacroAssemblerIA32::CheckAtStart(Label* on_at_start) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000178 Label not_at_start;
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000179 // Did we start the match at the start of the string at all?
180 __ cmp(Operand(ebp, kAtStart), Immediate(0));
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000181 BranchOrBacktrack(equal, &not_at_start);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000182 // If we did, are we still at the start of the input?
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000183 __ lea(eax, Operand(esi, edi, times_1, 0));
184 __ cmp(eax, Operand(ebp, kInputStart));
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000185 BranchOrBacktrack(equal, on_at_start);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000186 __ bind(&not_at_start);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000187}
188
189
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000190void RegExpMacroAssemblerIA32::CheckNotAtStart(Label* on_not_at_start) {
ager@chromium.org32912102009-01-16 10:38:43 +0000191 // Did we start the match at the start of the string at all?
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000192 __ cmp(Operand(ebp, kAtStart), Immediate(0));
193 BranchOrBacktrack(equal, on_not_at_start);
ager@chromium.org32912102009-01-16 10:38:43 +0000194 // If we did, are we still at the start of the input?
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000195 __ lea(eax, Operand(esi, edi, times_1, 0));
196 __ cmp(eax, Operand(ebp, kInputStart));
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000197 BranchOrBacktrack(not_equal, on_not_at_start);
198}
199
200
201void RegExpMacroAssemblerIA32::CheckCharacterLT(uc16 limit, Label* on_less) {
202 __ cmp(current_character(), limit);
203 BranchOrBacktrack(less, on_less);
204}
205
206
207void RegExpMacroAssemblerIA32::CheckCharacters(Vector<const uc16> str,
208 int cp_offset,
ager@chromium.org8bb60582008-12-11 12:02:20 +0000209 Label* on_failure,
210 bool check_end_of_string) {
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000211 int byte_length = str.length() * char_size();
212 int byte_offset = cp_offset * char_size();
ager@chromium.org8bb60582008-12-11 12:02:20 +0000213 if (check_end_of_string) {
ager@chromium.org32912102009-01-16 10:38:43 +0000214 // Check that there are at least str.length() characters left in the input.
ager@chromium.org8bb60582008-12-11 12:02:20 +0000215 __ cmp(Operand(edi), Immediate(-(byte_offset + byte_length)));
216 BranchOrBacktrack(greater, on_failure);
217 }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000218
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000219 Label backtrack;
220 if (on_failure == NULL) {
221 // Avoid inlining the Backtrack macro for each test.
222 Label skip_backtrack;
223 __ jmp(&skip_backtrack);
224 __ bind(&backtrack);
225 Backtrack();
226 __ bind(&skip_backtrack);
227 on_failure = &backtrack;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000228 }
229
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000230 for (int i = 0; i < str.length(); i++) {
231 if (mode_ == ASCII) {
232 __ cmpb(Operand(esi, edi, times_1, byte_offset + i),
233 static_cast<int8_t>(str[i]));
234 } else {
235 ASSERT(mode_ == UC16);
236 __ cmpw(Operand(esi, edi, times_1, byte_offset + i * sizeof(uc16)),
237 Immediate(str[i]));
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000238 }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000239 BranchOrBacktrack(not_equal, on_failure);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000240 }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000241}
242
243
ager@chromium.org8bb60582008-12-11 12:02:20 +0000244void RegExpMacroAssemblerIA32::CheckGreedyLoop(Label* on_equal) {
245 Label fallthrough;
ager@chromium.org32912102009-01-16 10:38:43 +0000246 __ cmp(edi, Operand(backtrack_stackpointer(), 0));
ager@chromium.org8bb60582008-12-11 12:02:20 +0000247 __ j(not_equal, &fallthrough);
ager@chromium.org32912102009-01-16 10:38:43 +0000248 __ add(Operand(backtrack_stackpointer()), Immediate(kPointerSize)); // Pop.
ager@chromium.org8bb60582008-12-11 12:02:20 +0000249 BranchOrBacktrack(no_condition, on_equal);
250 __ bind(&fallthrough);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000251}
252
253
254void RegExpMacroAssemblerIA32::CheckNotBackReferenceIgnoreCase(
ager@chromium.org8bb60582008-12-11 12:02:20 +0000255 int start_reg,
256 Label* on_no_match) {
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000257 Label fallthrough;
ager@chromium.org32912102009-01-16 10:38:43 +0000258 __ mov(edx, register_location(start_reg)); // Index of start of capture
259 __ mov(ebx, register_location(start_reg + 1)); // Index of end of capture
260 __ sub(ebx, Operand(edx)); // Length of capture.
261
262 // The length of a capture should not be negative. This can only happen
263 // if the end of the capture is unrecorded, or at a point earlier than
264 // the start of the capture.
265 BranchOrBacktrack(less, on_no_match, not_taken);
266
267 // If length is zero, either the capture is empty or it is completely
268 // uncaptured. In either case succeed immediately.
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000269 __ j(equal, &fallthrough);
270
ager@chromium.org8bb60582008-12-11 12:02:20 +0000271 if (mode_ == ASCII) {
272 Label success;
273 Label fail;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000274 Label loop_increment;
ager@chromium.org32912102009-01-16 10:38:43 +0000275 // Save register contents to make the registers available below.
ager@chromium.org8bb60582008-12-11 12:02:20 +0000276 __ push(edi);
ager@chromium.org32912102009-01-16 10:38:43 +0000277 __ push(backtrack_stackpointer());
iposva@chromium.org245aa852009-02-10 00:49:54 +0000278 // After this, the eax, ecx, and edi registers are available.
ager@chromium.org32912102009-01-16 10:38:43 +0000279
280 __ add(edx, Operand(esi)); // Start of capture
281 __ add(edi, Operand(esi)); // Start of text to match against capture.
282 __ add(ebx, Operand(edi)); // End of text to match against capture.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000283
ager@chromium.org8bb60582008-12-11 12:02:20 +0000284 Label loop;
285 __ bind(&loop);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000286 __ movzx_b(eax, Operand(edi, 0));
287 __ cmpb_al(Operand(edx, 0));
288 __ j(equal, &loop_increment);
289
ager@chromium.org32912102009-01-16 10:38:43 +0000290 // Mismatch, try case-insensitive match (converting letters to lower-case).
291 __ or_(eax, 0x20); // Convert match character to lower-case.
292 __ lea(ecx, Operand(eax, -'a'));
293 __ cmp(ecx, static_cast<int32_t>('z' - 'a')); // Is eax a lowercase letter?
ager@chromium.org8bb60582008-12-11 12:02:20 +0000294 __ j(above, &fail);
ager@chromium.org32912102009-01-16 10:38:43 +0000295 // Also convert capture character.
296 __ movzx_b(ecx, Operand(edx, 0));
297 __ or_(ecx, 0x20);
298
299 __ cmp(eax, Operand(ecx));
ager@chromium.org8bb60582008-12-11 12:02:20 +0000300 __ j(not_equal, &fail);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000301
302 __ bind(&loop_increment);
ager@chromium.org32912102009-01-16 10:38:43 +0000303 // Increment pointers into match and capture strings.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000304 __ add(Operand(edx), Immediate(1));
305 __ add(Operand(edi), Immediate(1));
ager@chromium.org32912102009-01-16 10:38:43 +0000306 // Compare to end of match, and loop if not done.
307 __ cmp(edi, Operand(ebx));
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000308 __ j(below, &loop, taken);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000309 __ jmp(&success);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000310
ager@chromium.org8bb60582008-12-11 12:02:20 +0000311 __ bind(&fail);
ager@chromium.org32912102009-01-16 10:38:43 +0000312 // Restore original values before failing.
313 __ pop(backtrack_stackpointer());
ager@chromium.org8bb60582008-12-11 12:02:20 +0000314 __ pop(edi);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000315 BranchOrBacktrack(no_condition, on_no_match);
316
317 __ bind(&success);
ager@chromium.org32912102009-01-16 10:38:43 +0000318 // Restore original value before continuing.
319 __ pop(backtrack_stackpointer());
320 // Drop original value of character position.
321 __ add(Operand(esp), Immediate(kPointerSize));
322 // Compute new value of character position after the matched part.
ager@chromium.org8bb60582008-12-11 12:02:20 +0000323 __ sub(edi, Operand(esi));
324 } else {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000325 ASSERT(mode_ == UC16);
ager@chromium.org32912102009-01-16 10:38:43 +0000326 // Save registers before calling C function.
ager@chromium.org8bb60582008-12-11 12:02:20 +0000327 __ push(esi);
328 __ push(edi);
ager@chromium.org32912102009-01-16 10:38:43 +0000329 __ push(backtrack_stackpointer());
330 __ push(ebx);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000331
332 const int argument_count = 3;
333 FrameAlign(argument_count, ecx);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000334 // Put arguments into allocated stack area, last argument highest on stack.
335 // Parameters are
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000336 // Address byte_offset1 - Address captured substring's start.
337 // Address byte_offset2 - Address of current character position.
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000338 // size_t byte_length - length of capture in bytes(!)
339
340 // Set byte_length.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000341 __ mov(Operand(esp, 2 * kPointerSize), ebx);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000342 // Set byte_offset2.
343 // Found by adding negative string-end offset of current position (edi)
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000344 // to end of string.
345 __ add(edi, Operand(esi));
346 __ mov(Operand(esp, 1 * kPointerSize), edi);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000347 // Set byte_offset1.
iposva@chromium.org245aa852009-02-10 00:49:54 +0000348 // Start of capture, where edx already holds string-end negative offset.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000349 __ add(edx, Operand(esi));
350 __ mov(Operand(esp, 0 * kPointerSize), edx);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000351
ager@chromium.org8bb60582008-12-11 12:02:20 +0000352 Address function_address = FUNCTION_ADDR(&CaseInsensitiveCompareUC16);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000353 CallCFunction(function_address, argument_count);
ager@chromium.org32912102009-01-16 10:38:43 +0000354 // Pop original values before reacting on result value.
355 __ pop(ebx);
356 __ pop(backtrack_stackpointer());
ager@chromium.org8bb60582008-12-11 12:02:20 +0000357 __ pop(edi);
358 __ pop(esi);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000359
ager@chromium.org32912102009-01-16 10:38:43 +0000360 // Check if function returned non-zero for success or zero for failure.
ager@chromium.org8bb60582008-12-11 12:02:20 +0000361 __ or_(eax, Operand(eax));
362 BranchOrBacktrack(zero, on_no_match);
ager@chromium.org32912102009-01-16 10:38:43 +0000363 // On success, increment position by length of capture.
364 __ add(edi, Operand(ebx));
ager@chromium.org8bb60582008-12-11 12:02:20 +0000365 }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000366 __ bind(&fallthrough);
367}
368
369
370void RegExpMacroAssemblerIA32::CheckNotBackReference(
ager@chromium.org8bb60582008-12-11 12:02:20 +0000371 int start_reg,
372 Label* on_no_match) {
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000373 Label fallthrough;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000374 Label success;
375 Label fail;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000376
ager@chromium.org32912102009-01-16 10:38:43 +0000377 // Find length of back-referenced capture.
378 __ mov(edx, register_location(start_reg));
379 __ mov(eax, register_location(start_reg + 1));
380 __ sub(eax, Operand(edx)); // Length to check.
381 // Fail on partial or illegal capture (start of capture after end of capture).
382 BranchOrBacktrack(less, on_no_match);
383 // Succeed on empty capture (including no capture)
384 __ j(equal, &fallthrough);
385
386 // Check that there are sufficient characters left in the input.
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000387 __ mov(ebx, edi);
ager@chromium.org32912102009-01-16 10:38:43 +0000388 __ add(ebx, Operand(eax));
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000389 BranchOrBacktrack(greater, on_no_match);
390
ager@chromium.org32912102009-01-16 10:38:43 +0000391 // Save register to make it available below.
392 __ push(backtrack_stackpointer());
393
394 // Compute pointers to match string and capture string
395 __ lea(ebx, Operand(esi, edi, times_1, 0)); // Start of match.
396 __ add(edx, Operand(esi)); // Start of capture.
397 __ lea(ecx, Operand(eax, ebx, times_1, 0)); // End of match
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000398
399 Label loop;
400 __ bind(&loop);
401 if (mode_ == ASCII) {
402 __ movzx_b(eax, Operand(edx, 0));
ager@chromium.org32912102009-01-16 10:38:43 +0000403 __ cmpb_al(Operand(ebx, 0));
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000404 } else {
405 ASSERT(mode_ == UC16);
406 __ movzx_w(eax, Operand(edx, 0));
ager@chromium.org32912102009-01-16 10:38:43 +0000407 __ cmpw_ax(Operand(ebx, 0));
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000408 }
409 __ j(not_equal, &fail);
ager@chromium.org32912102009-01-16 10:38:43 +0000410 // Increment pointers into capture and match string.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000411 __ add(Operand(edx), Immediate(char_size()));
ager@chromium.org32912102009-01-16 10:38:43 +0000412 __ add(Operand(ebx), Immediate(char_size()));
413 // Check if we have reached end of match area.
414 __ cmp(ebx, Operand(ecx));
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000415 __ j(below, &loop);
416 __ jmp(&success);
417
418 __ bind(&fail);
ager@chromium.org32912102009-01-16 10:38:43 +0000419 // Restore backtrack stackpointer.
420 __ pop(backtrack_stackpointer());
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000421 BranchOrBacktrack(no_condition, on_no_match);
422
423 __ bind(&success);
ager@chromium.org32912102009-01-16 10:38:43 +0000424 // Move current character position to position after match.
425 __ mov(edi, ecx);
426 __ sub(Operand(edi), esi);
427 // Restore backtrack stackpointer.
428 __ pop(backtrack_stackpointer());
429
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000430 __ bind(&fallthrough);
431}
432
433
434void RegExpMacroAssemblerIA32::CheckNotRegistersEqual(int reg1,
435 int reg2,
436 Label* on_not_equal) {
437 __ mov(eax, register_location(reg1));
438 __ cmp(eax, register_location(reg2));
439 BranchOrBacktrack(not_equal, on_not_equal);
440}
441
442
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000443void RegExpMacroAssemblerIA32::CheckNotCharacter(uint32_t c,
444 Label* on_not_equal) {
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000445 __ cmp(current_character(), c);
446 BranchOrBacktrack(not_equal, on_not_equal);
447}
448
449
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000450void RegExpMacroAssemblerIA32::CheckCharacterAfterAnd(uint32_t c,
451 uint32_t mask,
452 Label* on_equal) {
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000453 __ mov(eax, current_character());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000454 __ and_(eax, mask);
455 __ cmp(eax, c);
456 BranchOrBacktrack(equal, on_equal);
457}
458
459
460void RegExpMacroAssemblerIA32::CheckNotCharacterAfterAnd(uint32_t c,
461 uint32_t mask,
462 Label* on_not_equal) {
463 __ mov(eax, current_character());
464 __ and_(eax, mask);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000465 __ cmp(eax, c);
466 BranchOrBacktrack(not_equal, on_not_equal);
467}
468
469
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000470void RegExpMacroAssemblerIA32::CheckNotCharacterAfterMinusAnd(
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000471 uc16 c,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000472 uc16 minus,
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000473 uc16 mask,
474 Label* on_not_equal) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000475 ASSERT(minus < String::kMaxUC16CharCode);
476 __ lea(eax, Operand(current_character(), -minus));
477 __ and_(eax, mask);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000478 __ cmp(eax, c);
479 BranchOrBacktrack(not_equal, on_not_equal);
480}
481
ager@chromium.org32912102009-01-16 10:38:43 +0000482
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000483bool RegExpMacroAssemblerIA32::CheckSpecialCharacterClass(uc16 type,
484 int cp_offset,
485 bool check_offset,
486 Label* on_no_match) {
487 // Range checks (c in min..max) are generally implemented by an unsigned
488 // (c - min) <= (max - min) check
489 switch (type) {
490 case 's':
491 // Match space-characters
492 if (mode_ == ASCII) {
493 // ASCII space characters are '\t'..'\r' and ' '.
494 if (check_offset) {
495 LoadCurrentCharacter(cp_offset, on_no_match);
496 } else {
497 LoadCurrentCharacterUnchecked(cp_offset, 1);
498 }
499 Label success;
500 __ cmp(current_character(), ' ');
501 __ j(equal, &success);
502 // Check range 0x09..0x0d
503 __ sub(Operand(current_character()), Immediate('\t'));
504 __ cmp(current_character(), '\r' - '\t');
ager@chromium.org32912102009-01-16 10:38:43 +0000505 BranchOrBacktrack(above, on_no_match);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000506 __ bind(&success);
507 return true;
508 }
509 return false;
510 case 'S':
511 // Match non-space characters.
512 if (check_offset) {
513 LoadCurrentCharacter(cp_offset, on_no_match, 1);
514 } else {
515 LoadCurrentCharacterUnchecked(cp_offset, 1);
516 }
517 if (mode_ == ASCII) {
518 // ASCII space characters are '\t'..'\r' and ' '.
519 __ cmp(current_character(), ' ');
520 BranchOrBacktrack(equal, on_no_match);
521 __ sub(Operand(current_character()), Immediate('\t'));
522 __ cmp(current_character(), '\r' - '\t');
ager@chromium.org32912102009-01-16 10:38:43 +0000523 BranchOrBacktrack(below_equal, on_no_match);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000524 return true;
525 }
526 return false;
527 case 'd':
528 // Match ASCII digits ('0'..'9')
529 if (check_offset) {
530 LoadCurrentCharacter(cp_offset, on_no_match, 1);
531 } else {
532 LoadCurrentCharacterUnchecked(cp_offset, 1);
533 }
534 __ sub(Operand(current_character()), Immediate('0'));
535 __ cmp(current_character(), '9' - '0');
ager@chromium.org32912102009-01-16 10:38:43 +0000536 BranchOrBacktrack(above, on_no_match);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000537 return true;
538 case 'D':
539 // Match non ASCII-digits
540 if (check_offset) {
541 LoadCurrentCharacter(cp_offset, on_no_match, 1);
542 } else {
543 LoadCurrentCharacterUnchecked(cp_offset, 1);
544 }
545 __ sub(Operand(current_character()), Immediate('0'));
546 __ cmp(current_character(), '9' - '0');
ager@chromium.org32912102009-01-16 10:38:43 +0000547 BranchOrBacktrack(below_equal, on_no_match);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000548 return true;
549 case '.': {
550 // Match non-newlines (not 0x0a('\n'), 0x0d('\r'), 0x2028 and 0x2029)
551 if (check_offset) {
552 LoadCurrentCharacter(cp_offset, on_no_match, 1);
553 } else {
554 LoadCurrentCharacterUnchecked(cp_offset, 1);
555 }
ager@chromium.org32912102009-01-16 10:38:43 +0000556 __ xor_(Operand(current_character()), Immediate(0x01));
557 // See if current character is '\n'^1 or '\r'^1, i.e., 0x0b or 0x0c
558 __ sub(Operand(current_character()), Immediate(0x0b));
559 __ cmp(current_character(), 0x0c - 0x0b);
560 BranchOrBacktrack(below_equal, on_no_match);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000561 if (mode_ == UC16) {
562 // Compare original value to 0x2028 and 0x2029, using the already
ager@chromium.org32912102009-01-16 10:38:43 +0000563 // computed (current_char ^ 0x01 - 0x0b). I.e., check for
564 // 0x201d (0x2028 - 0x0b) or 0x201e.
565 __ sub(Operand(current_character()), Immediate(0x2028 - 0x0b));
566 __ cmp(current_character(), 1);
567 BranchOrBacktrack(below_equal, on_no_match);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000568 }
569 return true;
570 }
571 case '*':
572 // Match any character.
573 if (check_offset) {
574 CheckPosition(cp_offset, on_no_match);
575 }
576 return true;
577 // No custom implementation (yet): w, W, s(UC16), S(UC16).
578 default:
579 return false;
580 }
581}
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000582
583void RegExpMacroAssemblerIA32::DispatchHalfNibbleMap(
584 uc16 start,
585 Label* half_nibble_map,
586 const Vector<Label*>& destinations) {
587 UNIMPLEMENTED();
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000588}
589
590
591void RegExpMacroAssemblerIA32::DispatchByteMap(
592 uc16 start,
593 Label* byte_map,
594 const Vector<Label*>& destinations) {
595 UNIMPLEMENTED();
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000596}
597
598
599void RegExpMacroAssemblerIA32::DispatchHighByteMap(
600 byte start,
601 Label* byte_map,
602 const Vector<Label*>& destinations) {
603 UNIMPLEMENTED();
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000604}
605
606
607void RegExpMacroAssemblerIA32::EmitOrLink(Label* label) {
608 UNIMPLEMENTED(); // Has no use.
609}
610
611
612void RegExpMacroAssemblerIA32::Fail() {
ager@chromium.org32912102009-01-16 10:38:43 +0000613 ASSERT(FAILURE == 0); // Return value for failure is zero.
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000614 __ xor_(eax, Operand(eax)); // zero eax.
615 __ jmp(&exit_label_);
616}
617
618
ager@chromium.org8bb60582008-12-11 12:02:20 +0000619Handle<Object> RegExpMacroAssemblerIA32::GetCode(Handle<String> source) {
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000620 // Finalize code - write the entry point code now we know how many
621 // registers we need.
622
623 // Entry code:
624 __ bind(&entry_label_);
ager@chromium.org32912102009-01-16 10:38:43 +0000625 // Start new stack frame.
626 __ push(ebp);
627 __ mov(ebp, esp);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000628 // Save callee-save registers. Order here should correspond to order of
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000629 // kBackup_ebx etc.
630 __ push(esi);
631 __ push(edi);
632 __ push(ebx); // Callee-save on MacOS.
iposva@chromium.org245aa852009-02-10 00:49:54 +0000633 __ push(Immediate(0)); // Make room for "input start - 1" constant.
ager@chromium.org32912102009-01-16 10:38:43 +0000634
635 // Check if we have space on the stack for registers.
636 Label retry_stack_check;
637 Label stack_limit_hit;
638 Label stack_ok;
639
640 __ bind(&retry_stack_check);
641 ExternalReference stack_guard_limit =
642 ExternalReference::address_of_stack_guard_limit();
643 __ mov(ecx, esp);
644 __ sub(ecx, Operand::StaticVariable(stack_guard_limit));
645 // Handle it if the stack pointer is already below the stack limit.
646 __ j(below_equal, &stack_limit_hit, not_taken);
647 // Check if there is room for the variable number of registers above
648 // the stack limit.
649 __ cmp(ecx, num_registers_ * kPointerSize);
650 __ j(above_equal, &stack_ok, taken);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000651 // Exit with OutOfMemory exception. There is not enough space on the stack
652 // for our working registers.
ager@chromium.org32912102009-01-16 10:38:43 +0000653 __ mov(eax, EXCEPTION);
654 __ jmp(&exit_label_);
655
656 __ bind(&stack_limit_hit);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000657 CallCheckStackGuardState(ebx);
ager@chromium.org32912102009-01-16 10:38:43 +0000658 __ or_(eax, Operand(eax));
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000659 // If returned value is non-zero, we exit with the returned value as result.
ager@chromium.org32912102009-01-16 10:38:43 +0000660 // Otherwise it was a preemption and we just check the limit again.
661 __ j(equal, &retry_stack_check);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000662 // Return value was non-zero. Exit with exception or retry.
ager@chromium.org32912102009-01-16 10:38:43 +0000663 __ jmp(&exit_label_);
664
665 __ bind(&stack_ok);
666
667 // Allocate space on stack for registers.
668 __ sub(Operand(esp), Immediate(num_registers_ * kPointerSize));
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000669 // Load string length.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000670 __ mov(esi, Operand(ebp, kInputEnd));
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000671 // Load input position.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000672 __ mov(edi, Operand(ebp, kInputStart));
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000673 // Set up edi to be negative offset from string end.
674 __ sub(edi, Operand(esi));
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000675 if (num_saved_registers_ > 0) {
676 // Fill saved registers with initial value = start offset - 1
ager@chromium.org32912102009-01-16 10:38:43 +0000677 // Fill in stack push order, to avoid accessing across an unwritten
678 // page (a problem on Windows).
679 __ mov(ecx, kRegisterZero);
680 // Set eax to address of char before start of input
681 // (effectively string position -1).
682 __ lea(eax, Operand(edi, -char_size()));
683 // Store this value in a local variable, for use when clearing
684 // position registers.
685 __ mov(Operand(ebp, kInputStartMinusOne), eax);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000686 Label init_loop;
687 __ bind(&init_loop);
ager@chromium.org32912102009-01-16 10:38:43 +0000688 __ mov(Operand(ebp, ecx, times_1, +0), eax);
689 __ sub(Operand(ecx), Immediate(kPointerSize));
690 __ cmp(ecx, kRegisterZero - num_saved_registers_ * kPointerSize);
691 __ j(greater, &init_loop);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000692 }
ager@chromium.org32912102009-01-16 10:38:43 +0000693 // Ensure that we have written to each stack page, in order. Skipping a page
694 // on Windows can cause segmentation faults. Assuming page size is 4k.
695 const int kPageSize = 4096;
696 const int kRegistersPerPage = kPageSize / kPointerSize;
697 for (int i = num_saved_registers_ + kRegistersPerPage - 1;
698 i < num_registers_;
699 i += kRegistersPerPage) {
700 __ mov(register_location(i), eax); // One write every page.
701 }
702
703
704 // Initialize backtrack stack pointer.
705 __ mov(backtrack_stackpointer(), Operand(ebp, kStackHighEnd));
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000706 // Load previous char as initial value of current-character.
707 Label at_start;
708 __ cmp(Operand(ebp, kAtStart), Immediate(0));
709 __ j(not_equal, &at_start);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000710 LoadCurrentCharacterUnchecked(-1, 1); // Load previous char.
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000711 __ jmp(&start_label_);
712 __ bind(&at_start);
713 __ mov(current_character(), '\n');
714 __ jmp(&start_label_);
715
716
717 // Exit code:
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000718 if (success_label_.is_linked()) {
ager@chromium.org32912102009-01-16 10:38:43 +0000719 // Save captures when successful.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000720 __ bind(&success_label_);
721 if (num_saved_registers_ > 0) {
722 // copy captures to output
723 __ mov(ebx, Operand(ebp, kRegisterOutput));
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000724 __ mov(ecx, Operand(ebp, kInputEnd));
725 __ sub(ecx, Operand(ebp, kInputStart));
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000726 for (int i = 0; i < num_saved_registers_; i++) {
727 __ mov(eax, register_location(i));
728 __ add(eax, Operand(ecx)); // Convert to index from start, not end.
729 if (mode_ == UC16) {
730 __ sar(eax, 1); // Convert byte index to character index.
731 }
732 __ mov(Operand(ebx, i * kPointerSize), eax);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000733 }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000734 }
ager@chromium.org32912102009-01-16 10:38:43 +0000735 __ mov(eax, Immediate(SUCCESS));
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000736 }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000737 // Exit and return eax
738 __ bind(&exit_label_);
ager@chromium.org32912102009-01-16 10:38:43 +0000739 // Skip esp past regexp registers.
740 __ lea(esp, Operand(ebp, kBackup_ebx));
741 // Restore callee-save registers.
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000742 __ pop(ebx);
743 __ pop(edi);
744 __ pop(esi);
ager@chromium.org32912102009-01-16 10:38:43 +0000745 // Exit function frame, restore previous one.
746 __ pop(ebp);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000747 __ ret(0);
748
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000749 // Backtrack code (branch target for conditional backtracks).
750 if (backtrack_label_.is_linked()) {
751 __ bind(&backtrack_label_);
752 Backtrack();
753 }
754
ager@chromium.org32912102009-01-16 10:38:43 +0000755 Label exit_with_exception;
756
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000757 // Preempt-code
758 if (check_preempt_label_.is_linked()) {
759 __ bind(&check_preempt_label_);
ager@chromium.org32912102009-01-16 10:38:43 +0000760
761 __ push(backtrack_stackpointer());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000762 __ push(edi);
763
764 Label retry;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000765
766 __ bind(&retry);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000767 CallCheckStackGuardState(ebx);
768 __ or_(eax, Operand(eax));
769 // If returning non-zero, we should end execution with the given
770 // result as return value.
771 __ j(not_zero, &exit_label_);
772 // Check if we are still preempted.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000773 ExternalReference stack_guard_limit =
774 ExternalReference::address_of_stack_guard_limit();
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000775 __ cmp(esp, Operand::StaticVariable(stack_guard_limit));
776 __ j(below_equal, &retry);
777
778 __ pop(edi);
ager@chromium.org32912102009-01-16 10:38:43 +0000779 __ pop(backtrack_stackpointer());
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000780 // String might have moved: Reload esi from frame.
781 __ mov(esi, Operand(ebp, kInputEnd));
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000782 SafeReturn();
ager@chromium.org32912102009-01-16 10:38:43 +0000783 }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000784
ager@chromium.org32912102009-01-16 10:38:43 +0000785 // Backtrack stack overflow code.
786 if (stack_overflow_label_.is_linked()) {
787 __ bind(&stack_overflow_label_);
788 // Reached if the backtrack-stack limit has been hit.
789
790 Label grow_failed;
791 // Save registers before calling C function
792 __ push(esi);
793 __ push(edi);
794
795 // Call GrowStack(backtrack_stackpointer())
ager@chromium.org6f10e412009-02-13 10:11:16 +0000796 int num_arguments = 2;
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000797 FrameAlign(num_arguments, ebx);
ager@chromium.org6f10e412009-02-13 10:11:16 +0000798 __ lea(eax, Operand(ebp, kStackHighEnd));
799 __ mov(Operand(esp, 1 * kPointerSize), eax);
800 __ mov(Operand(esp, 0 * kPointerSize), backtrack_stackpointer());
ager@chromium.org32912102009-01-16 10:38:43 +0000801 CallCFunction(FUNCTION_ADDR(&GrowStack), num_arguments);
802 // If return NULL, we have failed to grow the stack, and
803 // must exit with a stack-overflow exception.
804 __ or_(eax, Operand(eax));
805 __ j(equal, &exit_with_exception);
806 // Otherwise use return value as new stack pointer.
807 __ mov(backtrack_stackpointer(), eax);
808 // Restore saved registers and continue.
809 __ pop(edi);
810 __ pop(esi);
811 SafeReturn();
812 }
813
814 if (exit_with_exception.is_linked()) {
815 // If any of the code above needed to exit with an exception.
816 __ bind(&exit_with_exception);
817 // Exit with Result EXCEPTION(-1) to signal thrown exception.
818 __ mov(eax, EXCEPTION);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000819 __ jmp(&exit_label_);
820 }
821
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000822 CodeDesc code_desc;
823 masm_->GetCode(&code_desc);
824 Handle<Code> code = Factory::NewCode(code_desc,
825 NULL,
826 Code::ComputeFlags(Code::REGEXP),
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000827 masm_->CodeObject());
ager@chromium.org71daaf62009-04-01 07:22:49 +0000828 LOG(RegExpCodeCreateEvent(*code, *source));
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000829 return Handle<Object>::cast(code);
830}
831
832
833void RegExpMacroAssemblerIA32::GoTo(Label* to) {
ager@chromium.org8bb60582008-12-11 12:02:20 +0000834 BranchOrBacktrack(no_condition, to);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000835}
836
837
838void RegExpMacroAssemblerIA32::IfRegisterGE(int reg,
839 int comparand,
840 Label* if_ge) {
841 __ cmp(register_location(reg), Immediate(comparand));
842 BranchOrBacktrack(greater_equal, if_ge);
843}
844
845
846void RegExpMacroAssemblerIA32::IfRegisterLT(int reg,
847 int comparand,
848 Label* if_lt) {
849 __ cmp(register_location(reg), Immediate(comparand));
850 BranchOrBacktrack(less, if_lt);
851}
852
853
ager@chromium.org32912102009-01-16 10:38:43 +0000854void RegExpMacroAssemblerIA32::IfRegisterEqPos(int reg,
855 Label* if_eq) {
856 __ cmp(edi, register_location(reg));
857 BranchOrBacktrack(equal, if_eq);
858}
859
860
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000861RegExpMacroAssembler::IrregexpImplementation
862 RegExpMacroAssemblerIA32::Implementation() {
863 return kIA32Implementation;
864}
865
866
867void RegExpMacroAssemblerIA32::LoadCurrentCharacter(int cp_offset,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000868 Label* on_end_of_input,
869 bool check_bounds,
870 int characters) {
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000871 ASSERT(cp_offset >= -1); // ^ and \b can look behind one character.
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000872 ASSERT(cp_offset < (1<<30)); // Be sane! (And ensure negation works)
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000873 CheckPosition(cp_offset + characters - 1, on_end_of_input);
874 LoadCurrentCharacterUnchecked(cp_offset, characters);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000875}
876
877
878void RegExpMacroAssemblerIA32::PopCurrentPosition() {
ager@chromium.org32912102009-01-16 10:38:43 +0000879 Pop(edi);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000880}
881
882
883void RegExpMacroAssemblerIA32::PopRegister(int register_index) {
ager@chromium.org32912102009-01-16 10:38:43 +0000884 Pop(eax);
885 __ mov(register_location(register_index), eax);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000886}
887
888
889void RegExpMacroAssemblerIA32::PushBacktrack(Label* label) {
ager@chromium.org32912102009-01-16 10:38:43 +0000890 Push(Immediate::CodeRelativeOffset(label));
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000891 CheckStackLimit();
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000892}
893
894
895void RegExpMacroAssemblerIA32::PushCurrentPosition() {
ager@chromium.org32912102009-01-16 10:38:43 +0000896 Push(edi);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000897}
898
899
ager@chromium.org32912102009-01-16 10:38:43 +0000900void RegExpMacroAssemblerIA32::PushRegister(int register_index,
901 StackCheckFlag check_stack_limit) {
902 __ mov(eax, register_location(register_index));
903 Push(eax);
904 if (check_stack_limit) CheckStackLimit();
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000905}
906
907
908void RegExpMacroAssemblerIA32::ReadCurrentPositionFromRegister(int reg) {
909 __ mov(edi, register_location(reg));
910}
911
912
913void RegExpMacroAssemblerIA32::ReadStackPointerFromRegister(int reg) {
ager@chromium.org32912102009-01-16 10:38:43 +0000914 __ mov(backtrack_stackpointer(), register_location(reg));
ager@chromium.org6f10e412009-02-13 10:11:16 +0000915 __ add(backtrack_stackpointer(), Operand(ebp, kStackHighEnd));
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000916}
917
918
919void RegExpMacroAssemblerIA32::SetRegister(int register_index, int to) {
920 ASSERT(register_index >= num_saved_registers_); // Reserved for positions!
921 __ mov(register_location(register_index), Immediate(to));
922}
923
924
925void RegExpMacroAssemblerIA32::Succeed() {
926 __ jmp(&success_label_);
927}
928
929
ager@chromium.org8bb60582008-12-11 12:02:20 +0000930void RegExpMacroAssemblerIA32::WriteCurrentPositionToRegister(int reg,
931 int cp_offset) {
932 if (cp_offset == 0) {
933 __ mov(register_location(reg), edi);
934 } else {
935 __ lea(eax, Operand(edi, cp_offset * char_size()));
936 __ mov(register_location(reg), eax);
937 }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000938}
939
ager@chromium.org8bb60582008-12-11 12:02:20 +0000940
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000941void RegExpMacroAssemblerIA32::ClearRegisters(int reg_from, int reg_to) {
942 ASSERT(reg_from <= reg_to);
ager@chromium.org32912102009-01-16 10:38:43 +0000943 __ mov(eax, Operand(ebp, kInputStartMinusOne));
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000944 for (int reg = reg_from; reg <= reg_to; reg++) {
945 __ mov(register_location(reg), eax);
946 }
ager@chromium.org32912102009-01-16 10:38:43 +0000947}
948
949
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000950void RegExpMacroAssemblerIA32::WriteStackPointerToRegister(int reg) {
ager@chromium.org6f10e412009-02-13 10:11:16 +0000951 __ mov(eax, backtrack_stackpointer());
952 __ sub(eax, Operand(ebp, kStackHighEnd));
953 __ mov(register_location(reg), eax);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000954}
955
956
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000957RegExpMacroAssemblerIA32::Result RegExpMacroAssemblerIA32::Match(
958 Handle<Code> regexp_code,
959 Handle<String> subject,
960 int* offsets_vector,
961 int offsets_vector_length,
962 int previous_index) {
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000963
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000964 ASSERT(subject->IsFlat());
ager@chromium.org41826e72009-03-30 13:30:57 +0000965 ASSERT(previous_index >= 0);
966 ASSERT(previous_index <= subject->length());
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000967
968 // No allocations before calling the regexp, but we can't use
969 // AssertNoAllocation, since regexps might be preempted, and another thread
970 // might do allocation anyway.
971
972 String* subject_ptr = *subject;
973 // Character offsets into string.
974 int start_offset = previous_index;
975 int end_offset = subject_ptr->length();
976
977 bool is_ascii = StringShape(*subject).IsAsciiRepresentation();
978
979 if (StringShape(subject_ptr).IsCons()) {
980 subject_ptr = ConsString::cast(subject_ptr)->first();
981 } else if (StringShape(subject_ptr).IsSliced()) {
982 SlicedString* slice = SlicedString::cast(subject_ptr);
983 start_offset += slice->start();
984 end_offset += slice->start();
985 subject_ptr = slice->buffer();
986 }
987 // Ensure that an underlying string has the same ascii-ness.
988 ASSERT(StringShape(subject_ptr).IsAsciiRepresentation() == is_ascii);
989 ASSERT(subject_ptr->IsExternalString() || subject_ptr->IsSeqString());
990 // String is now either Sequential or External
991 int char_size_shift = is_ascii ? 0 : 1;
992 int char_length = end_offset - start_offset;
993
994 const byte* input_start =
995 StringCharacterPosition(subject_ptr, start_offset);
996 int byte_length = char_length << char_size_shift;
997 const byte* input_end = input_start + byte_length;
998 RegExpMacroAssemblerIA32::Result res = Execute(*regexp_code,
999 subject_ptr,
1000 start_offset,
1001 input_start,
1002 input_end,
1003 offsets_vector,
1004 previous_index == 0);
1005
1006 if (res == SUCCESS) {
1007 // Capture values are relative to start_offset only.
1008 // Convert them to be relative to start of string.
1009 for (int i = 0; i < offsets_vector_length; i++) {
1010 if (offsets_vector[i] >= 0) {
1011 offsets_vector[i] += previous_index;
1012 }
1013 }
1014 }
1015
1016 return res;
1017}
1018
1019// Private methods:
ager@chromium.org8bb60582008-12-11 12:02:20 +00001020
1021static unibrow::Mapping<unibrow::Ecma262Canonicalize> canonicalize;
1022
ager@chromium.org32912102009-01-16 10:38:43 +00001023RegExpMacroAssemblerIA32::Result RegExpMacroAssemblerIA32::Execute(
1024 Code* code,
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001025 String* input,
ager@chromium.org32912102009-01-16 10:38:43 +00001026 int start_offset,
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001027 const byte* input_start,
1028 const byte* input_end,
ager@chromium.org32912102009-01-16 10:38:43 +00001029 int* output,
1030 bool at_start) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001031 typedef int (*matcher)(String*, int, const byte*,
1032 const byte*, int*, int, Address);
ager@chromium.org32912102009-01-16 10:38:43 +00001033 matcher matcher_func = FUNCTION_CAST<matcher>(code->entry());
1034
1035 int at_start_val = at_start ? 1 : 0;
1036
1037 // Ensure that the minimum stack has been allocated.
1038 RegExpStack stack;
ager@chromium.org6f10e412009-02-13 10:11:16 +00001039 Address stack_top = RegExpStack::stack_top();
ager@chromium.org32912102009-01-16 10:38:43 +00001040
1041 int result = matcher_func(input,
1042 start_offset,
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001043 input_start,
1044 input_end,
ager@chromium.org32912102009-01-16 10:38:43 +00001045 output,
1046 at_start_val,
1047 stack_top);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001048 ASSERT(result <= SUCCESS);
1049 ASSERT(result >= RETRY);
ager@chromium.org32912102009-01-16 10:38:43 +00001050
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001051 if (result == EXCEPTION && !Top::has_pending_exception()) {
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001052 // We detected a stack overflow (on the backtrack stack) in RegExp code,
1053 // but haven't created the exception yet.
ager@chromium.org32912102009-01-16 10:38:43 +00001054 Top::StackOverflow();
1055 }
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001056 return static_cast<Result>(result);
ager@chromium.org32912102009-01-16 10:38:43 +00001057}
1058
1059
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001060int RegExpMacroAssemblerIA32::CaseInsensitiveCompareUC16(Address byte_offset1,
1061 Address byte_offset2,
ager@chromium.org8bb60582008-12-11 12:02:20 +00001062 size_t byte_length) {
1063 // This function is not allowed to cause a garbage collection.
1064 // A GC might move the calling generated code and invalidate the
1065 // return address on the stack.
1066 ASSERT(byte_length % 2 == 0);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001067 uc16* substring1 = reinterpret_cast<uc16*>(byte_offset1);
1068 uc16* substring2 = reinterpret_cast<uc16*>(byte_offset2);
ager@chromium.org8bb60582008-12-11 12:02:20 +00001069 size_t length = byte_length >> 1;
1070
1071 for (size_t i = 0; i < length; i++) {
1072 unibrow::uchar c1 = substring1[i];
1073 unibrow::uchar c2 = substring2[i];
1074 if (c1 != c2) {
1075 canonicalize.get(c1, '\0', &c1);
1076 if (c1 != c2) {
1077 canonicalize.get(c2, '\0', &c2);
1078 if (c1 != c2) {
1079 return 0;
1080 }
1081 }
1082 }
1083 }
1084 return 1;
1085}
1086
1087
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001088void RegExpMacroAssemblerIA32::CallCheckStackGuardState(Register scratch) {
1089 int num_arguments = 3;
1090 FrameAlign(num_arguments, scratch);
1091 // RegExp code frame pointer.
1092 __ mov(Operand(esp, 2 * kPointerSize), ebp);
1093 // Code* of self.
1094 __ mov(Operand(esp, 1 * kPointerSize), Immediate(masm_->CodeObject()));
1095 // Next address on the stack (will be address of return address).
1096 __ lea(eax, Operand(esp, -kPointerSize));
1097 __ mov(Operand(esp, 0 * kPointerSize), eax);
1098 CallCFunction(FUNCTION_ADDR(&CheckStackGuardState), num_arguments);
1099}
1100
1101
1102// Helper function for reading a value out of a stack frame.
1103template <typename T>
1104static T& frame_entry(Address re_frame, int frame_offset) {
1105 return reinterpret_cast<T&>(Memory::int32_at(re_frame + frame_offset));
1106}
1107
1108
1109const byte* RegExpMacroAssemblerIA32::StringCharacterPosition(String* subject,
1110 int start_index) {
1111 // Not just flat, but ultra flat.
1112 ASSERT(subject->IsExternalString() || subject->IsSeqString());
1113 ASSERT(start_index >= 0);
1114 ASSERT(start_index <= subject->length());
1115 if (StringShape(subject).IsAsciiRepresentation()) {
1116 const byte* address;
1117 if (StringShape(subject).IsExternal()) {
1118 const char* data = ExternalAsciiString::cast(subject)->resource()->data();
1119 address = reinterpret_cast<const byte*>(data);
1120 } else {
1121 ASSERT(subject->IsSeqAsciiString());
1122 char* data = SeqAsciiString::cast(subject)->GetChars();
1123 address = reinterpret_cast<const byte*>(data);
1124 }
1125 return address + start_index;
1126 }
1127 const uc16* data;
1128 if (StringShape(subject).IsExternal()) {
1129 data = ExternalTwoByteString::cast(subject)->resource()->data();
1130 } else {
1131 ASSERT(subject->IsSeqTwoByteString());
1132 data = SeqTwoByteString::cast(subject)->GetChars();
1133 }
1134 return reinterpret_cast<const byte*>(data + start_index);
1135}
1136
1137
ager@chromium.org381abbb2009-02-25 13:23:22 +00001138int RegExpMacroAssemblerIA32::CheckStackGuardState(Address* return_address,
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001139 Code* re_code,
1140 Address re_frame) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001141 if (StackGuard::IsStackOverflow()) {
1142 Top::StackOverflow();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001143 return EXCEPTION;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001144 }
1145
1146 // If not real stack overflow the stack guard was used to interrupt
1147 // execution for another purpose.
1148
1149 // Prepare for possible GC.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001150 HandleScope handles;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001151 Handle<Code> code_handle(re_code);
ager@chromium.org32912102009-01-16 10:38:43 +00001152
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001153 Handle<String> subject(frame_entry<String*>(re_frame, kInputString));
1154 // Current string.
1155 bool is_ascii = StringShape(*subject).IsAsciiRepresentation();
1156
ager@chromium.org381abbb2009-02-25 13:23:22 +00001157 ASSERT(re_code->instruction_start() <= *return_address);
1158 ASSERT(*return_address <=
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001159 re_code->instruction_start() + re_code->instruction_size());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001160
1161 Object* result = Execution::HandleStackGuardInterrupt();
1162
1163 if (*code_handle != re_code) { // Return address no longer valid
1164 int delta = *code_handle - re_code;
ager@chromium.org381abbb2009-02-25 13:23:22 +00001165 // Overwrite the return address on the stack.
1166 *return_address += delta;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001167 }
1168
1169 if (result->IsException()) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001170 return EXCEPTION;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001171 }
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001172
1173 // String might have changed.
1174 if (StringShape(*subject).IsAsciiRepresentation() != is_ascii) {
1175 // If we changed between an ASCII and an UC16 string, the specialized
1176 // code cannot be used, and we need to restart regexp matching from
1177 // scratch (including, potentially, compiling a new version of the code).
1178 return RETRY;
1179 }
1180
1181 // Otherwise, the content of the string might have moved. It must still
1182 // be a sequential or external string with the same content.
1183 // Update the start and end pointers in the stack frame to the current
1184 // location (whether it has actually moved or not).
1185 ASSERT(StringShape(*subject).IsSequential() ||
1186 StringShape(*subject).IsExternal());
1187
1188 // The original start address of the characters to match.
1189 const byte* start_address = frame_entry<const byte*>(re_frame, kInputStart);
1190
1191 // Find the current start address of the same character at the current string
1192 // position.
1193 int start_index = frame_entry<int>(re_frame, kStartIndex);
1194 const byte* new_address = StringCharacterPosition(*subject, start_index);
1195
1196 if (start_address != new_address) {
1197 // If there is a difference, update start and end addresses in the
1198 // RegExp stack frame to match the new value.
1199 const byte* end_address = frame_entry<const byte* >(re_frame, kInputEnd);
1200 int byte_length = end_address - start_address;
1201 frame_entry<const byte*>(re_frame, kInputStart) = new_address;
1202 frame_entry<const byte*>(re_frame, kInputEnd) = new_address + byte_length;
1203 }
1204
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001205 return 0;
1206}
1207
1208
ager@chromium.org6f10e412009-02-13 10:11:16 +00001209Address RegExpMacroAssemblerIA32::GrowStack(Address stack_pointer,
1210 Address* stack_top) {
ager@chromium.org32912102009-01-16 10:38:43 +00001211 size_t size = RegExpStack::stack_capacity();
ager@chromium.org6f10e412009-02-13 10:11:16 +00001212 Address old_stack_top = RegExpStack::stack_top();
1213 ASSERT(old_stack_top == *stack_top);
1214 ASSERT(stack_pointer <= old_stack_top);
1215 ASSERT(static_cast<size_t>(old_stack_top - stack_pointer) <= size);
1216 Address new_stack_top = RegExpStack::EnsureCapacity(size * 2);
1217 if (new_stack_top == NULL) {
ager@chromium.org32912102009-01-16 10:38:43 +00001218 return NULL;
1219 }
ager@chromium.org6f10e412009-02-13 10:11:16 +00001220 *stack_top = new_stack_top;
1221 return new_stack_top - (old_stack_top - stack_pointer);
ager@chromium.org32912102009-01-16 10:38:43 +00001222}
1223
1224
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001225Operand RegExpMacroAssemblerIA32::register_location(int register_index) {
1226 ASSERT(register_index < (1<<30));
1227 if (num_registers_ <= register_index) {
1228 num_registers_ = register_index + 1;
1229 }
ager@chromium.org32912102009-01-16 10:38:43 +00001230 return Operand(ebp, kRegisterZero - register_index * kPointerSize);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001231}
1232
1233
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001234void RegExpMacroAssemblerIA32::CheckPosition(int cp_offset,
1235 Label* on_outside_input) {
1236 __ cmp(edi, -cp_offset * char_size());
1237 BranchOrBacktrack(greater_equal, on_outside_input);
1238}
1239
1240
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001241void RegExpMacroAssemblerIA32::BranchOrBacktrack(Condition condition,
ager@chromium.org32912102009-01-16 10:38:43 +00001242 Label* to,
1243 Hint hint) {
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001244 if (condition < 0) { // No condition
1245 if (to == NULL) {
1246 Backtrack();
1247 return;
1248 }
1249 __ jmp(to);
1250 return;
1251 }
1252 if (to == NULL) {
ager@chromium.org32912102009-01-16 10:38:43 +00001253 __ j(condition, &backtrack_label_, hint);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001254 return;
1255 }
ager@chromium.org32912102009-01-16 10:38:43 +00001256 __ j(condition, to, hint);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001257}
1258
1259
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001260void RegExpMacroAssemblerIA32::SafeCall(Label* to) {
1261 Label return_to;
1262 __ push(Immediate::CodeRelativeOffset(&return_to));
1263 __ jmp(to);
1264 __ bind(&return_to);
1265}
1266
1267
1268void RegExpMacroAssemblerIA32::SafeReturn() {
ager@chromium.org32912102009-01-16 10:38:43 +00001269 __ pop(ebx);
kasperl@chromium.org061ef742009-02-27 12:16:20 +00001270 __ add(Operand(ebx), Immediate(masm_->CodeObject()));
ager@chromium.org32912102009-01-16 10:38:43 +00001271 __ jmp(Operand(ebx));
1272}
1273
1274
1275void RegExpMacroAssemblerIA32::Push(Register source) {
1276 ASSERT(!source.is(backtrack_stackpointer()));
1277 // Notice: This updates flags, unlike normal Push.
1278 __ sub(Operand(backtrack_stackpointer()), Immediate(kPointerSize));
1279 __ mov(Operand(backtrack_stackpointer(), 0), source);
1280}
1281
1282
1283void RegExpMacroAssemblerIA32::Push(Immediate value) {
1284 // Notice: This updates flags, unlike normal Push.
1285 __ sub(Operand(backtrack_stackpointer()), Immediate(kPointerSize));
1286 __ mov(Operand(backtrack_stackpointer(), 0), value);
1287}
1288
1289
1290void RegExpMacroAssemblerIA32::Pop(Register target) {
1291 ASSERT(!target.is(backtrack_stackpointer()));
1292 __ mov(target, Operand(backtrack_stackpointer(), 0));
1293 // Notice: This updates flags, unlike normal Pop.
1294 __ add(Operand(backtrack_stackpointer()), Immediate(kPointerSize));
1295}
1296
1297
1298void RegExpMacroAssemblerIA32::CheckPreemption() {
1299 // Check for preemption.
1300 Label no_preempt;
1301 ExternalReference stack_guard_limit =
1302 ExternalReference::address_of_stack_guard_limit();
1303 __ cmp(esp, Operand::StaticVariable(stack_guard_limit));
1304 __ j(above, &no_preempt, taken);
1305
1306 SafeCall(&check_preempt_label_);
1307
1308 __ bind(&no_preempt);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001309}
1310
1311
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001312void RegExpMacroAssemblerIA32::CheckStackLimit() {
1313 if (FLAG_check_stack) {
ager@chromium.org32912102009-01-16 10:38:43 +00001314 Label no_stack_overflow;
1315 ExternalReference stack_limit =
1316 ExternalReference::address_of_regexp_stack_limit();
1317 __ cmp(backtrack_stackpointer(), Operand::StaticVariable(stack_limit));
1318 __ j(above, &no_stack_overflow);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001319
ager@chromium.org32912102009-01-16 10:38:43 +00001320 SafeCall(&stack_overflow_label_);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001321
ager@chromium.org32912102009-01-16 10:38:43 +00001322 __ bind(&no_stack_overflow);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001323 }
1324}
1325
1326
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001327void RegExpMacroAssemblerIA32::FrameAlign(int num_arguments, Register scratch) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001328 // TODO(lrn): Since we no longer use the system stack arbitrarily (but we do
1329 // use it, e.g., for SafeCall), we know the number of elements on the stack
1330 // since the last frame alignment. We might be able to do this simpler then.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001331 int frameAlignment = OS::ActivationFrameAlignment();
1332 if (frameAlignment != 0) {
1333 // Make stack end at alignment and make room for num_arguments words
1334 // and the original value of esp.
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001335 __ mov(scratch, esp);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001336 __ sub(Operand(esp), Immediate((num_arguments + 1) * kPointerSize));
1337 ASSERT(IsPowerOf2(frameAlignment));
1338 __ and_(esp, -frameAlignment);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001339 __ mov(Operand(esp, num_arguments * kPointerSize), scratch);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001340 } else {
1341 __ sub(Operand(esp), Immediate(num_arguments * kPointerSize));
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001342 }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001343}
1344
1345
1346void RegExpMacroAssemblerIA32::CallCFunction(Address function_address,
1347 int num_arguments) {
1348 __ mov(Operand(eax), Immediate(reinterpret_cast<int32_t>(function_address)));
1349 __ call(Operand(eax));
1350 if (OS::ActivationFrameAlignment() != 0) {
1351 __ mov(esp, Operand(esp, num_arguments * kPointerSize));
1352 } else {
1353 __ add(Operand(esp), Immediate(num_arguments * sizeof(int32_t)));
1354 }
1355}
1356
1357
1358void RegExpMacroAssemblerIA32::LoadCurrentCharacterUnchecked(int cp_offset,
1359 int characters) {
1360 if (mode_ == ASCII) {
1361 if (characters == 4) {
1362 __ mov(current_character(), Operand(esi, edi, times_1, cp_offset));
1363 } else if (characters == 2) {
1364 __ movzx_w(current_character(), Operand(esi, edi, times_1, cp_offset));
1365 } else {
1366 ASSERT(characters == 1);
1367 __ movzx_b(current_character(), Operand(esi, edi, times_1, cp_offset));
1368 }
1369 } else {
1370 ASSERT(mode_ == UC16);
1371 if (characters == 2) {
1372 __ mov(current_character(),
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001373 Operand(esi, edi, times_1, cp_offset * sizeof(uc16)));
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001374 } else {
1375 ASSERT(characters == 1);
1376 __ movzx_w(current_character(),
1377 Operand(esi, edi, times_1, cp_offset * sizeof(uc16)));
1378 }
1379 }
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001380}
1381
1382
1383void RegExpMacroAssemblerIA32::LoadConstantBufferAddress(Register reg,
1384 ArraySlice* buffer) {
1385 __ mov(reg, buffer->array());
1386 __ add(Operand(reg), Immediate(buffer->base_offset()));
1387}
1388
1389#undef __
1390}} // namespace v8::internal