2008-11-25 11:07:48 +00:00
|
|
|
// Copyright 2008 the V8 project authors. All rights reserved.
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
#include "v8.h"
|
|
|
|
#include "ast.h"
|
|
|
|
#include "assembler.h"
|
2009-08-14 11:24:32 +00:00
|
|
|
#include "regexp-stack.h"
|
2008-11-25 11:07:48 +00:00
|
|
|
#include "regexp-macro-assembler.h"
|
2009-11-05 13:27:21 +00:00
|
|
|
#include "simulator.h"
|
2008-11-25 11:07:48 +00:00
|
|
|
|
2009-05-25 10:05:56 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
2008-11-25 11:07:48 +00:00
|
|
|
|
2011-05-11 06:39:27 +00:00
|
|
|
RegExpMacroAssembler::RegExpMacroAssembler() : slow_safe_compiler_(false) {
|
2008-11-25 11:07:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
RegExpMacroAssembler::~RegExpMacroAssembler() {
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-31 12:40:37 +00:00
|
|
|
bool RegExpMacroAssembler::CanReadUnaligned() {
|
|
|
|
#ifdef V8_HOST_CAN_READ_UNALIGNED
|
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-19 19:30:11 +00:00
|
|
|
#ifndef V8_INTERPRETED_REGEXP // Avoid unused code, e.g., on ARM.
|
2009-08-14 11:24:32 +00:00
|
|
|
|
2011-05-11 06:39:27 +00:00
|
|
|
NativeRegExpMacroAssembler::NativeRegExpMacroAssembler()
|
|
|
|
: RegExpMacroAssembler() {
|
2009-08-14 11:24:32 +00:00
|
|
|
}
|
2008-11-25 11:07:48 +00:00
|
|
|
|
|
|
|
|
2009-08-14 11:24:32 +00:00
|
|
|
NativeRegExpMacroAssembler::~NativeRegExpMacroAssembler() {
|
|
|
|
}
|
2008-11-25 11:07:48 +00:00
|
|
|
|
2009-08-31 12:40:37 +00:00
|
|
|
|
|
|
|
bool NativeRegExpMacroAssembler::CanReadUnaligned() {
|
|
|
|
#ifdef V8_TARGET_CAN_READ_UNALIGNED
|
2011-05-11 06:39:27 +00:00
|
|
|
return !slow_safe();
|
2009-08-31 12:40:37 +00:00
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-08-14 11:24:32 +00:00
|
|
|
const byte* NativeRegExpMacroAssembler::StringCharacterPosition(
|
|
|
|
String* subject,
|
|
|
|
int start_index) {
|
|
|
|
// Not just flat, but ultra flat.
|
|
|
|
ASSERT(subject->IsExternalString() || subject->IsSeqString());
|
|
|
|
ASSERT(start_index >= 0);
|
|
|
|
ASSERT(start_index <= subject->length());
|
|
|
|
if (subject->IsAsciiRepresentation()) {
|
|
|
|
const byte* address;
|
|
|
|
if (StringShape(subject).IsExternal()) {
|
2011-11-17 17:05:12 +00:00
|
|
|
const char* data = ExternalAsciiString::cast(subject)->GetChars();
|
2009-08-14 11:24:32 +00:00
|
|
|
address = reinterpret_cast<const byte*>(data);
|
|
|
|
} else {
|
|
|
|
ASSERT(subject->IsSeqAsciiString());
|
|
|
|
char* data = SeqAsciiString::cast(subject)->GetChars();
|
|
|
|
address = reinterpret_cast<const byte*>(data);
|
2008-11-25 11:07:48 +00:00
|
|
|
}
|
2009-08-14 11:24:32 +00:00
|
|
|
return address + start_index;
|
|
|
|
}
|
|
|
|
const uc16* data;
|
|
|
|
if (StringShape(subject).IsExternal()) {
|
2011-11-17 17:05:12 +00:00
|
|
|
data = ExternalTwoByteString::cast(subject)->GetChars();
|
2009-08-14 11:24:32 +00:00
|
|
|
} else {
|
|
|
|
ASSERT(subject->IsSeqTwoByteString());
|
|
|
|
data = SeqTwoByteString::cast(subject)->GetChars();
|
2008-11-25 11:07:48 +00:00
|
|
|
}
|
2009-08-14 11:24:32 +00:00
|
|
|
return reinterpret_cast<const byte*>(data + start_index);
|
2008-11-25 11:07:48 +00:00
|
|
|
}
|
|
|
|
|
2009-01-12 13:05:23 +00:00
|
|
|
|
2009-08-14 11:24:32 +00:00
|
|
|
NativeRegExpMacroAssembler::Result NativeRegExpMacroAssembler::Match(
|
|
|
|
Handle<Code> regexp_code,
|
|
|
|
Handle<String> subject,
|
|
|
|
int* offsets_vector,
|
|
|
|
int offsets_vector_length,
|
2011-03-18 20:35:07 +00:00
|
|
|
int previous_index,
|
|
|
|
Isolate* isolate) {
|
2009-08-14 11:24:32 +00:00
|
|
|
|
|
|
|
ASSERT(subject->IsFlat());
|
|
|
|
ASSERT(previous_index >= 0);
|
|
|
|
ASSERT(previous_index <= subject->length());
|
|
|
|
|
|
|
|
// No allocations before calling the regexp, but we can't use
|
|
|
|
// AssertNoAllocation, since regexps might be preempted, and another thread
|
|
|
|
// might do allocation anyway.
|
|
|
|
|
|
|
|
String* subject_ptr = *subject;
|
|
|
|
// Character offsets into string.
|
|
|
|
int start_offset = previous_index;
|
2011-08-26 13:03:30 +00:00
|
|
|
int char_length = subject_ptr->length() - start_offset;
|
|
|
|
int slice_offset = 0;
|
2009-08-14 11:24:32 +00:00
|
|
|
|
2011-08-26 13:03:30 +00:00
|
|
|
// The string has been flattened, so if it is a cons string it contains the
|
2010-01-22 08:31:33 +00:00
|
|
|
// full string in the first part.
|
2009-08-14 11:24:32 +00:00
|
|
|
if (StringShape(subject_ptr).IsCons()) {
|
2010-01-22 08:31:33 +00:00
|
|
|
ASSERT_EQ(0, ConsString::cast(subject_ptr)->second()->length());
|
2009-08-14 11:24:32 +00:00
|
|
|
subject_ptr = ConsString::cast(subject_ptr)->first();
|
2011-08-26 13:03:30 +00:00
|
|
|
} else if (StringShape(subject_ptr).IsSliced()) {
|
|
|
|
SlicedString* slice = SlicedString::cast(subject_ptr);
|
|
|
|
subject_ptr = slice->parent();
|
|
|
|
slice_offset = slice->offset();
|
2009-08-14 11:24:32 +00:00
|
|
|
}
|
2012-01-16 12:38:59 +00:00
|
|
|
// Ensure that an underlying string has the same ASCII-ness.
|
2010-06-30 07:40:40 +00:00
|
|
|
bool is_ascii = subject_ptr->IsAsciiRepresentation();
|
2009-08-14 11:24:32 +00:00
|
|
|
ASSERT(subject_ptr->IsExternalString() || subject_ptr->IsSeqString());
|
|
|
|
// String is now either Sequential or External
|
|
|
|
int char_size_shift = is_ascii ? 0 : 1;
|
|
|
|
|
|
|
|
const byte* input_start =
|
2011-08-26 13:03:30 +00:00
|
|
|
StringCharacterPosition(subject_ptr, start_offset + slice_offset);
|
2009-08-14 11:24:32 +00:00
|
|
|
int byte_length = char_length << char_size_shift;
|
|
|
|
const byte* input_end = input_start + byte_length;
|
|
|
|
Result res = Execute(*regexp_code,
|
2011-08-26 13:03:30 +00:00
|
|
|
*subject,
|
2009-08-14 11:24:32 +00:00
|
|
|
start_offset,
|
|
|
|
input_start,
|
|
|
|
input_end,
|
2011-03-18 20:35:07 +00:00
|
|
|
offsets_vector,
|
|
|
|
isolate);
|
2009-08-14 11:24:32 +00:00
|
|
|
return res;
|
2008-11-25 11:07:48 +00:00
|
|
|
}
|
2009-08-14 11:24:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
NativeRegExpMacroAssembler::Result NativeRegExpMacroAssembler::Execute(
|
|
|
|
Code* code,
|
2011-08-26 13:03:30 +00:00
|
|
|
String* input, // This needs to be the unpacked (sliced, cons) string.
|
2009-08-14 11:24:32 +00:00
|
|
|
int start_offset,
|
|
|
|
const byte* input_start,
|
|
|
|
const byte* input_end,
|
2011-03-18 20:35:07 +00:00
|
|
|
int* output,
|
|
|
|
Isolate* isolate) {
|
|
|
|
ASSERT(isolate == Isolate::Current());
|
2009-08-14 11:24:32 +00:00
|
|
|
// Ensure that the minimum stack has been allocated.
|
2011-03-18 20:35:07 +00:00
|
|
|
RegExpStackScope stack_scope(isolate);
|
|
|
|
Address stack_base = stack_scope.stack()->stack_base();
|
2009-08-14 11:24:32 +00:00
|
|
|
|
2010-01-06 11:09:30 +00:00
|
|
|
int direct_call = 0;
|
2011-02-16 09:18:19 +00:00
|
|
|
int result = CALL_GENERATED_REGEXP_CODE(code->entry(),
|
2009-08-31 12:40:37 +00:00
|
|
|
input,
|
|
|
|
start_offset,
|
|
|
|
input_start,
|
|
|
|
input_end,
|
|
|
|
output,
|
2010-01-06 11:09:30 +00:00
|
|
|
stack_base,
|
2011-03-18 20:35:07 +00:00
|
|
|
direct_call,
|
|
|
|
isolate);
|
2009-08-14 11:24:32 +00:00
|
|
|
ASSERT(result <= SUCCESS);
|
|
|
|
ASSERT(result >= RETRY);
|
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
if (result == EXCEPTION && !isolate->has_pending_exception()) {
|
2009-08-14 11:24:32 +00:00
|
|
|
// We detected a stack overflow (on the backtrack stack) in RegExp code,
|
|
|
|
// but haven't created the exception yet.
|
2011-03-18 20:35:07 +00:00
|
|
|
isolate->StackOverflow();
|
2009-08-14 11:24:32 +00:00
|
|
|
}
|
|
|
|
return static_cast<Result>(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
const byte NativeRegExpMacroAssembler::word_character_map[] = {
|
2010-01-18 09:49:50 +00:00
|
|
|
0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u,
|
|
|
|
0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u,
|
|
|
|
0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u,
|
|
|
|
0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u,
|
|
|
|
|
|
|
|
0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u,
|
|
|
|
0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u,
|
|
|
|
0xffu, 0xffu, 0xffu, 0xffu, 0xffu, 0xffu, 0xffu, 0xffu, // '0' - '7'
|
|
|
|
0xffu, 0xffu, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, // '8' - '9'
|
|
|
|
|
|
|
|
0x00u, 0xffu, 0xffu, 0xffu, 0xffu, 0xffu, 0xffu, 0xffu, // 'A' - 'G'
|
|
|
|
0xffu, 0xffu, 0xffu, 0xffu, 0xffu, 0xffu, 0xffu, 0xffu, // 'H' - 'O'
|
|
|
|
0xffu, 0xffu, 0xffu, 0xffu, 0xffu, 0xffu, 0xffu, 0xffu, // 'P' - 'W'
|
|
|
|
0xffu, 0xffu, 0xffu, 0x00u, 0x00u, 0x00u, 0x00u, 0xffu, // 'X' - 'Z', '_'
|
|
|
|
|
|
|
|
0x00u, 0xffu, 0xffu, 0xffu, 0xffu, 0xffu, 0xffu, 0xffu, // 'a' - 'g'
|
|
|
|
0xffu, 0xffu, 0xffu, 0xffu, 0xffu, 0xffu, 0xffu, 0xffu, // 'h' - 'o'
|
|
|
|
0xffu, 0xffu, 0xffu, 0xffu, 0xffu, 0xffu, 0xffu, 0xffu, // 'p' - 'w'
|
|
|
|
0xffu, 0xffu, 0xffu, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, // 'x' - 'z'
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-08-14 11:24:32 +00:00
|
|
|
int NativeRegExpMacroAssembler::CaseInsensitiveCompareUC16(
|
|
|
|
Address byte_offset1,
|
|
|
|
Address byte_offset2,
|
2011-03-18 20:35:07 +00:00
|
|
|
size_t byte_length,
|
|
|
|
Isolate* isolate) {
|
|
|
|
ASSERT(isolate == Isolate::Current());
|
|
|
|
unibrow::Mapping<unibrow::Ecma262Canonicalize>* canonicalize =
|
|
|
|
isolate->regexp_macro_assembler_canonicalize();
|
2009-08-14 11:24:32 +00:00
|
|
|
// This function is not allowed to cause a garbage collection.
|
|
|
|
// A GC might move the calling generated code and invalidate the
|
|
|
|
// return address on the stack.
|
|
|
|
ASSERT(byte_length % 2 == 0);
|
|
|
|
uc16* substring1 = reinterpret_cast<uc16*>(byte_offset1);
|
|
|
|
uc16* substring2 = reinterpret_cast<uc16*>(byte_offset2);
|
|
|
|
size_t length = byte_length >> 1;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < length; i++) {
|
|
|
|
unibrow::uchar c1 = substring1[i];
|
|
|
|
unibrow::uchar c2 = substring2[i];
|
|
|
|
if (c1 != c2) {
|
|
|
|
unibrow::uchar s1[1] = { c1 };
|
2011-03-18 20:35:07 +00:00
|
|
|
canonicalize->get(c1, '\0', s1);
|
2009-08-14 11:24:32 +00:00
|
|
|
if (s1[0] != c2) {
|
|
|
|
unibrow::uchar s2[1] = { c2 };
|
2011-03-18 20:35:07 +00:00
|
|
|
canonicalize->get(c2, '\0', s2);
|
2009-08-14 11:24:32 +00:00
|
|
|
if (s1[0] != s2[0]) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-08-31 12:40:37 +00:00
|
|
|
|
|
|
|
Address NativeRegExpMacroAssembler::GrowStack(Address stack_pointer,
|
2011-03-18 20:35:07 +00:00
|
|
|
Address* stack_base,
|
|
|
|
Isolate* isolate) {
|
|
|
|
ASSERT(isolate == Isolate::Current());
|
|
|
|
RegExpStack* regexp_stack = isolate->regexp_stack();
|
|
|
|
size_t size = regexp_stack->stack_capacity();
|
|
|
|
Address old_stack_base = regexp_stack->stack_base();
|
2009-08-31 12:40:37 +00:00
|
|
|
ASSERT(old_stack_base == *stack_base);
|
|
|
|
ASSERT(stack_pointer <= old_stack_base);
|
|
|
|
ASSERT(static_cast<size_t>(old_stack_base - stack_pointer) <= size);
|
2011-03-18 20:35:07 +00:00
|
|
|
Address new_stack_base = regexp_stack->EnsureCapacity(size * 2);
|
2009-08-31 12:40:37 +00:00
|
|
|
if (new_stack_base == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
*stack_base = new_stack_base;
|
|
|
|
intptr_t stack_content_size = old_stack_base - stack_pointer;
|
|
|
|
return new_stack_base - stack_content_size;
|
|
|
|
}
|
|
|
|
|
2010-04-19 19:30:11 +00:00
|
|
|
#endif // V8_INTERPRETED_REGEXP
|
|
|
|
|
2009-01-12 13:05:23 +00:00
|
|
|
} } // namespace v8::internal
|