ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1 | // Copyright 2008 the V8 project authors. All rights reserved. |
| 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 28 | #include "v8.h" |
| 29 | #include "ast.h" |
| 30 | #include "assembler.h" |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 31 | #include "regexp-stack.h" |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 32 | #include "regexp-macro-assembler.h" |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame^] | 33 | #if V8_TARGET_ARCH_ARM |
| 34 | #include "arm/simulator-arm.h" |
| 35 | #elif V8_TARGET_ARCH_IA32 |
| 36 | #include "ia32/simulator-ia32.h" |
| 37 | #elif V8_TARGET_ARCH_X64 |
| 38 | #include "x64/simulator-x64.h" |
| 39 | #endif |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 40 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 41 | namespace v8 { |
| 42 | namespace internal { |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 43 | |
| 44 | RegExpMacroAssembler::RegExpMacroAssembler() { |
| 45 | } |
| 46 | |
| 47 | |
| 48 | RegExpMacroAssembler::~RegExpMacroAssembler() { |
| 49 | } |
| 50 | |
| 51 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame^] | 52 | bool RegExpMacroAssembler::CanReadUnaligned() { |
| 53 | #ifdef V8_HOST_CAN_READ_UNALIGNED |
| 54 | return true; |
| 55 | #else |
| 56 | return false; |
| 57 | #endif |
| 58 | } |
| 59 | |
| 60 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 61 | #ifdef V8_NATIVE_REGEXP // Avoid unused code, e.g., on ARM. |
| 62 | |
| 63 | NativeRegExpMacroAssembler::NativeRegExpMacroAssembler() { |
| 64 | } |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 65 | |
| 66 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 67 | NativeRegExpMacroAssembler::~NativeRegExpMacroAssembler() { |
| 68 | } |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 69 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame^] | 70 | |
| 71 | bool NativeRegExpMacroAssembler::CanReadUnaligned() { |
| 72 | #ifdef V8_TARGET_CAN_READ_UNALIGNED |
| 73 | return true; |
| 74 | #else |
| 75 | return false; |
| 76 | #endif |
| 77 | } |
| 78 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 79 | const byte* NativeRegExpMacroAssembler::StringCharacterPosition( |
| 80 | String* subject, |
| 81 | int start_index) { |
| 82 | // Not just flat, but ultra flat. |
| 83 | ASSERT(subject->IsExternalString() || subject->IsSeqString()); |
| 84 | ASSERT(start_index >= 0); |
| 85 | ASSERT(start_index <= subject->length()); |
| 86 | if (subject->IsAsciiRepresentation()) { |
| 87 | const byte* address; |
| 88 | if (StringShape(subject).IsExternal()) { |
| 89 | const char* data = ExternalAsciiString::cast(subject)->resource()->data(); |
| 90 | address = reinterpret_cast<const byte*>(data); |
| 91 | } else { |
| 92 | ASSERT(subject->IsSeqAsciiString()); |
| 93 | char* data = SeqAsciiString::cast(subject)->GetChars(); |
| 94 | address = reinterpret_cast<const byte*>(data); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 95 | } |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 96 | return address + start_index; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 97 | } |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 98 | const uc16* data; |
| 99 | if (StringShape(subject).IsExternal()) { |
| 100 | data = ExternalTwoByteString::cast(subject)->resource()->data(); |
| 101 | } else { |
| 102 | ASSERT(subject->IsSeqTwoByteString()); |
| 103 | data = SeqTwoByteString::cast(subject)->GetChars(); |
| 104 | } |
| 105 | return reinterpret_cast<const byte*>(data + start_index); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 106 | } |
| 107 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 108 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 109 | NativeRegExpMacroAssembler::Result NativeRegExpMacroAssembler::Match( |
| 110 | Handle<Code> regexp_code, |
| 111 | Handle<String> subject, |
| 112 | int* offsets_vector, |
| 113 | int offsets_vector_length, |
| 114 | int previous_index) { |
| 115 | |
| 116 | ASSERT(subject->IsFlat()); |
| 117 | ASSERT(previous_index >= 0); |
| 118 | ASSERT(previous_index <= subject->length()); |
| 119 | |
| 120 | // No allocations before calling the regexp, but we can't use |
| 121 | // AssertNoAllocation, since regexps might be preempted, and another thread |
| 122 | // might do allocation anyway. |
| 123 | |
| 124 | String* subject_ptr = *subject; |
| 125 | // Character offsets into string. |
| 126 | int start_offset = previous_index; |
| 127 | int end_offset = subject_ptr->length(); |
| 128 | |
| 129 | bool is_ascii = subject->IsAsciiRepresentation(); |
| 130 | |
| 131 | if (StringShape(subject_ptr).IsCons()) { |
| 132 | subject_ptr = ConsString::cast(subject_ptr)->first(); |
| 133 | } else if (StringShape(subject_ptr).IsSliced()) { |
| 134 | SlicedString* slice = SlicedString::cast(subject_ptr); |
| 135 | start_offset += slice->start(); |
| 136 | end_offset += slice->start(); |
| 137 | subject_ptr = slice->buffer(); |
| 138 | } |
| 139 | // Ensure that an underlying string has the same ascii-ness. |
| 140 | ASSERT(subject_ptr->IsAsciiRepresentation() == is_ascii); |
| 141 | ASSERT(subject_ptr->IsExternalString() || subject_ptr->IsSeqString()); |
| 142 | // String is now either Sequential or External |
| 143 | int char_size_shift = is_ascii ? 0 : 1; |
| 144 | int char_length = end_offset - start_offset; |
| 145 | |
| 146 | const byte* input_start = |
| 147 | StringCharacterPosition(subject_ptr, start_offset); |
| 148 | int byte_length = char_length << char_size_shift; |
| 149 | const byte* input_end = input_start + byte_length; |
| 150 | Result res = Execute(*regexp_code, |
| 151 | subject_ptr, |
| 152 | start_offset, |
| 153 | input_start, |
| 154 | input_end, |
| 155 | offsets_vector, |
| 156 | previous_index == 0); |
| 157 | |
| 158 | if (res == SUCCESS) { |
| 159 | // Capture values are relative to start_offset only. |
| 160 | // Convert them to be relative to start of string. |
| 161 | for (int i = 0; i < offsets_vector_length; i++) { |
| 162 | if (offsets_vector[i] >= 0) { |
| 163 | offsets_vector[i] += previous_index; |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | return res; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 169 | } |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 170 | |
| 171 | |
| 172 | NativeRegExpMacroAssembler::Result NativeRegExpMacroAssembler::Execute( |
| 173 | Code* code, |
| 174 | String* input, |
| 175 | int start_offset, |
| 176 | const byte* input_start, |
| 177 | const byte* input_end, |
| 178 | int* output, |
| 179 | bool at_start) { |
| 180 | typedef int (*matcher)(String*, int, const byte*, |
| 181 | const byte*, int*, int, Address); |
| 182 | matcher matcher_func = FUNCTION_CAST<matcher>(code->entry()); |
| 183 | |
| 184 | int at_start_val = at_start ? 1 : 0; |
| 185 | |
| 186 | // Ensure that the minimum stack has been allocated. |
| 187 | RegExpStack stack; |
| 188 | Address stack_base = RegExpStack::stack_base(); |
| 189 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame^] | 190 | int result = CALL_GENERATED_REGEXP_CODE(matcher_func, |
| 191 | input, |
| 192 | start_offset, |
| 193 | input_start, |
| 194 | input_end, |
| 195 | output, |
| 196 | at_start_val, |
| 197 | stack_base); |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 198 | ASSERT(result <= SUCCESS); |
| 199 | ASSERT(result >= RETRY); |
| 200 | |
| 201 | if (result == EXCEPTION && !Top::has_pending_exception()) { |
| 202 | // We detected a stack overflow (on the backtrack stack) in RegExp code, |
| 203 | // but haven't created the exception yet. |
| 204 | Top::StackOverflow(); |
| 205 | } |
| 206 | return static_cast<Result>(result); |
| 207 | } |
| 208 | |
| 209 | |
| 210 | static unibrow::Mapping<unibrow::Ecma262Canonicalize> canonicalize; |
| 211 | |
| 212 | int NativeRegExpMacroAssembler::CaseInsensitiveCompareUC16( |
| 213 | Address byte_offset1, |
| 214 | Address byte_offset2, |
| 215 | size_t byte_length) { |
| 216 | // This function is not allowed to cause a garbage collection. |
| 217 | // A GC might move the calling generated code and invalidate the |
| 218 | // return address on the stack. |
| 219 | ASSERT(byte_length % 2 == 0); |
| 220 | uc16* substring1 = reinterpret_cast<uc16*>(byte_offset1); |
| 221 | uc16* substring2 = reinterpret_cast<uc16*>(byte_offset2); |
| 222 | size_t length = byte_length >> 1; |
| 223 | |
| 224 | for (size_t i = 0; i < length; i++) { |
| 225 | unibrow::uchar c1 = substring1[i]; |
| 226 | unibrow::uchar c2 = substring2[i]; |
| 227 | if (c1 != c2) { |
| 228 | unibrow::uchar s1[1] = { c1 }; |
| 229 | canonicalize.get(c1, '\0', s1); |
| 230 | if (s1[0] != c2) { |
| 231 | unibrow::uchar s2[1] = { c2 }; |
| 232 | canonicalize.get(c2, '\0', s2); |
| 233 | if (s1[0] != s2[0]) { |
| 234 | return 0; |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | return 1; |
| 240 | } |
| 241 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame^] | 242 | |
| 243 | Address NativeRegExpMacroAssembler::GrowStack(Address stack_pointer, |
| 244 | Address* stack_base) { |
| 245 | size_t size = RegExpStack::stack_capacity(); |
| 246 | Address old_stack_base = RegExpStack::stack_base(); |
| 247 | ASSERT(old_stack_base == *stack_base); |
| 248 | ASSERT(stack_pointer <= old_stack_base); |
| 249 | ASSERT(static_cast<size_t>(old_stack_base - stack_pointer) <= size); |
| 250 | Address new_stack_base = RegExpStack::EnsureCapacity(size * 2); |
| 251 | if (new_stack_base == NULL) { |
| 252 | return NULL; |
| 253 | } |
| 254 | *stack_base = new_stack_base; |
| 255 | intptr_t stack_content_size = old_stack_base - stack_pointer; |
| 256 | return new_stack_base - stack_content_size; |
| 257 | } |
| 258 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 259 | #endif // V8_NATIVE_REGEXP |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 260 | } } // namespace v8::internal |