blob: 82a3735d77e9ea1c4e3b9f7f4539f6d950419545 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// Copyright 2012 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Steve Blocka7e24c12009-10-30 11:49:00 +00004
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005#include "src/v8.h"
Leon Clarkef7060e22010-06-03 12:02:55 +01006
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007#if V8_TARGET_ARCH_X64
Leon Clarkef7060e22010-06-03 12:02:55 +01008
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009#include "src/cpu-profiler.h"
10#include "src/log.h"
11#include "src/macro-assembler.h"
12#include "src/regexp-macro-assembler.h"
13#include "src/regexp-stack.h"
14#include "src/serialize.h"
15#include "src/unicode.h"
16#include "src/x64/regexp-macro-assembler-x64.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000017
18namespace v8 {
19namespace internal {
20
Steve Block6ded16b2010-05-10 14:33:55 +010021#ifndef V8_INTERPRETED_REGEXP
Steve Blocka7e24c12009-10-30 11:49:00 +000022
23/*
24 * This assembler uses the following register assignment convention
Ben Murdochb8a8cc12014-11-26 15:28:44 +000025 * - rdx : Currently loaded character(s) as Latin1 or UC16. Must be loaded
26 * using LoadCurrentCharacter before using any of the dispatch methods.
27 * Temporarily stores the index of capture start after a matching pass
28 * for a global regexp.
29 * - rdi : Current position in input, as negative offset from end of string.
Steve Blocka7e24c12009-10-30 11:49:00 +000030 * Please notice that this is the byte offset, not the character
Ben Murdochb8a8cc12014-11-26 15:28:44 +000031 * offset! Is always a 32-bit signed (negative) offset, but must be
Steve Blocka7e24c12009-10-30 11:49:00 +000032 * maintained sign-extended to 64 bits, since it is used as index.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000033 * - rsi : End of input (points to byte after last character in input),
Steve Blocka7e24c12009-10-30 11:49:00 +000034 * so that rsi+rdi points to the current character.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000035 * - rbp : Frame pointer. Used to access arguments, local variables and
Steve Blocka7e24c12009-10-30 11:49:00 +000036 * RegExp registers.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000037 * - rsp : Points to tip of C stack.
38 * - rcx : Points to tip of backtrack stack. The backtrack stack contains
39 * only 32-bit values. Most are offsets from some base (e.g., character
Steve Blocka7e24c12009-10-30 11:49:00 +000040 * positions from end of string or code location from Code* pointer).
Ben Murdochb8a8cc12014-11-26 15:28:44 +000041 * - r8 : Code object pointer. Used to convert between absolute and
Steve Blocka7e24c12009-10-30 11:49:00 +000042 * code-object-relative addresses.
43 *
Leon Clarkee46be812010-01-19 14:06:41 +000044 * The registers rax, rbx, r9 and r11 are free to use for computations.
Steve Blocka7e24c12009-10-30 11:49:00 +000045 * If changed to use r12+, they should be saved as callee-save registers.
Steve Block44f0eee2011-05-26 01:26:41 +010046 * The macro assembler special registers r12 and r13 (kSmiConstantRegister,
47 * kRootRegister) aren't special during execution of RegExp code (they don't
48 * hold the values assumed when creating JS code), so no Smi or Root related
49 * macro operations can be used.
Steve Blocka7e24c12009-10-30 11:49:00 +000050 *
51 * Each call to a C++ method should retain these registers.
52 *
53 * The stack will have the following content, in some order, indexable from the
54 * frame pointer (see, e.g., kStackHighEnd):
Ben Murdochb8a8cc12014-11-26 15:28:44 +000055 * - Isolate* isolate (address of the current isolate)
Leon Clarkee46be812010-01-19 14:06:41 +000056 * - direct_call (if 1, direct call from JavaScript code, if 0 call
57 * through the runtime system)
Ben Murdochb8a8cc12014-11-26 15:28:44 +000058 * - stack_area_base (high end of the memory area to use as
Leon Clarkee46be812010-01-19 14:06:41 +000059 * backtracking stack)
Ben Murdochb8a8cc12014-11-26 15:28:44 +000060 * - capture array size (may fit multiple sets of matches)
Leon Clarkee46be812010-01-19 14:06:41 +000061 * - int* capture_array (int[num_saved_registers_], for output).
Ben Murdochb8a8cc12014-11-26 15:28:44 +000062 * - end of input (address of end of string)
63 * - start of input (address of first character in string)
Leon Clarkee46be812010-01-19 14:06:41 +000064 * - start index (character index of start)
65 * - String* input_string (input string)
Steve Blocka7e24c12009-10-30 11:49:00 +000066 * - return address
67 * - backup of callee save registers (rbx, possibly rsi and rdi).
Ben Murdochb8a8cc12014-11-26 15:28:44 +000068 * - success counter (only useful for global regexp to count matches)
Steve Blocka7e24c12009-10-30 11:49:00 +000069 * - Offset of location before start of input (effectively character
Ben Murdochb8a8cc12014-11-26 15:28:44 +000070 * position -1). Used to initialize capture registers to a non-position.
Leon Clarked91b9f72010-01-27 17:25:45 +000071 * - At start of string (if 1, we are starting at the start of the
72 * string, otherwise 0)
Steve Blocka7e24c12009-10-30 11:49:00 +000073 * - register 0 rbp[-n] (Only positions must be stored in the first
74 * - register 1 rbp[-n-8] num_saved_registers_ registers)
75 * - ...
76 *
77 * The first num_saved_registers_ registers are initialized to point to
78 * "character -1" in the string (i.e., char_size() bytes before the first
Ben Murdochb8a8cc12014-11-26 15:28:44 +000079 * character of the string). The remaining registers starts out uninitialized.
Steve Blocka7e24c12009-10-30 11:49:00 +000080 *
81 * The first seven values must be provided by the calling code by
82 * calling the code's entry address cast to a function pointer with the
83 * following signature:
84 * int (*match)(String* input_string,
Leon Clarkee46be812010-01-19 14:06:41 +000085 * int start_index,
Steve Blocka7e24c12009-10-30 11:49:00 +000086 * Address start,
87 * Address end,
88 * int* capture_output_array,
89 * bool at_start,
Leon Clarkee46be812010-01-19 14:06:41 +000090 * byte* stack_area_base,
91 * bool direct_call)
Steve Blocka7e24c12009-10-30 11:49:00 +000092 */
93
Steve Block44f0eee2011-05-26 01:26:41 +010094#define __ ACCESS_MASM((&masm_))
Steve Blocka7e24c12009-10-30 11:49:00 +000095
96RegExpMacroAssemblerX64::RegExpMacroAssemblerX64(
97 Mode mode,
Ben Murdochb8a8cc12014-11-26 15:28:44 +000098 int registers_to_save,
99 Zone* zone)
100 : NativeRegExpMacroAssembler(zone),
101 masm_(zone->isolate(), NULL, kRegExpCodeSize),
Steve Block44f0eee2011-05-26 01:26:41 +0100102 no_root_array_scope_(&masm_),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000103 code_relative_fixup_positions_(4, zone),
Steve Blocka7e24c12009-10-30 11:49:00 +0000104 mode_(mode),
105 num_registers_(registers_to_save),
106 num_saved_registers_(registers_to_save),
107 entry_label_(),
108 start_label_(),
109 success_label_(),
110 backtrack_label_(),
111 exit_label_() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000112 DCHECK_EQ(0, registers_to_save % 2);
Steve Blocka7e24c12009-10-30 11:49:00 +0000113 __ jmp(&entry_label_); // We'll write the entry code when we know more.
114 __ bind(&start_label_); // And then continue from here.
115}
116
117
118RegExpMacroAssemblerX64::~RegExpMacroAssemblerX64() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000119 // 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 RegExpMacroAssemblerX64::stack_limit_slack() {
131 return RegExpStack::kStackLimitSlack;
132}
133
134
135void RegExpMacroAssemblerX64::AdvanceCurrentPosition(int by) {
136 if (by != 0) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000137 __ addq(rdi, Immediate(by * char_size()));
138 }
139}
140
141
142void RegExpMacroAssemblerX64::AdvanceRegister(int reg, int by) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000143 DCHECK(reg >= 0);
144 DCHECK(reg < num_registers_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000145 if (by != 0) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000146 __ addp(register_location(reg), Immediate(by));
Steve Blocka7e24c12009-10-30 11:49:00 +0000147 }
148}
149
150
151void RegExpMacroAssemblerX64::Backtrack() {
152 CheckPreemption();
153 // Pop Code* offset from backtrack stack, add Code* and jump to location.
154 Pop(rbx);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000155 __ addp(rbx, code_object_pointer());
Steve Blocka7e24c12009-10-30 11:49:00 +0000156 __ jmp(rbx);
157}
158
159
160void RegExpMacroAssemblerX64::Bind(Label* label) {
161 __ bind(label);
162}
163
164
165void RegExpMacroAssemblerX64::CheckCharacter(uint32_t c, Label* on_equal) {
166 __ cmpl(current_character(), Immediate(c));
167 BranchOrBacktrack(equal, on_equal);
168}
169
170
171void RegExpMacroAssemblerX64::CheckCharacterGT(uc16 limit, Label* on_greater) {
172 __ cmpl(current_character(), Immediate(limit));
173 BranchOrBacktrack(greater, on_greater);
174}
175
176
177void RegExpMacroAssemblerX64::CheckAtStart(Label* on_at_start) {
178 Label not_at_start;
179 // Did we start the match at the start of the string at all?
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100180 __ cmpl(Operand(rbp, kStartIndex), Immediate(0));
Kristian Monsen25f61362010-05-21 11:50:48 +0100181 BranchOrBacktrack(not_equal, &not_at_start);
Steve Blocka7e24c12009-10-30 11:49:00 +0000182 // If we did, are we still at the start of the input?
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000183 __ leap(rax, Operand(rsi, rdi, times_1, 0));
184 __ cmpp(rax, Operand(rbp, kInputStart));
Steve Blocka7e24c12009-10-30 11:49:00 +0000185 BranchOrBacktrack(equal, on_at_start);
186 __ bind(&not_at_start);
187}
188
189
190void RegExpMacroAssemblerX64::CheckNotAtStart(Label* on_not_at_start) {
191 // Did we start the match at the start of the string at all?
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100192 __ cmpl(Operand(rbp, kStartIndex), Immediate(0));
Kristian Monsen25f61362010-05-21 11:50:48 +0100193 BranchOrBacktrack(not_equal, on_not_at_start);
Steve Blocka7e24c12009-10-30 11:49:00 +0000194 // If we did, are we still at the start of the input?
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000195 __ leap(rax, Operand(rsi, rdi, times_1, 0));
196 __ cmpp(rax, Operand(rbp, kInputStart));
Steve Blocka7e24c12009-10-30 11:49:00 +0000197 BranchOrBacktrack(not_equal, on_not_at_start);
198}
199
200
201void RegExpMacroAssemblerX64::CheckCharacterLT(uc16 limit, Label* on_less) {
202 __ cmpl(current_character(), Immediate(limit));
203 BranchOrBacktrack(less, on_less);
204}
205
206
Steve Blocka7e24c12009-10-30 11:49:00 +0000207void RegExpMacroAssemblerX64::CheckGreedyLoop(Label* on_equal) {
208 Label fallthrough;
209 __ cmpl(rdi, Operand(backtrack_stackpointer(), 0));
210 __ j(not_equal, &fallthrough);
211 Drop();
212 BranchOrBacktrack(no_condition, on_equal);
213 __ bind(&fallthrough);
214}
215
216
217void RegExpMacroAssemblerX64::CheckNotBackReferenceIgnoreCase(
218 int start_reg,
219 Label* on_no_match) {
220 Label fallthrough;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000221 ReadPositionFromRegister(rdx, start_reg); // Offset of start of capture
222 ReadPositionFromRegister(rbx, start_reg + 1); // Offset of end of capture
223 __ subp(rbx, rdx); // Length of capture.
Steve Blocka7e24c12009-10-30 11:49:00 +0000224
225 // -----------------------
226 // rdx = Start offset of capture.
227 // rbx = Length of capture
228
229 // If length is negative, this code will fail (it's a symptom of a partial or
230 // illegal capture where start of capture after end of capture).
231 // This must not happen (no back-reference can reference a capture that wasn't
232 // closed before in the reg-exp, and we must not generate code that can cause
233 // this condition).
234
235 // If length is zero, either the capture is empty or it is nonparticipating.
236 // In either case succeed immediately.
237 __ j(equal, &fallthrough);
238
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000239 // -----------------------
240 // rdx - Start of capture
241 // rbx - length of capture
242 // Check that there are sufficient characters left in the input.
243 __ movl(rax, rdi);
244 __ addl(rax, rbx);
245 BranchOrBacktrack(greater, on_no_match);
246
247 if (mode_ == LATIN1) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000248 Label loop_increment;
249 if (on_no_match == NULL) {
250 on_no_match = &backtrack_label_;
251 }
252
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000253 __ leap(r9, Operand(rsi, rdx, times_1, 0));
254 __ leap(r11, Operand(rsi, rdi, times_1, 0));
255 __ addp(rbx, r9); // End of capture
Steve Blocka7e24c12009-10-30 11:49:00 +0000256 // ---------------------
257 // r11 - current input character address
258 // r9 - current capture character address
259 // rbx - end of capture
260
261 Label loop;
262 __ bind(&loop);
263 __ movzxbl(rdx, Operand(r9, 0));
264 __ movzxbl(rax, Operand(r11, 0));
265 // al - input character
266 // dl - capture character
267 __ cmpb(rax, rdx);
268 __ j(equal, &loop_increment);
269
270 // Mismatch, try case-insensitive match (converting letters to lower-case).
271 // I.e., if or-ing with 0x20 makes values equal and in range 'a'-'z', it's
272 // a match.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000273 __ orp(rax, Immediate(0x20)); // Convert match character to lower-case.
274 __ orp(rdx, Immediate(0x20)); // Convert capture character to lower-case.
Steve Blocka7e24c12009-10-30 11:49:00 +0000275 __ cmpb(rax, rdx);
276 __ j(not_equal, on_no_match); // Definitely not equal.
277 __ subb(rax, Immediate('a'));
278 __ cmpb(rax, Immediate('z' - 'a'));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000279 __ j(below_equal, &loop_increment); // In range 'a'-'z'.
280 // Latin-1: Check for values in range [224,254] but not 247.
281 __ subb(rax, Immediate(224 - 'a'));
282 __ cmpb(rax, Immediate(254 - 224));
283 __ j(above, on_no_match); // Weren't Latin-1 letters.
284 __ cmpb(rax, Immediate(247 - 224)); // Check for 247.
285 __ j(equal, on_no_match);
Steve Blocka7e24c12009-10-30 11:49:00 +0000286 __ bind(&loop_increment);
287 // Increment pointers into match and capture strings.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000288 __ addp(r11, Immediate(1));
289 __ addp(r9, Immediate(1));
Steve Blocka7e24c12009-10-30 11:49:00 +0000290 // Compare to end of capture, and loop if not done.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000291 __ cmpp(r9, rbx);
Steve Blocka7e24c12009-10-30 11:49:00 +0000292 __ j(below, &loop);
293
294 // Compute new value of character position after the matched part.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000295 __ movp(rdi, r11);
Steve Blocka7e24c12009-10-30 11:49:00 +0000296 __ subq(rdi, rsi);
297 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000298 DCHECK(mode_ == UC16);
Steve Blocka7e24c12009-10-30 11:49:00 +0000299 // Save important/volatile registers before calling C function.
300#ifndef _WIN64
Leon Clarke4515c472010-02-03 11:58:03 +0000301 // Caller save on Linux and callee save in Windows.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000302 __ pushq(rsi);
303 __ pushq(rdi);
Steve Blocka7e24c12009-10-30 11:49:00 +0000304#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000305 __ pushq(backtrack_stackpointer());
Steve Blocka7e24c12009-10-30 11:49:00 +0000306
Ben Murdoch8b112d22011-06-08 16:22:53 +0100307 static const int num_arguments = 4;
Leon Clarke4515c472010-02-03 11:58:03 +0000308 __ PrepareCallCFunction(num_arguments);
Steve Blocka7e24c12009-10-30 11:49:00 +0000309
310 // Put arguments into parameter registers. Parameters are
311 // Address byte_offset1 - Address captured substring's start.
312 // Address byte_offset2 - Address of current character position.
313 // size_t byte_length - length of capture in bytes(!)
Ben Murdoch8b112d22011-06-08 16:22:53 +0100314 // Isolate* isolate
Steve Blocka7e24c12009-10-30 11:49:00 +0000315#ifdef _WIN64
316 // Compute and set byte_offset1 (start of capture).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000317 __ leap(rcx, Operand(rsi, rdx, times_1, 0));
Steve Blocka7e24c12009-10-30 11:49:00 +0000318 // Set byte_offset2.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000319 __ leap(rdx, Operand(rsi, rdi, times_1, 0));
Steve Blocka7e24c12009-10-30 11:49:00 +0000320 // Set byte_length.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000321 __ movp(r8, rbx);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100322 // Isolate.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000323 __ LoadAddress(r9, ExternalReference::isolate_address(isolate()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000324#else // AMD64 calling convention
325 // Compute byte_offset2 (current position = rsi+rdi).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000326 __ leap(rax, Operand(rsi, rdi, times_1, 0));
Steve Blocka7e24c12009-10-30 11:49:00 +0000327 // Compute and set byte_offset1 (start of capture).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000328 __ leap(rdi, Operand(rsi, rdx, times_1, 0));
Steve Blocka7e24c12009-10-30 11:49:00 +0000329 // Set byte_offset2.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000330 __ movp(rsi, rax);
Steve Blocka7e24c12009-10-30 11:49:00 +0000331 // Set byte_length.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000332 __ movp(rdx, rbx);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100333 // Isolate.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000334 __ LoadAddress(rcx, ExternalReference::isolate_address(isolate()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000335#endif
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100336
337 { // NOLINT: Can't find a way to open this scope without confusing the
338 // linter.
339 AllowExternalCallThatCantCauseGC scope(&masm_);
340 ExternalReference compare =
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000341 ExternalReference::re_case_insensitive_compare_uc16(isolate());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100342 __ CallCFunction(compare, num_arguments);
343 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000344
345 // Restore original values before reacting on result value.
Steve Block44f0eee2011-05-26 01:26:41 +0100346 __ Move(code_object_pointer(), masm_.CodeObject());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000347 __ popq(backtrack_stackpointer());
Steve Blocka7e24c12009-10-30 11:49:00 +0000348#ifndef _WIN64
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000349 __ popq(rdi);
350 __ popq(rsi);
Steve Blocka7e24c12009-10-30 11:49:00 +0000351#endif
352
353 // Check if function returned non-zero for success or zero for failure.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000354 __ testp(rax, rax);
Steve Blocka7e24c12009-10-30 11:49:00 +0000355 BranchOrBacktrack(zero, on_no_match);
356 // On success, increment position by length of capture.
357 // Requires that rbx is callee save (true for both Win64 and AMD64 ABIs).
358 __ addq(rdi, rbx);
359 }
360 __ bind(&fallthrough);
361}
362
363
364void RegExpMacroAssemblerX64::CheckNotBackReference(
365 int start_reg,
366 Label* on_no_match) {
367 Label fallthrough;
368
369 // Find length of back-referenced capture.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000370 ReadPositionFromRegister(rdx, start_reg); // Offset of start of capture
371 ReadPositionFromRegister(rax, start_reg + 1); // Offset of end of capture
372 __ subp(rax, rdx); // Length to check.
Steve Blocka7e24c12009-10-30 11:49:00 +0000373
374 // Fail on partial or illegal capture (start of capture after end of capture).
375 // This must not happen (no back-reference can reference a capture that wasn't
376 // closed before in the reg-exp).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000377 __ Check(greater_equal, kInvalidCaptureReferenced);
Steve Blocka7e24c12009-10-30 11:49:00 +0000378
379 // Succeed on empty capture (including non-participating capture)
380 __ j(equal, &fallthrough);
381
382 // -----------------------
383 // rdx - Start of capture
384 // rax - length of capture
385
386 // Check that there are sufficient characters left in the input.
387 __ movl(rbx, rdi);
388 __ addl(rbx, rax);
389 BranchOrBacktrack(greater, on_no_match);
390
391 // Compute pointers to match string and capture string
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000392 __ leap(rbx, Operand(rsi, rdi, times_1, 0)); // Start of match.
393 __ addp(rdx, rsi); // Start of capture.
394 __ leap(r9, Operand(rdx, rax, times_1, 0)); // End of capture
Steve Blocka7e24c12009-10-30 11:49:00 +0000395
396 // -----------------------
397 // rbx - current capture character address.
398 // rbx - current input character address .
399 // r9 - end of input to match (capture length after rbx).
400
401 Label loop;
402 __ bind(&loop);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000403 if (mode_ == LATIN1) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000404 __ movzxbl(rax, Operand(rdx, 0));
405 __ cmpb(rax, Operand(rbx, 0));
406 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000407 DCHECK(mode_ == UC16);
Steve Blocka7e24c12009-10-30 11:49:00 +0000408 __ movzxwl(rax, Operand(rdx, 0));
409 __ cmpw(rax, Operand(rbx, 0));
410 }
411 BranchOrBacktrack(not_equal, on_no_match);
412 // Increment pointers into capture and match string.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000413 __ addp(rbx, Immediate(char_size()));
414 __ addp(rdx, Immediate(char_size()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000415 // Check if we have reached end of match area.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000416 __ cmpp(rdx, r9);
Steve Blocka7e24c12009-10-30 11:49:00 +0000417 __ j(below, &loop);
418
419 // Success.
420 // Set current character position to position after match.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000421 __ movp(rdi, rbx);
Steve Blocka7e24c12009-10-30 11:49:00 +0000422 __ subq(rdi, rsi);
423
424 __ bind(&fallthrough);
425}
426
427
Steve Blocka7e24c12009-10-30 11:49:00 +0000428void RegExpMacroAssemblerX64::CheckNotCharacter(uint32_t c,
429 Label* on_not_equal) {
430 __ cmpl(current_character(), Immediate(c));
431 BranchOrBacktrack(not_equal, on_not_equal);
432}
433
434
435void RegExpMacroAssemblerX64::CheckCharacterAfterAnd(uint32_t c,
436 uint32_t mask,
437 Label* on_equal) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000438 if (c == 0) {
439 __ testl(current_character(), Immediate(mask));
440 } else {
441 __ movl(rax, Immediate(mask));
442 __ andp(rax, current_character());
443 __ cmpl(rax, Immediate(c));
444 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000445 BranchOrBacktrack(equal, on_equal);
446}
447
448
449void RegExpMacroAssemblerX64::CheckNotCharacterAfterAnd(uint32_t c,
450 uint32_t mask,
451 Label* on_not_equal) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000452 if (c == 0) {
453 __ testl(current_character(), Immediate(mask));
454 } else {
455 __ movl(rax, Immediate(mask));
456 __ andp(rax, current_character());
457 __ cmpl(rax, Immediate(c));
458 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000459 BranchOrBacktrack(not_equal, on_not_equal);
460}
461
462
463void RegExpMacroAssemblerX64::CheckNotCharacterAfterMinusAnd(
464 uc16 c,
465 uc16 minus,
466 uc16 mask,
467 Label* on_not_equal) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000468 DCHECK(minus < String::kMaxUtf16CodeUnit);
469 __ leap(rax, Operand(current_character(), -minus));
470 __ andp(rax, Immediate(mask));
Steve Blocka7e24c12009-10-30 11:49:00 +0000471 __ cmpl(rax, Immediate(c));
472 BranchOrBacktrack(not_equal, on_not_equal);
473}
474
475
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000476void RegExpMacroAssemblerX64::CheckCharacterInRange(
477 uc16 from,
478 uc16 to,
479 Label* on_in_range) {
480 __ leal(rax, Operand(current_character(), -from));
481 __ cmpl(rax, Immediate(to - from));
482 BranchOrBacktrack(below_equal, on_in_range);
483}
484
485
486void RegExpMacroAssemblerX64::CheckCharacterNotInRange(
487 uc16 from,
488 uc16 to,
489 Label* on_not_in_range) {
490 __ leal(rax, Operand(current_character(), -from));
491 __ cmpl(rax, Immediate(to - from));
492 BranchOrBacktrack(above, on_not_in_range);
493}
494
495
496void RegExpMacroAssemblerX64::CheckBitInTable(
497 Handle<ByteArray> table,
498 Label* on_bit_set) {
499 __ Move(rax, table);
500 Register index = current_character();
501 if (mode_ != LATIN1 || kTableMask != String::kMaxOneByteCharCode) {
502 __ movp(rbx, current_character());
503 __ andp(rbx, Immediate(kTableMask));
504 index = rbx;
505 }
506 __ cmpb(FieldOperand(rax, index, times_1, ByteArray::kHeaderSize),
507 Immediate(0));
508 BranchOrBacktrack(not_equal, on_bit_set);
509}
510
511
Steve Blocka7e24c12009-10-30 11:49:00 +0000512bool RegExpMacroAssemblerX64::CheckSpecialCharacterClass(uc16 type,
Steve Blocka7e24c12009-10-30 11:49:00 +0000513 Label* on_no_match) {
514 // Range checks (c in min..max) are generally implemented by an unsigned
Leon Clarkee46be812010-01-19 14:06:41 +0000515 // (c - min) <= (max - min) check, using the sequence:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000516 // leap(rax, Operand(current_character(), -min)) or sub(rax, Immediate(min))
Leon Clarkee46be812010-01-19 14:06:41 +0000517 // cmp(rax, Immediate(max - min))
Steve Blocka7e24c12009-10-30 11:49:00 +0000518 switch (type) {
519 case 's':
520 // Match space-characters
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000521 if (mode_ == LATIN1) {
522 // One byte space characters are '\t'..'\r', ' ' and \u00a0.
Steve Blocka7e24c12009-10-30 11:49:00 +0000523 Label success;
524 __ cmpl(current_character(), Immediate(' '));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000525 __ j(equal, &success, Label::kNear);
Steve Blocka7e24c12009-10-30 11:49:00 +0000526 // Check range 0x09..0x0d
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000527 __ leap(rax, Operand(current_character(), -'\t'));
Leon Clarkee46be812010-01-19 14:06:41 +0000528 __ cmpl(rax, Immediate('\r' - '\t'));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000529 __ j(below_equal, &success, Label::kNear);
530 // \u00a0 (NBSP).
531 __ cmpl(rax, Immediate(0x00a0 - '\t'));
532 BranchOrBacktrack(not_equal, on_no_match);
Steve Blocka7e24c12009-10-30 11:49:00 +0000533 __ bind(&success);
534 return true;
535 }
536 return false;
537 case 'S':
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000538 // The emitted code for generic character classes is good enough.
Steve Blocka7e24c12009-10-30 11:49:00 +0000539 return false;
540 case 'd':
541 // Match ASCII digits ('0'..'9')
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000542 __ leap(rax, Operand(current_character(), -'0'));
Leon Clarkee46be812010-01-19 14:06:41 +0000543 __ cmpl(rax, Immediate('9' - '0'));
Steve Blocka7e24c12009-10-30 11:49:00 +0000544 BranchOrBacktrack(above, on_no_match);
545 return true;
546 case 'D':
547 // Match non ASCII-digits
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000548 __ leap(rax, Operand(current_character(), -'0'));
Leon Clarkee46be812010-01-19 14:06:41 +0000549 __ cmpl(rax, Immediate('9' - '0'));
Steve Blocka7e24c12009-10-30 11:49:00 +0000550 BranchOrBacktrack(below_equal, on_no_match);
551 return true;
552 case '.': {
553 // Match non-newlines (not 0x0a('\n'), 0x0d('\r'), 0x2028 and 0x2029)
Leon Clarkee46be812010-01-19 14:06:41 +0000554 __ movl(rax, current_character());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000555 __ xorp(rax, Immediate(0x01));
Steve Blocka7e24c12009-10-30 11:49:00 +0000556 // See if current character is '\n'^1 or '\r'^1, i.e., 0x0b or 0x0c
Leon Clarkee46be812010-01-19 14:06:41 +0000557 __ subl(rax, Immediate(0x0b));
558 __ cmpl(rax, Immediate(0x0c - 0x0b));
Steve Blocka7e24c12009-10-30 11:49:00 +0000559 BranchOrBacktrack(below_equal, on_no_match);
560 if (mode_ == UC16) {
561 // Compare original value to 0x2028 and 0x2029, using the already
562 // computed (current_char ^ 0x01 - 0x0b). I.e., check for
563 // 0x201d (0x2028 - 0x0b) or 0x201e.
Leon Clarkee46be812010-01-19 14:06:41 +0000564 __ subl(rax, Immediate(0x2028 - 0x0b));
565 __ cmpl(rax, Immediate(0x2029 - 0x2028));
Steve Blocka7e24c12009-10-30 11:49:00 +0000566 BranchOrBacktrack(below_equal, on_no_match);
567 }
568 return true;
569 }
Leon Clarkee46be812010-01-19 14:06:41 +0000570 case 'n': {
571 // Match newlines (0x0a('\n'), 0x0d('\r'), 0x2028 and 0x2029)
572 __ movl(rax, current_character());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000573 __ xorp(rax, Immediate(0x01));
Leon Clarkee46be812010-01-19 14:06:41 +0000574 // See if current character is '\n'^1 or '\r'^1, i.e., 0x0b or 0x0c
575 __ subl(rax, Immediate(0x0b));
576 __ cmpl(rax, Immediate(0x0c - 0x0b));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000577 if (mode_ == LATIN1) {
Leon Clarkee46be812010-01-19 14:06:41 +0000578 BranchOrBacktrack(above, on_no_match);
579 } else {
580 Label done;
581 BranchOrBacktrack(below_equal, &done);
582 // Compare original value to 0x2028 and 0x2029, using the already
583 // computed (current_char ^ 0x01 - 0x0b). I.e., check for
584 // 0x201d (0x2028 - 0x0b) or 0x201e.
585 __ subl(rax, Immediate(0x2028 - 0x0b));
586 __ cmpl(rax, Immediate(0x2029 - 0x2028));
587 BranchOrBacktrack(above, on_no_match);
588 __ bind(&done);
Steve Blocka7e24c12009-10-30 11:49:00 +0000589 }
590 return true;
Leon Clarkee46be812010-01-19 14:06:41 +0000591 }
592 case 'w': {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000593 if (mode_ != LATIN1) {
594 // Table is 256 entries, so all Latin1 characters can be tested.
Leon Clarkee46be812010-01-19 14:06:41 +0000595 __ cmpl(current_character(), Immediate('z'));
596 BranchOrBacktrack(above, on_no_match);
597 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000598 __ Move(rbx, ExternalReference::re_word_character_map());
599 DCHECK_EQ(0, word_character_map[0]); // Character '\0' is not a word char.
Leon Clarkee46be812010-01-19 14:06:41 +0000600 __ testb(Operand(rbx, current_character(), times_1, 0),
601 current_character());
602 BranchOrBacktrack(zero, on_no_match);
603 return true;
604 }
605 case 'W': {
606 Label done;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000607 if (mode_ != LATIN1) {
608 // Table is 256 entries, so all Latin1 characters can be tested.
Leon Clarkee46be812010-01-19 14:06:41 +0000609 __ cmpl(current_character(), Immediate('z'));
610 __ j(above, &done);
611 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000612 __ Move(rbx, ExternalReference::re_word_character_map());
613 DCHECK_EQ(0, word_character_map[0]); // Character '\0' is not a word char.
Leon Clarkee46be812010-01-19 14:06:41 +0000614 __ testb(Operand(rbx, current_character(), times_1, 0),
615 current_character());
616 BranchOrBacktrack(not_zero, on_no_match);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000617 if (mode_ != LATIN1) {
Leon Clarkee46be812010-01-19 14:06:41 +0000618 __ bind(&done);
619 }
620 return true;
621 }
622
623 case '*':
624 // Match any character.
625 return true;
626 // No custom implementation (yet): s(UC16), S(UC16).
Steve Blocka7e24c12009-10-30 11:49:00 +0000627 default:
628 return false;
629 }
630}
631
632
633void RegExpMacroAssemblerX64::Fail() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000634 STATIC_ASSERT(FAILURE == 0); // Return value for failure is zero.
635 if (!global()) {
636 __ Set(rax, FAILURE);
637 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000638 __ jmp(&exit_label_);
639}
640
641
Steve Block053d10c2011-06-13 19:13:29 +0100642Handle<HeapObject> RegExpMacroAssemblerX64::GetCode(Handle<String> source) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000643 Label return_rax;
Steve Blocka7e24c12009-10-30 11:49:00 +0000644 // Finalize code - write the entry point code now we know how many
645 // registers we need.
Steve Blocka7e24c12009-10-30 11:49:00 +0000646 // Entry code:
647 __ bind(&entry_label_);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100648
649 // Tell the system that we have a stack frame. Because the type is MANUAL, no
650 // is generated.
651 FrameScope scope(&masm_, StackFrame::MANUAL);
652
653 // Actually emit code to start a new stack frame.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000654 __ pushq(rbp);
655 __ movp(rbp, rsp);
Steve Blocka7e24c12009-10-30 11:49:00 +0000656 // Save parameters and callee-save registers. Order here should correspond
657 // to order of kBackup_ebx etc.
658#ifdef _WIN64
659 // MSVC passes arguments in rcx, rdx, r8, r9, with backing stack slots.
660 // Store register parameters in pre-allocated stack slots,
661 __ movq(Operand(rbp, kInputString), rcx);
662 __ movq(Operand(rbp, kStartIndex), rdx); // Passed as int32 in edx.
663 __ movq(Operand(rbp, kInputStart), r8);
664 __ movq(Operand(rbp, kInputEnd), r9);
665 // Callee-save on Win64.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000666 __ pushq(rsi);
667 __ pushq(rdi);
668 __ pushq(rbx);
Steve Blocka7e24c12009-10-30 11:49:00 +0000669#else
670 // GCC passes arguments in rdi, rsi, rdx, rcx, r8, r9 (and then on stack).
671 // Push register parameters on stack for reference.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000672 DCHECK_EQ(kInputString, -1 * kRegisterSize);
673 DCHECK_EQ(kStartIndex, -2 * kRegisterSize);
674 DCHECK_EQ(kInputStart, -3 * kRegisterSize);
675 DCHECK_EQ(kInputEnd, -4 * kRegisterSize);
676 DCHECK_EQ(kRegisterOutput, -5 * kRegisterSize);
677 DCHECK_EQ(kNumOutputRegisters, -6 * kRegisterSize);
678 __ pushq(rdi);
679 __ pushq(rsi);
680 __ pushq(rdx);
681 __ pushq(rcx);
682 __ pushq(r8);
683 __ pushq(r9);
Steve Blocka7e24c12009-10-30 11:49:00 +0000684
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000685 __ pushq(rbx); // Callee-save
Steve Blocka7e24c12009-10-30 11:49:00 +0000686#endif
Leon Clarke4515c472010-02-03 11:58:03 +0000687
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000688 __ Push(Immediate(0)); // Number of successful matches in a global regexp.
689 __ Push(Immediate(0)); // Make room for "input start - 1" constant.
Steve Blocka7e24c12009-10-30 11:49:00 +0000690
691 // Check if we have space on the stack for registers.
692 Label stack_limit_hit;
693 Label stack_ok;
694
Steve Blockd0582a62009-12-15 09:54:21 +0000695 ExternalReference stack_limit =
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000696 ExternalReference::address_of_stack_limit(isolate());
697 __ movp(rcx, rsp);
698 __ Move(kScratchRegister, stack_limit);
699 __ subp(rcx, Operand(kScratchRegister, 0));
Steve Blocka7e24c12009-10-30 11:49:00 +0000700 // Handle it if the stack pointer is already below the stack limit.
701 __ j(below_equal, &stack_limit_hit);
702 // Check if there is room for the variable number of registers above
703 // the stack limit.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000704 __ cmpp(rcx, Immediate(num_registers_ * kPointerSize));
Steve Blocka7e24c12009-10-30 11:49:00 +0000705 __ j(above_equal, &stack_ok);
706 // Exit with OutOfMemory exception. There is not enough space on the stack
707 // for our working registers.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100708 __ Set(rax, EXCEPTION);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000709 __ jmp(&return_rax);
Steve Blocka7e24c12009-10-30 11:49:00 +0000710
711 __ bind(&stack_limit_hit);
Steve Block44f0eee2011-05-26 01:26:41 +0100712 __ Move(code_object_pointer(), masm_.CodeObject());
Steve Blocka7e24c12009-10-30 11:49:00 +0000713 CallCheckStackGuardState(); // Preserves no registers beside rbp and rsp.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000714 __ testp(rax, rax);
Steve Blocka7e24c12009-10-30 11:49:00 +0000715 // If returned value is non-zero, we exit with the returned value as result.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000716 __ j(not_zero, &return_rax);
Steve Blocka7e24c12009-10-30 11:49:00 +0000717
718 __ bind(&stack_ok);
719
720 // Allocate space on stack for registers.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000721 __ subp(rsp, Immediate(num_registers_ * kPointerSize));
Steve Blocka7e24c12009-10-30 11:49:00 +0000722 // Load string length.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000723 __ movp(rsi, Operand(rbp, kInputEnd));
Steve Blocka7e24c12009-10-30 11:49:00 +0000724 // Load input position.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000725 __ movp(rdi, Operand(rbp, kInputStart));
Steve Blocka7e24c12009-10-30 11:49:00 +0000726 // Set up rdi to be negative offset from string end.
727 __ subq(rdi, rsi);
Steve Block6ded16b2010-05-10 14:33:55 +0100728 // Set rax to address of char before start of the string
Steve Blocka7e24c12009-10-30 11:49:00 +0000729 // (effectively string position -1).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000730 __ movp(rbx, Operand(rbp, kStartIndex));
731 __ negq(rbx);
Steve Block6ded16b2010-05-10 14:33:55 +0100732 if (mode_ == UC16) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000733 __ leap(rax, Operand(rdi, rbx, times_2, -char_size()));
Steve Block6ded16b2010-05-10 14:33:55 +0100734 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000735 __ leap(rax, Operand(rdi, rbx, times_1, -char_size()));
Steve Block6ded16b2010-05-10 14:33:55 +0100736 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000737 // Store this value in a local variable, for use when clearing
738 // position registers.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000739 __ movp(Operand(rbp, kInputStartMinusOne), rax);
Leon Clarked91b9f72010-01-27 17:25:45 +0000740
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000741#if V8_OS_WIN
Steve Blocka7e24c12009-10-30 11:49:00 +0000742 // Ensure that we have written to each stack page, in order. Skipping a page
743 // on Windows can cause segmentation faults. Assuming page size is 4k.
744 const int kPageSize = 4096;
745 const int kRegistersPerPage = kPageSize / kPointerSize;
746 for (int i = num_saved_registers_ + kRegistersPerPage - 1;
747 i < num_registers_;
748 i += kRegistersPerPage) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000749 __ movp(register_location(i), rax); // One write every page.
750 }
751#endif // V8_OS_WIN
752
753 // Initialize code object pointer.
754 __ Move(code_object_pointer(), masm_.CodeObject());
755
756 Label load_char_start_regexp, start_regexp;
757 // Load newline if index is at start, previous character otherwise.
758 __ cmpl(Operand(rbp, kStartIndex), Immediate(0));
759 __ j(not_equal, &load_char_start_regexp, Label::kNear);
760 __ Set(current_character(), '\n');
761 __ jmp(&start_regexp, Label::kNear);
762
763 // Global regexp restarts matching here.
764 __ bind(&load_char_start_regexp);
765 // Load previous char as initial value of current character register.
766 LoadCurrentCharacterUnchecked(-1, 1);
767 __ bind(&start_regexp);
768
769 // Initialize on-stack registers.
770 if (num_saved_registers_ > 0) {
771 // Fill saved registers with initial value = start offset - 1
772 // Fill in stack push order, to avoid accessing across an unwritten
773 // page (a problem on Windows).
774 if (num_saved_registers_ > 8) {
775 __ Set(rcx, kRegisterZero);
776 Label init_loop;
777 __ bind(&init_loop);
778 __ movp(Operand(rbp, rcx, times_1, 0), rax);
779 __ subq(rcx, Immediate(kPointerSize));
780 __ cmpq(rcx,
781 Immediate(kRegisterZero - num_saved_registers_ * kPointerSize));
782 __ j(greater, &init_loop);
783 } else { // Unroll the loop.
784 for (int i = 0; i < num_saved_registers_; i++) {
785 __ movp(register_location(i), rax);
786 }
787 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000788 }
789
790 // Initialize backtrack stack pointer.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000791 __ movp(backtrack_stackpointer(), Operand(rbp, kStackHighEnd));
Steve Blocka7e24c12009-10-30 11:49:00 +0000792
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000793 __ jmp(&start_label_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000794
795 // Exit code:
796 if (success_label_.is_linked()) {
797 // Save captures when successful.
798 __ bind(&success_label_);
799 if (num_saved_registers_ > 0) {
800 // copy captures to output
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000801 __ movp(rdx, Operand(rbp, kStartIndex));
802 __ movp(rbx, Operand(rbp, kRegisterOutput));
803 __ movp(rcx, Operand(rbp, kInputEnd));
804 __ subp(rcx, Operand(rbp, kInputStart));
Steve Block6ded16b2010-05-10 14:33:55 +0100805 if (mode_ == UC16) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000806 __ leap(rcx, Operand(rcx, rdx, times_2, 0));
Steve Block6ded16b2010-05-10 14:33:55 +0100807 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000808 __ addp(rcx, rdx);
Steve Block6ded16b2010-05-10 14:33:55 +0100809 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000810 for (int i = 0; i < num_saved_registers_; i++) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000811 __ movp(rax, register_location(i));
812 if (i == 0 && global_with_zero_length_check()) {
813 // Keep capture start in rdx for the zero-length check later.
814 __ movp(rdx, rax);
815 }
816 __ addp(rax, rcx); // Convert to index from start, not end.
Steve Blocka7e24c12009-10-30 11:49:00 +0000817 if (mode_ == UC16) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000818 __ sarp(rax, Immediate(1)); // Convert byte index to character index.
Steve Blocka7e24c12009-10-30 11:49:00 +0000819 }
820 __ movl(Operand(rbx, i * kIntSize), rax);
821 }
822 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000823
824 if (global()) {
825 // Restart matching if the regular expression is flagged as global.
826 // Increment success counter.
827 __ incp(Operand(rbp, kSuccessfulCaptures));
828 // Capture results have been stored, so the number of remaining global
829 // output registers is reduced by the number of stored captures.
830 __ movsxlq(rcx, Operand(rbp, kNumOutputRegisters));
831 __ subp(rcx, Immediate(num_saved_registers_));
832 // Check whether we have enough room for another set of capture results.
833 __ cmpp(rcx, Immediate(num_saved_registers_));
834 __ j(less, &exit_label_);
835
836 __ movp(Operand(rbp, kNumOutputRegisters), rcx);
837 // Advance the location for output.
838 __ addp(Operand(rbp, kRegisterOutput),
839 Immediate(num_saved_registers_ * kIntSize));
840
841 // Prepare rax to initialize registers with its value in the next run.
842 __ movp(rax, Operand(rbp, kInputStartMinusOne));
843
844 if (global_with_zero_length_check()) {
845 // Special case for zero-length matches.
846 // rdx: capture start index
847 __ cmpp(rdi, rdx);
848 // Not a zero-length match, restart.
849 __ j(not_equal, &load_char_start_regexp);
850 // rdi (offset from the end) is zero if we already reached the end.
851 __ testp(rdi, rdi);
852 __ j(zero, &exit_label_, Label::kNear);
853 // Advance current position after a zero-length match.
854 if (mode_ == UC16) {
855 __ addq(rdi, Immediate(2));
856 } else {
857 __ incq(rdi);
858 }
859 }
860
861 __ jmp(&load_char_start_regexp);
862 } else {
863 __ movp(rax, Immediate(SUCCESS));
864 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000865 }
866
Steve Blocka7e24c12009-10-30 11:49:00 +0000867 __ bind(&exit_label_);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000868 if (global()) {
869 // Return the number of successful captures.
870 __ movp(rax, Operand(rbp, kSuccessfulCaptures));
871 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000872
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000873 __ bind(&return_rax);
Steve Blocka7e24c12009-10-30 11:49:00 +0000874#ifdef _WIN64
875 // Restore callee save registers.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000876 __ leap(rsp, Operand(rbp, kLastCalleeSaveRegister));
877 __ popq(rbx);
878 __ popq(rdi);
879 __ popq(rsi);
Steve Blocka7e24c12009-10-30 11:49:00 +0000880 // Stack now at rbp.
881#else
882 // Restore callee save register.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000883 __ movp(rbx, Operand(rbp, kBackup_rbx));
Steve Blocka7e24c12009-10-30 11:49:00 +0000884 // Skip rsp to rbp.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000885 __ movp(rsp, rbp);
Steve Blocka7e24c12009-10-30 11:49:00 +0000886#endif
887 // Exit function frame, restore previous one.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000888 __ popq(rbp);
Steve Blocka7e24c12009-10-30 11:49:00 +0000889 __ ret(0);
890
891 // Backtrack code (branch target for conditional backtracks).
892 if (backtrack_label_.is_linked()) {
893 __ bind(&backtrack_label_);
894 Backtrack();
895 }
896
897 Label exit_with_exception;
898
899 // Preempt-code
900 if (check_preempt_label_.is_linked()) {
901 SafeCallTarget(&check_preempt_label_);
902
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000903 __ pushq(backtrack_stackpointer());
904 __ pushq(rdi);
Steve Blocka7e24c12009-10-30 11:49:00 +0000905
906 CallCheckStackGuardState();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000907 __ testp(rax, rax);
Steve Blocka7e24c12009-10-30 11:49:00 +0000908 // If returning non-zero, we should end execution with the given
909 // result as return value.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000910 __ j(not_zero, &return_rax);
Steve Blocka7e24c12009-10-30 11:49:00 +0000911
912 // Restore registers.
Steve Block44f0eee2011-05-26 01:26:41 +0100913 __ Move(code_object_pointer(), masm_.CodeObject());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000914 __ popq(rdi);
915 __ popq(backtrack_stackpointer());
Steve Blocka7e24c12009-10-30 11:49:00 +0000916 // String might have moved: Reload esi from frame.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000917 __ movp(rsi, Operand(rbp, kInputEnd));
Steve Blocka7e24c12009-10-30 11:49:00 +0000918 SafeReturn();
919 }
920
921 // Backtrack stack overflow code.
922 if (stack_overflow_label_.is_linked()) {
923 SafeCallTarget(&stack_overflow_label_);
924 // Reached if the backtrack-stack limit has been hit.
925
926 Label grow_failed;
927 // Save registers before calling C function
928#ifndef _WIN64
929 // Callee-save in Microsoft 64-bit ABI, but not in AMD64 ABI.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000930 __ pushq(rsi);
931 __ pushq(rdi);
Steve Blocka7e24c12009-10-30 11:49:00 +0000932#endif
933
934 // Call GrowStack(backtrack_stackpointer())
Ben Murdoch8b112d22011-06-08 16:22:53 +0100935 static const int num_arguments = 3;
Leon Clarke4515c472010-02-03 11:58:03 +0000936 __ PrepareCallCFunction(num_arguments);
Steve Blocka7e24c12009-10-30 11:49:00 +0000937#ifdef _WIN64
Ben Murdoch8b112d22011-06-08 16:22:53 +0100938 // Microsoft passes parameters in rcx, rdx, r8.
Steve Blocka7e24c12009-10-30 11:49:00 +0000939 // First argument, backtrack stackpointer, is already in rcx.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000940 __ leap(rdx, Operand(rbp, kStackHighEnd)); // Second argument
941 __ LoadAddress(r8, ExternalReference::isolate_address(isolate()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000942#else
Ben Murdoch8b112d22011-06-08 16:22:53 +0100943 // AMD64 ABI passes parameters in rdi, rsi, rdx.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000944 __ movp(rdi, backtrack_stackpointer()); // First argument.
945 __ leap(rsi, Operand(rbp, kStackHighEnd)); // Second argument.
946 __ LoadAddress(rdx, ExternalReference::isolate_address(isolate()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000947#endif
Steve Block44f0eee2011-05-26 01:26:41 +0100948 ExternalReference grow_stack =
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000949 ExternalReference::re_grow_stack(isolate());
Leon Clarke4515c472010-02-03 11:58:03 +0000950 __ CallCFunction(grow_stack, num_arguments);
Steve Blocka7e24c12009-10-30 11:49:00 +0000951 // If return NULL, we have failed to grow the stack, and
952 // must exit with a stack-overflow exception.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000953 __ testp(rax, rax);
Steve Blocka7e24c12009-10-30 11:49:00 +0000954 __ j(equal, &exit_with_exception);
955 // Otherwise use return value as new stack pointer.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000956 __ movp(backtrack_stackpointer(), rax);
Steve Blocka7e24c12009-10-30 11:49:00 +0000957 // Restore saved registers and continue.
Steve Block44f0eee2011-05-26 01:26:41 +0100958 __ Move(code_object_pointer(), masm_.CodeObject());
Steve Blocka7e24c12009-10-30 11:49:00 +0000959#ifndef _WIN64
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000960 __ popq(rdi);
961 __ popq(rsi);
Steve Blocka7e24c12009-10-30 11:49:00 +0000962#endif
963 SafeReturn();
964 }
965
966 if (exit_with_exception.is_linked()) {
967 // If any of the code above needed to exit with an exception.
968 __ bind(&exit_with_exception);
969 // Exit with Result EXCEPTION(-1) to signal thrown exception.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100970 __ Set(rax, EXCEPTION);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000971 __ jmp(&return_rax);
Steve Blocka7e24c12009-10-30 11:49:00 +0000972 }
973
974 FixupCodeRelativePositions();
975
976 CodeDesc code_desc;
Steve Block44f0eee2011-05-26 01:26:41 +0100977 masm_.GetCode(&code_desc);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000978 Isolate* isolate = this->isolate();
Steve Block44f0eee2011-05-26 01:26:41 +0100979 Handle<Code> code = isolate->factory()->NewCode(
980 code_desc, Code::ComputeFlags(Code::REGEXP),
981 masm_.CodeObject());
982 PROFILE(isolate, RegExpCodeCreateEvent(*code, *source));
Steve Block053d10c2011-06-13 19:13:29 +0100983 return Handle<HeapObject>::cast(code);
Steve Blocka7e24c12009-10-30 11:49:00 +0000984}
985
986
987void RegExpMacroAssemblerX64::GoTo(Label* to) {
988 BranchOrBacktrack(no_condition, to);
989}
990
991
992void RegExpMacroAssemblerX64::IfRegisterGE(int reg,
993 int comparand,
994 Label* if_ge) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000995 __ cmpp(register_location(reg), Immediate(comparand));
Steve Blocka7e24c12009-10-30 11:49:00 +0000996 BranchOrBacktrack(greater_equal, if_ge);
997}
998
999
1000void RegExpMacroAssemblerX64::IfRegisterLT(int reg,
1001 int comparand,
1002 Label* if_lt) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001003 __ cmpp(register_location(reg), Immediate(comparand));
Steve Blocka7e24c12009-10-30 11:49:00 +00001004 BranchOrBacktrack(less, if_lt);
1005}
1006
1007
1008void RegExpMacroAssemblerX64::IfRegisterEqPos(int reg,
1009 Label* if_eq) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001010 __ cmpp(rdi, register_location(reg));
Steve Blocka7e24c12009-10-30 11:49:00 +00001011 BranchOrBacktrack(equal, if_eq);
1012}
1013
1014
1015RegExpMacroAssembler::IrregexpImplementation
1016 RegExpMacroAssemblerX64::Implementation() {
1017 return kX64Implementation;
1018}
1019
1020
1021void RegExpMacroAssemblerX64::LoadCurrentCharacter(int cp_offset,
1022 Label* on_end_of_input,
1023 bool check_bounds,
1024 int characters) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001025 DCHECK(cp_offset >= -1); // ^ and \b can look behind one character.
1026 DCHECK(cp_offset < (1<<30)); // Be sane! (And ensure negation works)
Steve Blocka7e24c12009-10-30 11:49:00 +00001027 if (check_bounds) {
1028 CheckPosition(cp_offset + characters - 1, on_end_of_input);
1029 }
1030 LoadCurrentCharacterUnchecked(cp_offset, characters);
1031}
1032
1033
1034void RegExpMacroAssemblerX64::PopCurrentPosition() {
1035 Pop(rdi);
1036}
1037
1038
1039void RegExpMacroAssemblerX64::PopRegister(int register_index) {
1040 Pop(rax);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001041 __ movp(register_location(register_index), rax);
Steve Blocka7e24c12009-10-30 11:49:00 +00001042}
1043
1044
1045void RegExpMacroAssemblerX64::PushBacktrack(Label* label) {
1046 Push(label);
1047 CheckStackLimit();
1048}
1049
1050
1051void RegExpMacroAssemblerX64::PushCurrentPosition() {
1052 Push(rdi);
1053}
1054
1055
1056void RegExpMacroAssemblerX64::PushRegister(int register_index,
1057 StackCheckFlag check_stack_limit) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001058 __ movp(rax, register_location(register_index));
Steve Blocka7e24c12009-10-30 11:49:00 +00001059 Push(rax);
1060 if (check_stack_limit) CheckStackLimit();
1061}
1062
1063
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001064STATIC_ASSERT(kPointerSize == kInt64Size || kPointerSize == kInt32Size);
1065
1066
Steve Blocka7e24c12009-10-30 11:49:00 +00001067void RegExpMacroAssemblerX64::ReadCurrentPositionFromRegister(int reg) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001068 if (kPointerSize == kInt64Size) {
1069 __ movq(rdi, register_location(reg));
1070 } else {
1071 // Need sign extension for x32 as rdi might be used as an index register.
1072 __ movsxlq(rdi, register_location(reg));
1073 }
1074}
1075
1076
1077void RegExpMacroAssemblerX64::ReadPositionFromRegister(Register dst, int reg) {
1078 if (kPointerSize == kInt64Size) {
1079 __ movq(dst, register_location(reg));
1080 } else {
1081 // Need sign extension for x32 as dst might be used as an index register.
1082 __ movsxlq(dst, register_location(reg));
1083 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001084}
1085
1086
1087void RegExpMacroAssemblerX64::ReadStackPointerFromRegister(int reg) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001088 __ movp(backtrack_stackpointer(), register_location(reg));
1089 __ addp(backtrack_stackpointer(), Operand(rbp, kStackHighEnd));
Steve Blocka7e24c12009-10-30 11:49:00 +00001090}
1091
1092
Ben Murdochf87a2032010-10-22 12:50:53 +01001093void RegExpMacroAssemblerX64::SetCurrentPositionFromEnd(int by) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001094 Label after_position;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001095 __ cmpp(rdi, Immediate(-by * char_size()));
Ben Murdoch257744e2011-11-30 15:57:28 +00001096 __ j(greater_equal, &after_position, Label::kNear);
Ben Murdochf87a2032010-10-22 12:50:53 +01001097 __ movq(rdi, Immediate(-by * char_size()));
1098 // On RegExp code entry (where this operation is used), the character before
1099 // the current position is expected to be already loaded.
1100 // We have advanced the position, so it's safe to read backwards.
1101 LoadCurrentCharacterUnchecked(-1, 1);
1102 __ bind(&after_position);
1103}
1104
1105
Steve Blocka7e24c12009-10-30 11:49:00 +00001106void RegExpMacroAssemblerX64::SetRegister(int register_index, int to) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001107 DCHECK(register_index >= num_saved_registers_); // Reserved for positions!
1108 __ movp(register_location(register_index), Immediate(to));
Steve Blocka7e24c12009-10-30 11:49:00 +00001109}
1110
1111
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001112bool RegExpMacroAssemblerX64::Succeed() {
Steve Blocka7e24c12009-10-30 11:49:00 +00001113 __ jmp(&success_label_);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001114 return global();
Steve Blocka7e24c12009-10-30 11:49:00 +00001115}
1116
1117
1118void RegExpMacroAssemblerX64::WriteCurrentPositionToRegister(int reg,
1119 int cp_offset) {
1120 if (cp_offset == 0) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001121 __ movp(register_location(reg), rdi);
Steve Blocka7e24c12009-10-30 11:49:00 +00001122 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001123 __ leap(rax, Operand(rdi, cp_offset * char_size()));
1124 __ movp(register_location(reg), rax);
Steve Blocka7e24c12009-10-30 11:49:00 +00001125 }
1126}
1127
1128
1129void RegExpMacroAssemblerX64::ClearRegisters(int reg_from, int reg_to) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001130 DCHECK(reg_from <= reg_to);
1131 __ movp(rax, Operand(rbp, kInputStartMinusOne));
Steve Blocka7e24c12009-10-30 11:49:00 +00001132 for (int reg = reg_from; reg <= reg_to; reg++) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001133 __ movp(register_location(reg), rax);
Steve Blocka7e24c12009-10-30 11:49:00 +00001134 }
1135}
1136
1137
1138void RegExpMacroAssemblerX64::WriteStackPointerToRegister(int reg) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001139 __ movp(rax, backtrack_stackpointer());
1140 __ subp(rax, Operand(rbp, kStackHighEnd));
1141 __ movp(register_location(reg), rax);
Steve Blocka7e24c12009-10-30 11:49:00 +00001142}
1143
1144
1145// Private methods:
1146
1147void RegExpMacroAssemblerX64::CallCheckStackGuardState() {
1148 // This function call preserves no register values. Caller should
1149 // store anything volatile in a C call or overwritten by this function.
Steve Block6ded16b2010-05-10 14:33:55 +01001150 static const int num_arguments = 3;
Leon Clarke4515c472010-02-03 11:58:03 +00001151 __ PrepareCallCFunction(num_arguments);
Steve Blocka7e24c12009-10-30 11:49:00 +00001152#ifdef _WIN64
1153 // Second argument: Code* of self. (Do this before overwriting r8).
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001154 __ movp(rdx, code_object_pointer());
Steve Blocka7e24c12009-10-30 11:49:00 +00001155 // Third argument: RegExp code frame pointer.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001156 __ movp(r8, rbp);
Steve Blocka7e24c12009-10-30 11:49:00 +00001157 // First argument: Next address on the stack (will be address of
1158 // return address).
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001159 __ leap(rcx, Operand(rsp, -kPointerSize));
Steve Blocka7e24c12009-10-30 11:49:00 +00001160#else
1161 // Third argument: RegExp code frame pointer.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001162 __ movp(rdx, rbp);
Steve Blocka7e24c12009-10-30 11:49:00 +00001163 // Second argument: Code* of self.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001164 __ movp(rsi, code_object_pointer());
Steve Blocka7e24c12009-10-30 11:49:00 +00001165 // First argument: Next address on the stack (will be address of
1166 // return address).
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001167 __ leap(rdi, Operand(rsp, -kRegisterSize));
Steve Blocka7e24c12009-10-30 11:49:00 +00001168#endif
1169 ExternalReference stack_check =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001170 ExternalReference::re_check_stack_guard_state(isolate());
Leon Clarke4515c472010-02-03 11:58:03 +00001171 __ CallCFunction(stack_check, num_arguments);
Steve Blocka7e24c12009-10-30 11:49:00 +00001172}
1173
1174
1175// Helper function for reading a value out of a stack frame.
1176template <typename T>
1177static T& frame_entry(Address re_frame, int frame_offset) {
1178 return reinterpret_cast<T&>(Memory::int32_at(re_frame + frame_offset));
1179}
1180
1181
1182int RegExpMacroAssemblerX64::CheckStackGuardState(Address* return_address,
1183 Code* re_code,
1184 Address re_frame) {
Steve Block44f0eee2011-05-26 01:26:41 +01001185 Isolate* isolate = frame_entry<Isolate*>(re_frame, kIsolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001186 StackLimitCheck check(isolate);
1187 if (check.JsHasOverflowed()) {
Steve Block44f0eee2011-05-26 01:26:41 +01001188 isolate->StackOverflow();
Steve Blocka7e24c12009-10-30 11:49:00 +00001189 return EXCEPTION;
1190 }
1191
1192 // If not real stack overflow the stack guard was used to interrupt
1193 // execution for another purpose.
1194
Leon Clarke4515c472010-02-03 11:58:03 +00001195 // If this is a direct call from JavaScript retry the RegExp forcing the call
1196 // through the runtime system. Currently the direct call cannot handle a GC.
1197 if (frame_entry<int>(re_frame, kDirectCall) == 1) {
1198 return RETRY;
1199 }
1200
Steve Blocka7e24c12009-10-30 11:49:00 +00001201 // Prepare for possible GC.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001202 HandleScope handles(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001203 Handle<Code> code_handle(re_code);
1204
1205 Handle<String> subject(frame_entry<String*>(re_frame, kInputString));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001206
Steve Blocka7e24c12009-10-30 11:49:00 +00001207 // Current string.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001208 bool is_one_byte = subject->IsOneByteRepresentationUnderneath();
Steve Blocka7e24c12009-10-30 11:49:00 +00001209
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001210 DCHECK(re_code->instruction_start() <= *return_address);
1211 DCHECK(*return_address <=
Steve Blocka7e24c12009-10-30 11:49:00 +00001212 re_code->instruction_start() + re_code->instruction_size());
1213
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001214 Object* result = isolate->stack_guard()->HandleInterrupts();
Steve Blocka7e24c12009-10-30 11:49:00 +00001215
1216 if (*code_handle != re_code) { // Return address no longer valid
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001217 intptr_t delta = code_handle->address() - re_code->address();
Steve Blocka7e24c12009-10-30 11:49:00 +00001218 // Overwrite the return address on the stack.
1219 *return_address += delta;
1220 }
1221
1222 if (result->IsException()) {
1223 return EXCEPTION;
1224 }
1225
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001226 Handle<String> subject_tmp = subject;
1227 int slice_offset = 0;
1228
1229 // Extract the underlying string and the slice offset.
1230 if (StringShape(*subject_tmp).IsCons()) {
1231 subject_tmp = Handle<String>(ConsString::cast(*subject_tmp)->first());
1232 } else if (StringShape(*subject_tmp).IsSliced()) {
1233 SlicedString* slice = SlicedString::cast(*subject_tmp);
1234 subject_tmp = Handle<String>(slice->parent());
1235 slice_offset = slice->offset();
1236 }
1237
Steve Blocka7e24c12009-10-30 11:49:00 +00001238 // String might have changed.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001239 if (subject_tmp->IsOneByteRepresentation() != is_one_byte) {
1240 // If we changed between an Latin1 and an UC16 string, the specialized
Steve Blocka7e24c12009-10-30 11:49:00 +00001241 // code cannot be used, and we need to restart regexp matching from
1242 // scratch (including, potentially, compiling a new version of the code).
1243 return RETRY;
1244 }
1245
1246 // Otherwise, the content of the string might have moved. It must still
1247 // be a sequential or external string with the same content.
1248 // Update the start and end pointers in the stack frame to the current
1249 // location (whether it has actually moved or not).
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001250 DCHECK(StringShape(*subject_tmp).IsSequential() ||
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001251 StringShape(*subject_tmp).IsExternal());
Steve Blocka7e24c12009-10-30 11:49:00 +00001252
1253 // The original start address of the characters to match.
1254 const byte* start_address = frame_entry<const byte*>(re_frame, kInputStart);
1255
1256 // Find the current start address of the same character at the current string
1257 // position.
1258 int start_index = frame_entry<int>(re_frame, kStartIndex);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001259 const byte* new_address = StringCharacterPosition(*subject_tmp,
1260 start_index + slice_offset);
Steve Blocka7e24c12009-10-30 11:49:00 +00001261
1262 if (start_address != new_address) {
1263 // If there is a difference, update the object pointer and start and end
1264 // addresses in the RegExp stack frame to match the new value.
1265 const byte* end_address = frame_entry<const byte* >(re_frame, kInputEnd);
Steve Blockd0582a62009-12-15 09:54:21 +00001266 int byte_length = static_cast<int>(end_address - start_address);
Steve Blocka7e24c12009-10-30 11:49:00 +00001267 frame_entry<const String*>(re_frame, kInputString) = *subject;
1268 frame_entry<const byte*>(re_frame, kInputStart) = new_address;
1269 frame_entry<const byte*>(re_frame, kInputEnd) = new_address + byte_length;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001270 } else if (frame_entry<const String*>(re_frame, kInputString) != *subject) {
1271 // Subject string might have been a ConsString that underwent
1272 // short-circuiting during GC. That will not change start_address but
1273 // will change pointer inside the subject handle.
1274 frame_entry<const String*>(re_frame, kInputString) = *subject;
Steve Blocka7e24c12009-10-30 11:49:00 +00001275 }
1276
1277 return 0;
1278}
1279
1280
1281Operand RegExpMacroAssemblerX64::register_location(int register_index) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001282 DCHECK(register_index < (1<<30));
Steve Blocka7e24c12009-10-30 11:49:00 +00001283 if (num_registers_ <= register_index) {
1284 num_registers_ = register_index + 1;
1285 }
1286 return Operand(rbp, kRegisterZero - register_index * kPointerSize);
1287}
1288
1289
1290void RegExpMacroAssemblerX64::CheckPosition(int cp_offset,
1291 Label* on_outside_input) {
1292 __ cmpl(rdi, Immediate(-cp_offset * char_size()));
1293 BranchOrBacktrack(greater_equal, on_outside_input);
1294}
1295
1296
1297void RegExpMacroAssemblerX64::BranchOrBacktrack(Condition condition,
1298 Label* to) {
1299 if (condition < 0) { // No condition
1300 if (to == NULL) {
1301 Backtrack();
1302 return;
1303 }
1304 __ jmp(to);
1305 return;
1306 }
1307 if (to == NULL) {
1308 __ j(condition, &backtrack_label_);
1309 return;
1310 }
1311 __ j(condition, to);
1312}
1313
1314
1315void RegExpMacroAssemblerX64::SafeCall(Label* to) {
1316 __ call(to);
1317}
1318
1319
1320void RegExpMacroAssemblerX64::SafeCallTarget(Label* label) {
1321 __ bind(label);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001322 __ subp(Operand(rsp, 0), code_object_pointer());
Steve Blocka7e24c12009-10-30 11:49:00 +00001323}
1324
1325
1326void RegExpMacroAssemblerX64::SafeReturn() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001327 __ addp(Operand(rsp, 0), code_object_pointer());
Steve Blocka7e24c12009-10-30 11:49:00 +00001328 __ ret(0);
1329}
1330
1331
1332void RegExpMacroAssemblerX64::Push(Register source) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001333 DCHECK(!source.is(backtrack_stackpointer()));
Steve Blocka7e24c12009-10-30 11:49:00 +00001334 // Notice: This updates flags, unlike normal Push.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001335 __ subp(backtrack_stackpointer(), Immediate(kIntSize));
Steve Blocka7e24c12009-10-30 11:49:00 +00001336 __ movl(Operand(backtrack_stackpointer(), 0), source);
1337}
1338
1339
1340void RegExpMacroAssemblerX64::Push(Immediate value) {
1341 // Notice: This updates flags, unlike normal Push.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001342 __ subp(backtrack_stackpointer(), Immediate(kIntSize));
Steve Blocka7e24c12009-10-30 11:49:00 +00001343 __ movl(Operand(backtrack_stackpointer(), 0), value);
1344}
1345
1346
1347void RegExpMacroAssemblerX64::FixupCodeRelativePositions() {
1348 for (int i = 0, n = code_relative_fixup_positions_.length(); i < n; i++) {
1349 int position = code_relative_fixup_positions_[i];
1350 // The position succeeds a relative label offset from position.
1351 // Patch the relative offset to be relative to the Code object pointer
1352 // instead.
1353 int patch_position = position - kIntSize;
Steve Block44f0eee2011-05-26 01:26:41 +01001354 int offset = masm_.long_at(patch_position);
1355 masm_.long_at_put(patch_position,
Steve Blocka7e24c12009-10-30 11:49:00 +00001356 offset
1357 + position
1358 + Code::kHeaderSize
1359 - kHeapObjectTag);
1360 }
1361 code_relative_fixup_positions_.Clear();
1362}
1363
1364
1365void RegExpMacroAssemblerX64::Push(Label* backtrack_target) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001366 __ subp(backtrack_stackpointer(), Immediate(kIntSize));
Steve Blocka7e24c12009-10-30 11:49:00 +00001367 __ movl(Operand(backtrack_stackpointer(), 0), backtrack_target);
1368 MarkPositionForCodeRelativeFixup();
1369}
1370
1371
1372void RegExpMacroAssemblerX64::Pop(Register target) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001373 DCHECK(!target.is(backtrack_stackpointer()));
Steve Blocka7e24c12009-10-30 11:49:00 +00001374 __ movsxlq(target, Operand(backtrack_stackpointer(), 0));
1375 // Notice: This updates flags, unlike normal Pop.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001376 __ addp(backtrack_stackpointer(), Immediate(kIntSize));
Steve Blocka7e24c12009-10-30 11:49:00 +00001377}
1378
1379
1380void RegExpMacroAssemblerX64::Drop() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001381 __ addp(backtrack_stackpointer(), Immediate(kIntSize));
Steve Blocka7e24c12009-10-30 11:49:00 +00001382}
1383
1384
1385void RegExpMacroAssemblerX64::CheckPreemption() {
1386 // Check for preemption.
1387 Label no_preempt;
Steve Blockd0582a62009-12-15 09:54:21 +00001388 ExternalReference stack_limit =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001389 ExternalReference::address_of_stack_limit(isolate());
Steve Blockd0582a62009-12-15 09:54:21 +00001390 __ load_rax(stack_limit);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001391 __ cmpp(rsp, rax);
Steve Blocka7e24c12009-10-30 11:49:00 +00001392 __ j(above, &no_preempt);
1393
1394 SafeCall(&check_preempt_label_);
1395
1396 __ bind(&no_preempt);
1397}
1398
1399
1400void RegExpMacroAssemblerX64::CheckStackLimit() {
Steve Blockd0582a62009-12-15 09:54:21 +00001401 Label no_stack_overflow;
1402 ExternalReference stack_limit =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001403 ExternalReference::address_of_regexp_stack_limit(isolate());
Steve Blockd0582a62009-12-15 09:54:21 +00001404 __ load_rax(stack_limit);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001405 __ cmpp(backtrack_stackpointer(), rax);
Steve Blockd0582a62009-12-15 09:54:21 +00001406 __ j(above, &no_stack_overflow);
Steve Blocka7e24c12009-10-30 11:49:00 +00001407
Steve Blockd0582a62009-12-15 09:54:21 +00001408 SafeCall(&stack_overflow_label_);
Steve Blocka7e24c12009-10-30 11:49:00 +00001409
Steve Blockd0582a62009-12-15 09:54:21 +00001410 __ bind(&no_stack_overflow);
Steve Blocka7e24c12009-10-30 11:49:00 +00001411}
1412
1413
Steve Blocka7e24c12009-10-30 11:49:00 +00001414void RegExpMacroAssemblerX64::LoadCurrentCharacterUnchecked(int cp_offset,
1415 int characters) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001416 if (mode_ == LATIN1) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001417 if (characters == 4) {
1418 __ movl(current_character(), Operand(rsi, rdi, times_1, cp_offset));
1419 } else if (characters == 2) {
1420 __ movzxwl(current_character(), Operand(rsi, rdi, times_1, cp_offset));
1421 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001422 DCHECK(characters == 1);
Steve Blocka7e24c12009-10-30 11:49:00 +00001423 __ movzxbl(current_character(), Operand(rsi, rdi, times_1, cp_offset));
1424 }
1425 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001426 DCHECK(mode_ == UC16);
Steve Blocka7e24c12009-10-30 11:49:00 +00001427 if (characters == 2) {
1428 __ movl(current_character(),
1429 Operand(rsi, rdi, times_1, cp_offset * sizeof(uc16)));
1430 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001431 DCHECK(characters == 1);
Steve Blocka7e24c12009-10-30 11:49:00 +00001432 __ movzxwl(current_character(),
1433 Operand(rsi, rdi, times_1, cp_offset * sizeof(uc16)));
1434 }
1435 }
1436}
1437
Steve Blocka7e24c12009-10-30 11:49:00 +00001438#undef __
1439
Steve Block6ded16b2010-05-10 14:33:55 +01001440#endif // V8_INTERPRETED_REGEXP
Steve Blocka7e24c12009-10-30 11:49:00 +00001441
1442}} // namespace v8::internal
Leon Clarkef7060e22010-06-03 12:02:55 +01001443
1444#endif // V8_TARGET_ARCH_X64