2011-08-23 12:22:12 +00:00
|
|
|
// Copyright 2011 the V8 project authors. All rights reserved.
|
2014-04-29 06:42:26 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
2008-11-25 11:07:48 +00:00
|
|
|
|
|
|
|
// A simple interpreter for the Irregexp byte code.
|
|
|
|
|
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/v8.h"
|
2014-06-20 08:40:11 +00:00
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/ast.h"
|
|
|
|
#include "src/bytecodes-irregexp.h"
|
|
|
|
#include "src/interpreter-irregexp.h"
|
|
|
|
#include "src/jsregexp.h"
|
|
|
|
#include "src/regexp-macro-assembler.h"
|
2014-06-20 08:40:11 +00:00
|
|
|
#include "src/unicode.h"
|
|
|
|
#include "src/utils.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-03-18 20:35:07 +00:00
|
|
|
typedef unibrow::Mapping<unibrow::Ecma262Canonicalize> Canonicalize;
|
2008-11-25 11:39:20 +00:00
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
static bool BackRefMatchesNoCase(Canonicalize* interp_canonicalize,
|
|
|
|
int from,
|
2008-11-25 11:39:20 +00:00
|
|
|
int current,
|
|
|
|
int len,
|
|
|
|
Vector<const uc16> subject) {
|
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
unibrow::uchar old_char = subject[from++];
|
|
|
|
unibrow::uchar new_char = subject[current++];
|
|
|
|
if (old_char == new_char) continue;
|
2009-07-20 12:28:02 +00:00
|
|
|
unibrow::uchar old_string[1] = { old_char };
|
|
|
|
unibrow::uchar new_string[1] = { new_char };
|
2011-03-18 20:35:07 +00:00
|
|
|
interp_canonicalize->get(old_char, '\0', old_string);
|
|
|
|
interp_canonicalize->get(new_char, '\0', new_string);
|
2009-07-20 12:28:02 +00:00
|
|
|
if (old_string[0] != new_string[0]) {
|
2008-11-25 11:39:20 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
static bool BackRefMatchesNoCase(Canonicalize* interp_canonicalize,
|
|
|
|
int from,
|
2008-12-09 08:30:49 +00:00
|
|
|
int current,
|
|
|
|
int len,
|
2013-01-09 15:47:53 +00:00
|
|
|
Vector<const uint8_t> subject) {
|
2008-12-09 08:30:49 +00:00
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
unsigned int old_char = subject[from++];
|
|
|
|
unsigned int new_char = subject[current++];
|
|
|
|
if (old_char == new_char) continue;
|
2013-03-22 09:47:06 +00:00
|
|
|
// Convert both characters to lower case.
|
|
|
|
old_char |= 0x20;
|
|
|
|
new_char |= 0x20;
|
2008-12-09 08:30:49 +00:00
|
|
|
if (old_char != new_char) return false;
|
2013-03-22 09:47:06 +00:00
|
|
|
// Not letters in the ASCII range and Latin-1 range.
|
|
|
|
if (!(old_char - 'a' <= 'z' - 'a') &&
|
|
|
|
!(old_char - 224 <= 254 - 224 && old_char != 247)) {
|
|
|
|
return false;
|
|
|
|
}
|
2008-12-09 08:30:49 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-25 11:07:48 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
static void TraceInterpreter(const byte* code_base,
|
|
|
|
const byte* pc,
|
|
|
|
int stack_depth,
|
|
|
|
int current_position,
|
2008-12-19 12:02:34 +00:00
|
|
|
uint32_t current_char,
|
2008-11-25 11:07:48 +00:00
|
|
|
int bytecode_length,
|
|
|
|
const char* bytecode_name) {
|
|
|
|
if (FLAG_trace_regexp_bytecodes) {
|
2008-12-19 12:02:34 +00:00
|
|
|
bool printable = (current_char < 127 && current_char >= 32);
|
|
|
|
const char* format =
|
|
|
|
printable ?
|
|
|
|
"pc = %02x, sp = %d, curpos = %d, curchar = %08x (%c), bc = %s" :
|
|
|
|
"pc = %02x, sp = %d, curpos = %d, curchar = %08x .%c., bc = %s";
|
|
|
|
PrintF(format,
|
2008-11-26 12:18:17 +00:00
|
|
|
pc - code_base,
|
|
|
|
stack_depth,
|
|
|
|
current_position,
|
2008-12-19 12:02:34 +00:00
|
|
|
current_char,
|
|
|
|
printable ? current_char : '.',
|
2008-11-26 12:18:17 +00:00
|
|
|
bytecode_name);
|
2009-01-20 18:50:01 +00:00
|
|
|
for (int i = 0; i < bytecode_length; i++) {
|
2008-11-25 11:07:48 +00:00
|
|
|
printf(", %02x", pc[i]);
|
|
|
|
}
|
2008-12-19 12:02:34 +00:00
|
|
|
printf(" ");
|
|
|
|
for (int i = 1; i < bytecode_length; i++) {
|
|
|
|
unsigned char b = pc[i];
|
|
|
|
if (b < 127 && b >= 32) {
|
|
|
|
printf("%c", b);
|
|
|
|
} else {
|
|
|
|
printf(".");
|
|
|
|
}
|
|
|
|
}
|
2008-11-25 11:07:48 +00:00
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-11 09:50:06 +00:00
|
|
|
#define BYTECODE(name) \
|
|
|
|
case BC_##name: \
|
|
|
|
TraceInterpreter(code_base, \
|
|
|
|
pc, \
|
|
|
|
static_cast<int>(backtrack_sp - backtrack_stack_base), \
|
|
|
|
current, \
|
|
|
|
current_char, \
|
|
|
|
BC_##name##_LENGTH, \
|
2008-11-26 12:18:17 +00:00
|
|
|
#name);
|
2008-11-25 11:07:48 +00:00
|
|
|
#else
|
2009-11-11 09:50:06 +00:00
|
|
|
#define BYTECODE(name) \
|
2008-11-26 12:18:17 +00:00
|
|
|
case BC_##name:
|
2008-11-25 11:07:48 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2009-01-20 18:50:01 +00:00
|
|
|
static int32_t Load32Aligned(const byte* pc) {
|
2009-05-05 14:23:41 +00:00
|
|
|
ASSERT((reinterpret_cast<intptr_t>(pc) & 3) == 0);
|
2009-01-20 18:50:01 +00:00
|
|
|
return *reinterpret_cast<const int32_t *>(pc);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int32_t Load16Aligned(const byte* pc) {
|
2009-05-05 14:23:41 +00:00
|
|
|
ASSERT((reinterpret_cast<intptr_t>(pc) & 1) == 0);
|
2009-01-20 18:50:01 +00:00
|
|
|
return *reinterpret_cast<const uint16_t *>(pc);
|
|
|
|
}
|
|
|
|
|
2008-11-25 11:07:48 +00:00
|
|
|
|
2009-07-02 19:46:28 +00:00
|
|
|
// A simple abstraction over the backtracking stack used by the interpreter.
|
|
|
|
// This backtracking stack does not grow automatically, but it ensures that the
|
|
|
|
// the memory held by the stack is released or remembered in a cache if the
|
|
|
|
// matching terminates.
|
|
|
|
class BacktrackStack {
|
|
|
|
public:
|
2014-03-24 09:09:49 +00:00
|
|
|
explicit BacktrackStack() {
|
2014-03-14 13:22:14 +00:00
|
|
|
data_ = NewArray<int>(kBacktrackStackSize);
|
2009-07-02 19:46:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
~BacktrackStack() {
|
2014-03-14 13:22:14 +00:00
|
|
|
DeleteArray(data_);
|
2009-07-02 19:46:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int* data() const { return data_; }
|
|
|
|
|
|
|
|
int max_size() const { return kBacktrackStackSize; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
static const int kBacktrackStackSize = 10000;
|
|
|
|
|
|
|
|
int* data_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(BacktrackStack);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-12-09 08:30:49 +00:00
|
|
|
template <typename Char>
|
2011-10-26 06:37:57 +00:00
|
|
|
static RegExpImpl::IrregexpResult RawMatch(Isolate* isolate,
|
|
|
|
const byte* code_base,
|
|
|
|
Vector<const Char> subject,
|
|
|
|
int* registers,
|
|
|
|
int current,
|
|
|
|
uint32_t current_char) {
|
2008-11-25 11:07:48 +00:00
|
|
|
const byte* pc = code_base;
|
2009-07-02 19:46:28 +00:00
|
|
|
// BacktrackStack ensures that the memory allocated for the backtracking stack
|
|
|
|
// is returned to the system or cached if there is no stack being cached at
|
|
|
|
// the moment.
|
2014-03-24 09:09:49 +00:00
|
|
|
BacktrackStack backtrack_stack;
|
2009-07-02 19:46:28 +00:00
|
|
|
int* backtrack_stack_base = backtrack_stack.data();
|
2009-06-30 22:36:35 +00:00
|
|
|
int* backtrack_sp = backtrack_stack_base;
|
2009-07-02 19:46:28 +00:00
|
|
|
int backtrack_stack_space = backtrack_stack.max_size();
|
2008-11-25 11:07:48 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
if (FLAG_trace_regexp_bytecodes) {
|
|
|
|
PrintF("\n\nStart bytecode interpreter\n\n");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
while (true) {
|
2009-01-20 18:50:01 +00:00
|
|
|
int32_t insn = Load32Aligned(pc);
|
|
|
|
switch (insn & BYTECODE_MASK) {
|
2008-11-25 11:07:48 +00:00
|
|
|
BYTECODE(BREAK)
|
|
|
|
UNREACHABLE();
|
2011-10-26 06:37:57 +00:00
|
|
|
return RegExpImpl::RE_FAILURE;
|
2008-11-25 11:07:48 +00:00
|
|
|
BYTECODE(PUSH_CP)
|
|
|
|
if (--backtrack_stack_space < 0) {
|
2011-10-26 06:37:57 +00:00
|
|
|
return RegExpImpl::RE_EXCEPTION;
|
2008-11-25 11:07:48 +00:00
|
|
|
}
|
2009-01-20 18:50:01 +00:00
|
|
|
*backtrack_sp++ = current;
|
2008-11-25 11:07:48 +00:00
|
|
|
pc += BC_PUSH_CP_LENGTH;
|
|
|
|
break;
|
|
|
|
BYTECODE(PUSH_BT)
|
|
|
|
if (--backtrack_stack_space < 0) {
|
2011-10-26 06:37:57 +00:00
|
|
|
return RegExpImpl::RE_EXCEPTION;
|
2008-11-25 11:07:48 +00:00
|
|
|
}
|
2009-01-20 18:50:01 +00:00
|
|
|
*backtrack_sp++ = Load32Aligned(pc + 4);
|
2008-11-25 11:07:48 +00:00
|
|
|
pc += BC_PUSH_BT_LENGTH;
|
|
|
|
break;
|
|
|
|
BYTECODE(PUSH_REGISTER)
|
|
|
|
if (--backtrack_stack_space < 0) {
|
2011-10-26 06:37:57 +00:00
|
|
|
return RegExpImpl::RE_EXCEPTION;
|
2008-11-25 11:07:48 +00:00
|
|
|
}
|
2009-01-20 18:50:01 +00:00
|
|
|
*backtrack_sp++ = registers[insn >> BYTECODE_SHIFT];
|
2008-11-25 11:07:48 +00:00
|
|
|
pc += BC_PUSH_REGISTER_LENGTH;
|
|
|
|
break;
|
|
|
|
BYTECODE(SET_REGISTER)
|
2009-01-20 18:50:01 +00:00
|
|
|
registers[insn >> BYTECODE_SHIFT] = Load32Aligned(pc + 4);
|
2008-11-25 11:07:48 +00:00
|
|
|
pc += BC_SET_REGISTER_LENGTH;
|
|
|
|
break;
|
|
|
|
BYTECODE(ADVANCE_REGISTER)
|
2009-01-20 18:50:01 +00:00
|
|
|
registers[insn >> BYTECODE_SHIFT] += Load32Aligned(pc + 4);
|
2008-11-25 11:07:48 +00:00
|
|
|
pc += BC_ADVANCE_REGISTER_LENGTH;
|
|
|
|
break;
|
|
|
|
BYTECODE(SET_REGISTER_TO_CP)
|
2009-01-20 18:50:01 +00:00
|
|
|
registers[insn >> BYTECODE_SHIFT] = current + Load32Aligned(pc + 4);
|
2008-11-25 11:07:48 +00:00
|
|
|
pc += BC_SET_REGISTER_TO_CP_LENGTH;
|
|
|
|
break;
|
|
|
|
BYTECODE(SET_CP_TO_REGISTER)
|
2009-01-20 18:50:01 +00:00
|
|
|
current = registers[insn >> BYTECODE_SHIFT];
|
2008-11-25 11:07:48 +00:00
|
|
|
pc += BC_SET_CP_TO_REGISTER_LENGTH;
|
|
|
|
break;
|
|
|
|
BYTECODE(SET_REGISTER_TO_SP)
|
2009-11-11 09:50:06 +00:00
|
|
|
registers[insn >> BYTECODE_SHIFT] =
|
|
|
|
static_cast<int>(backtrack_sp - backtrack_stack_base);
|
2008-11-25 11:07:48 +00:00
|
|
|
pc += BC_SET_REGISTER_TO_SP_LENGTH;
|
|
|
|
break;
|
|
|
|
BYTECODE(SET_SP_TO_REGISTER)
|
2009-06-30 22:36:35 +00:00
|
|
|
backtrack_sp = backtrack_stack_base + registers[insn >> BYTECODE_SHIFT];
|
2009-07-02 19:46:28 +00:00
|
|
|
backtrack_stack_space = backtrack_stack.max_size() -
|
2009-11-11 09:50:06 +00:00
|
|
|
static_cast<int>(backtrack_sp - backtrack_stack_base);
|
2008-11-25 11:07:48 +00:00
|
|
|
pc += BC_SET_SP_TO_REGISTER_LENGTH;
|
|
|
|
break;
|
|
|
|
BYTECODE(POP_CP)
|
|
|
|
backtrack_stack_space++;
|
|
|
|
--backtrack_sp;
|
|
|
|
current = *backtrack_sp;
|
|
|
|
pc += BC_POP_CP_LENGTH;
|
|
|
|
break;
|
|
|
|
BYTECODE(POP_BT)
|
|
|
|
backtrack_stack_space++;
|
|
|
|
--backtrack_sp;
|
|
|
|
pc = code_base + *backtrack_sp;
|
|
|
|
break;
|
|
|
|
BYTECODE(POP_REGISTER)
|
|
|
|
backtrack_stack_space++;
|
|
|
|
--backtrack_sp;
|
2009-01-20 18:50:01 +00:00
|
|
|
registers[insn >> BYTECODE_SHIFT] = *backtrack_sp;
|
2008-11-25 11:07:48 +00:00
|
|
|
pc += BC_POP_REGISTER_LENGTH;
|
|
|
|
break;
|
|
|
|
BYTECODE(FAIL)
|
2011-10-26 06:37:57 +00:00
|
|
|
return RegExpImpl::RE_FAILURE;
|
2008-11-25 11:07:48 +00:00
|
|
|
BYTECODE(SUCCEED)
|
2011-10-26 06:37:57 +00:00
|
|
|
return RegExpImpl::RE_SUCCESS;
|
2008-11-25 11:07:48 +00:00
|
|
|
BYTECODE(ADVANCE_CP)
|
2009-01-20 18:50:01 +00:00
|
|
|
current += insn >> BYTECODE_SHIFT;
|
2008-11-25 11:07:48 +00:00
|
|
|
pc += BC_ADVANCE_CP_LENGTH;
|
|
|
|
break;
|
|
|
|
BYTECODE(GOTO)
|
2009-01-20 18:50:01 +00:00
|
|
|
pc = code_base + Load32Aligned(pc + 4);
|
2008-11-25 11:07:48 +00:00
|
|
|
break;
|
2009-02-19 10:03:27 +00:00
|
|
|
BYTECODE(ADVANCE_CP_AND_GOTO)
|
|
|
|
current += insn >> BYTECODE_SHIFT;
|
|
|
|
pc = code_base + Load32Aligned(pc + 4);
|
|
|
|
break;
|
2008-12-08 09:22:12 +00:00
|
|
|
BYTECODE(CHECK_GREEDY)
|
|
|
|
if (current == backtrack_sp[-1]) {
|
|
|
|
backtrack_sp--;
|
|
|
|
backtrack_stack_space++;
|
2009-01-20 18:50:01 +00:00
|
|
|
pc = code_base + Load32Aligned(pc + 4);
|
2008-12-08 09:22:12 +00:00
|
|
|
} else {
|
|
|
|
pc += BC_CHECK_GREEDY_LENGTH;
|
|
|
|
}
|
|
|
|
break;
|
2008-11-25 11:07:48 +00:00
|
|
|
BYTECODE(LOAD_CURRENT_CHAR) {
|
2009-01-20 18:50:01 +00:00
|
|
|
int pos = current + (insn >> BYTECODE_SHIFT);
|
2008-11-25 11:07:48 +00:00
|
|
|
if (pos >= subject.length()) {
|
2009-01-20 18:50:01 +00:00
|
|
|
pc = code_base + Load32Aligned(pc + 4);
|
2008-11-25 11:07:48 +00:00
|
|
|
} else {
|
|
|
|
current_char = subject[pos];
|
|
|
|
pc += BC_LOAD_CURRENT_CHAR_LENGTH;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2008-12-08 09:22:12 +00:00
|
|
|
BYTECODE(LOAD_CURRENT_CHAR_UNCHECKED) {
|
2009-01-20 18:50:01 +00:00
|
|
|
int pos = current + (insn >> BYTECODE_SHIFT);
|
2008-12-08 09:22:12 +00:00
|
|
|
current_char = subject[pos];
|
|
|
|
pc += BC_LOAD_CURRENT_CHAR_UNCHECKED_LENGTH;
|
|
|
|
break;
|
|
|
|
}
|
2008-12-19 12:02:34 +00:00
|
|
|
BYTECODE(LOAD_2_CURRENT_CHARS) {
|
2009-01-20 18:50:01 +00:00
|
|
|
int pos = current + (insn >> BYTECODE_SHIFT);
|
2008-12-19 12:02:34 +00:00
|
|
|
if (pos + 2 > subject.length()) {
|
2009-01-20 18:50:01 +00:00
|
|
|
pc = code_base + Load32Aligned(pc + 4);
|
2008-12-19 12:02:34 +00:00
|
|
|
} else {
|
|
|
|
Char next = subject[pos + 1];
|
|
|
|
current_char =
|
|
|
|
(subject[pos] | (next << (kBitsPerByte * sizeof(Char))));
|
|
|
|
pc += BC_LOAD_2_CURRENT_CHARS_LENGTH;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
BYTECODE(LOAD_2_CURRENT_CHARS_UNCHECKED) {
|
2009-01-20 18:50:01 +00:00
|
|
|
int pos = current + (insn >> BYTECODE_SHIFT);
|
2008-12-19 12:02:34 +00:00
|
|
|
Char next = subject[pos + 1];
|
|
|
|
current_char = (subject[pos] | (next << (kBitsPerByte * sizeof(Char))));
|
|
|
|
pc += BC_LOAD_2_CURRENT_CHARS_UNCHECKED_LENGTH;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
BYTECODE(LOAD_4_CURRENT_CHARS) {
|
|
|
|
ASSERT(sizeof(Char) == 1);
|
2009-01-20 18:50:01 +00:00
|
|
|
int pos = current + (insn >> BYTECODE_SHIFT);
|
2008-12-19 12:02:34 +00:00
|
|
|
if (pos + 4 > subject.length()) {
|
2009-01-20 18:50:01 +00:00
|
|
|
pc = code_base + Load32Aligned(pc + 4);
|
2008-12-19 12:02:34 +00:00
|
|
|
} else {
|
|
|
|
Char next1 = subject[pos + 1];
|
|
|
|
Char next2 = subject[pos + 2];
|
|
|
|
Char next3 = subject[pos + 3];
|
|
|
|
current_char = (subject[pos] |
|
|
|
|
(next1 << 8) |
|
|
|
|
(next2 << 16) |
|
|
|
|
(next3 << 24));
|
|
|
|
pc += BC_LOAD_4_CURRENT_CHARS_LENGTH;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
BYTECODE(LOAD_4_CURRENT_CHARS_UNCHECKED) {
|
|
|
|
ASSERT(sizeof(Char) == 1);
|
2009-01-20 18:50:01 +00:00
|
|
|
int pos = current + (insn >> BYTECODE_SHIFT);
|
2008-12-19 12:02:34 +00:00
|
|
|
Char next1 = subject[pos + 1];
|
|
|
|
Char next2 = subject[pos + 2];
|
|
|
|
Char next3 = subject[pos + 3];
|
|
|
|
current_char = (subject[pos] |
|
|
|
|
(next1 << 8) |
|
|
|
|
(next2 << 16) |
|
|
|
|
(next3 << 24));
|
|
|
|
pc += BC_LOAD_4_CURRENT_CHARS_UNCHECKED_LENGTH;
|
|
|
|
break;
|
|
|
|
}
|
2009-01-20 18:50:01 +00:00
|
|
|
BYTECODE(CHECK_4_CHARS) {
|
|
|
|
uint32_t c = Load32Aligned(pc + 4);
|
|
|
|
if (c == current_char) {
|
|
|
|
pc = code_base + Load32Aligned(pc + 8);
|
|
|
|
} else {
|
|
|
|
pc += BC_CHECK_4_CHARS_LENGTH;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2008-11-25 11:07:48 +00:00
|
|
|
BYTECODE(CHECK_CHAR) {
|
2009-01-20 18:50:01 +00:00
|
|
|
uint32_t c = (insn >> BYTECODE_SHIFT);
|
2008-11-25 11:07:48 +00:00
|
|
|
if (c == current_char) {
|
2009-01-20 18:50:01 +00:00
|
|
|
pc = code_base + Load32Aligned(pc + 4);
|
2008-11-25 11:07:48 +00:00
|
|
|
} else {
|
|
|
|
pc += BC_CHECK_CHAR_LENGTH;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2009-01-20 18:50:01 +00:00
|
|
|
BYTECODE(CHECK_NOT_4_CHARS) {
|
|
|
|
uint32_t c = Load32Aligned(pc + 4);
|
|
|
|
if (c != current_char) {
|
|
|
|
pc = code_base + Load32Aligned(pc + 8);
|
|
|
|
} else {
|
|
|
|
pc += BC_CHECK_NOT_4_CHARS_LENGTH;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2008-11-25 11:07:48 +00:00
|
|
|
BYTECODE(CHECK_NOT_CHAR) {
|
2009-01-20 18:50:01 +00:00
|
|
|
uint32_t c = (insn >> BYTECODE_SHIFT);
|
2008-11-25 11:07:48 +00:00
|
|
|
if (c != current_char) {
|
2009-01-20 18:50:01 +00:00
|
|
|
pc = code_base + Load32Aligned(pc + 4);
|
2008-11-25 11:07:48 +00:00
|
|
|
} else {
|
|
|
|
pc += BC_CHECK_NOT_CHAR_LENGTH;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2009-01-20 18:50:01 +00:00
|
|
|
BYTECODE(AND_CHECK_4_CHARS) {
|
|
|
|
uint32_t c = Load32Aligned(pc + 4);
|
|
|
|
if (c == (current_char & Load32Aligned(pc + 8))) {
|
|
|
|
pc = code_base + Load32Aligned(pc + 12);
|
|
|
|
} else {
|
|
|
|
pc += BC_AND_CHECK_4_CHARS_LENGTH;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2008-12-19 12:02:34 +00:00
|
|
|
BYTECODE(AND_CHECK_CHAR) {
|
2009-01-20 18:50:01 +00:00
|
|
|
uint32_t c = (insn >> BYTECODE_SHIFT);
|
|
|
|
if (c == (current_char & Load32Aligned(pc + 4))) {
|
|
|
|
pc = code_base + Load32Aligned(pc + 8);
|
2008-11-25 11:07:48 +00:00
|
|
|
} else {
|
2008-12-19 12:02:34 +00:00
|
|
|
pc += BC_AND_CHECK_CHAR_LENGTH;
|
2008-11-25 11:07:48 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2009-01-20 18:50:01 +00:00
|
|
|
BYTECODE(AND_CHECK_NOT_4_CHARS) {
|
|
|
|
uint32_t c = Load32Aligned(pc + 4);
|
|
|
|
if (c != (current_char & Load32Aligned(pc + 8))) {
|
|
|
|
pc = code_base + Load32Aligned(pc + 12);
|
|
|
|
} else {
|
|
|
|
pc += BC_AND_CHECK_NOT_4_CHARS_LENGTH;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2008-12-19 12:02:34 +00:00
|
|
|
BYTECODE(AND_CHECK_NOT_CHAR) {
|
2009-01-20 18:50:01 +00:00
|
|
|
uint32_t c = (insn >> BYTECODE_SHIFT);
|
|
|
|
if (c != (current_char & Load32Aligned(pc + 4))) {
|
|
|
|
pc = code_base + Load32Aligned(pc + 8);
|
2008-12-19 12:02:34 +00:00
|
|
|
} else {
|
|
|
|
pc += BC_AND_CHECK_NOT_CHAR_LENGTH;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
BYTECODE(MINUS_AND_CHECK_NOT_CHAR) {
|
2009-01-20 18:50:01 +00:00
|
|
|
uint32_t c = (insn >> BYTECODE_SHIFT);
|
|
|
|
uint32_t minus = Load16Aligned(pc + 4);
|
|
|
|
uint32_t mask = Load16Aligned(pc + 6);
|
2008-12-19 12:02:34 +00:00
|
|
|
if (c != ((current_char - minus) & mask)) {
|
2009-01-20 18:50:01 +00:00
|
|
|
pc = code_base + Load32Aligned(pc + 8);
|
2008-11-25 11:07:48 +00:00
|
|
|
} else {
|
2008-12-19 12:02:34 +00:00
|
|
|
pc += BC_MINUS_AND_CHECK_NOT_CHAR_LENGTH;
|
2008-11-25 11:07:48 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2012-03-30 07:43:48 +00:00
|
|
|
BYTECODE(CHECK_CHAR_IN_RANGE) {
|
|
|
|
uint32_t from = Load16Aligned(pc + 4);
|
|
|
|
uint32_t to = Load16Aligned(pc + 6);
|
|
|
|
if (from <= current_char && current_char <= to) {
|
|
|
|
pc = code_base + Load32Aligned(pc + 8);
|
|
|
|
} else {
|
|
|
|
pc += BC_CHECK_CHAR_IN_RANGE_LENGTH;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
BYTECODE(CHECK_CHAR_NOT_IN_RANGE) {
|
|
|
|
uint32_t from = Load16Aligned(pc + 4);
|
|
|
|
uint32_t to = Load16Aligned(pc + 6);
|
|
|
|
if (from > current_char || current_char > to) {
|
|
|
|
pc = code_base + Load32Aligned(pc + 8);
|
|
|
|
} else {
|
|
|
|
pc += BC_CHECK_CHAR_NOT_IN_RANGE_LENGTH;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
BYTECODE(CHECK_BIT_IN_TABLE) {
|
|
|
|
int mask = RegExpMacroAssembler::kTableMask;
|
|
|
|
byte b = pc[8 + ((current_char & mask) >> kBitsPerByteLog2)];
|
|
|
|
int bit = (current_char & (kBitsPerByte - 1));
|
|
|
|
if ((b & (1 << bit)) != 0) {
|
|
|
|
pc = code_base + Load32Aligned(pc + 4);
|
|
|
|
} else {
|
|
|
|
pc += BC_CHECK_BIT_IN_TABLE_LENGTH;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2008-11-25 11:07:48 +00:00
|
|
|
BYTECODE(CHECK_LT) {
|
2009-01-20 18:50:01 +00:00
|
|
|
uint32_t limit = (insn >> BYTECODE_SHIFT);
|
2008-11-25 11:07:48 +00:00
|
|
|
if (current_char < limit) {
|
2009-01-20 18:50:01 +00:00
|
|
|
pc = code_base + Load32Aligned(pc + 4);
|
2008-11-25 11:07:48 +00:00
|
|
|
} else {
|
|
|
|
pc += BC_CHECK_LT_LENGTH;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
BYTECODE(CHECK_GT) {
|
2009-01-20 18:50:01 +00:00
|
|
|
uint32_t limit = (insn >> BYTECODE_SHIFT);
|
2008-11-25 11:07:48 +00:00
|
|
|
if (current_char > limit) {
|
2009-01-20 18:50:01 +00:00
|
|
|
pc = code_base + Load32Aligned(pc + 4);
|
2008-11-25 11:07:48 +00:00
|
|
|
} else {
|
|
|
|
pc += BC_CHECK_GT_LENGTH;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
BYTECODE(CHECK_REGISTER_LT)
|
2009-01-20 18:50:01 +00:00
|
|
|
if (registers[insn >> BYTECODE_SHIFT] < Load32Aligned(pc + 4)) {
|
|
|
|
pc = code_base + Load32Aligned(pc + 8);
|
2008-11-25 11:07:48 +00:00
|
|
|
} else {
|
|
|
|
pc += BC_CHECK_REGISTER_LT_LENGTH;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
BYTECODE(CHECK_REGISTER_GE)
|
2009-01-20 18:50:01 +00:00
|
|
|
if (registers[insn >> BYTECODE_SHIFT] >= Load32Aligned(pc + 4)) {
|
|
|
|
pc = code_base + Load32Aligned(pc + 8);
|
2008-11-25 11:07:48 +00:00
|
|
|
} else {
|
|
|
|
pc += BC_CHECK_REGISTER_GE_LENGTH;
|
|
|
|
}
|
|
|
|
break;
|
2009-01-08 12:40:47 +00:00
|
|
|
BYTECODE(CHECK_REGISTER_EQ_POS)
|
2009-01-20 18:50:01 +00:00
|
|
|
if (registers[insn >> BYTECODE_SHIFT] == current) {
|
|
|
|
pc = code_base + Load32Aligned(pc + 4);
|
2009-01-08 12:40:47 +00:00
|
|
|
} else {
|
|
|
|
pc += BC_CHECK_REGISTER_EQ_POS_LENGTH;
|
|
|
|
}
|
|
|
|
break;
|
2008-11-28 08:53:53 +00:00
|
|
|
BYTECODE(CHECK_NOT_REGS_EQUAL)
|
2009-01-20 18:58:30 +00:00
|
|
|
if (registers[insn >> BYTECODE_SHIFT] ==
|
|
|
|
registers[Load32Aligned(pc + 4)]) {
|
2008-11-28 08:53:53 +00:00
|
|
|
pc += BC_CHECK_NOT_REGS_EQUAL_LENGTH;
|
|
|
|
} else {
|
2009-01-20 18:50:01 +00:00
|
|
|
pc = code_base + Load32Aligned(pc + 8);
|
2008-11-28 08:53:53 +00:00
|
|
|
}
|
|
|
|
break;
|
2008-11-25 11:07:48 +00:00
|
|
|
BYTECODE(CHECK_NOT_BACK_REF) {
|
2009-01-20 18:50:01 +00:00
|
|
|
int from = registers[insn >> BYTECODE_SHIFT];
|
|
|
|
int len = registers[(insn >> BYTECODE_SHIFT) + 1] - from;
|
2008-12-03 13:24:34 +00:00
|
|
|
if (from < 0 || len <= 0) {
|
|
|
|
pc += BC_CHECK_NOT_BACK_REF_LENGTH;
|
|
|
|
break;
|
|
|
|
}
|
2008-11-25 11:07:48 +00:00
|
|
|
if (current + len > subject.length()) {
|
2009-01-20 18:50:01 +00:00
|
|
|
pc = code_base + Load32Aligned(pc + 4);
|
2008-11-25 11:07:48 +00:00
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
if (subject[from + i] != subject[current + i]) {
|
2009-01-20 18:50:01 +00:00
|
|
|
pc = code_base + Load32Aligned(pc + 4);
|
2008-11-25 11:07:48 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i < len) break;
|
|
|
|
current += len;
|
|
|
|
}
|
|
|
|
pc += BC_CHECK_NOT_BACK_REF_LENGTH;
|
|
|
|
break;
|
|
|
|
}
|
2008-11-25 11:39:20 +00:00
|
|
|
BYTECODE(CHECK_NOT_BACK_REF_NO_CASE) {
|
2009-01-20 18:50:01 +00:00
|
|
|
int from = registers[insn >> BYTECODE_SHIFT];
|
|
|
|
int len = registers[(insn >> BYTECODE_SHIFT) + 1] - from;
|
2008-12-03 13:24:34 +00:00
|
|
|
if (from < 0 || len <= 0) {
|
|
|
|
pc += BC_CHECK_NOT_BACK_REF_NO_CASE_LENGTH;
|
|
|
|
break;
|
|
|
|
}
|
2008-11-25 11:39:20 +00:00
|
|
|
if (current + len > subject.length()) {
|
2009-01-20 18:50:01 +00:00
|
|
|
pc = code_base + Load32Aligned(pc + 4);
|
2008-11-25 11:39:20 +00:00
|
|
|
break;
|
|
|
|
} else {
|
2011-03-18 20:35:07 +00:00
|
|
|
if (BackRefMatchesNoCase(isolate->interp_canonicalize_mapping(),
|
|
|
|
from, current, len, subject)) {
|
2008-12-08 09:43:34 +00:00
|
|
|
current += len;
|
2008-11-25 11:39:20 +00:00
|
|
|
pc += BC_CHECK_NOT_BACK_REF_NO_CASE_LENGTH;
|
|
|
|
} else {
|
2009-01-20 18:50:01 +00:00
|
|
|
pc = code_base + Load32Aligned(pc + 4);
|
2008-11-25 11:39:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2009-01-19 18:56:47 +00:00
|
|
|
BYTECODE(CHECK_AT_START)
|
|
|
|
if (current == 0) {
|
2009-01-20 18:50:01 +00:00
|
|
|
pc = code_base + Load32Aligned(pc + 4);
|
2009-01-19 18:56:47 +00:00
|
|
|
} else {
|
|
|
|
pc += BC_CHECK_AT_START_LENGTH;
|
|
|
|
}
|
|
|
|
break;
|
2008-11-28 11:48:22 +00:00
|
|
|
BYTECODE(CHECK_NOT_AT_START)
|
|
|
|
if (current == 0) {
|
|
|
|
pc += BC_CHECK_NOT_AT_START_LENGTH;
|
|
|
|
} else {
|
2009-01-20 18:50:01 +00:00
|
|
|
pc = code_base + Load32Aligned(pc + 4);
|
2008-11-28 11:48:22 +00:00
|
|
|
}
|
|
|
|
break;
|
2010-10-19 14:00:01 +00:00
|
|
|
BYTECODE(SET_CURRENT_POSITION_FROM_END) {
|
|
|
|
int by = static_cast<uint32_t>(insn) >> BYTECODE_SHIFT;
|
|
|
|
if (subject.length() - current > by) {
|
|
|
|
current = subject.length() - by;
|
|
|
|
current_char = subject[current - 1];
|
|
|
|
}
|
|
|
|
pc += BC_SET_CURRENT_POSITION_FROM_END_LENGTH;
|
|
|
|
break;
|
|
|
|
}
|
2008-11-25 11:07:48 +00:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-26 06:37:57 +00:00
|
|
|
RegExpImpl::IrregexpResult IrregexpInterpreter::Match(
|
|
|
|
Isolate* isolate,
|
|
|
|
Handle<ByteArray> code_array,
|
|
|
|
Handle<String> subject,
|
|
|
|
int* registers,
|
|
|
|
int start_position) {
|
2009-03-17 09:33:06 +00:00
|
|
|
ASSERT(subject->IsFlat());
|
2008-11-25 11:07:48 +00:00
|
|
|
|
2013-06-03 15:32:22 +00:00
|
|
|
DisallowHeapAllocation no_gc;
|
2008-11-25 11:07:48 +00:00
|
|
|
const byte* code_base = code_array->GetDataStartAddress();
|
2008-11-28 10:37:06 +00:00
|
|
|
uc16 previous_char = '\n';
|
2011-08-23 13:23:30 +00:00
|
|
|
String::FlatContent subject_content = subject->GetFlatContent();
|
2011-08-23 12:22:12 +00:00
|
|
|
if (subject_content.IsAscii()) {
|
2013-01-09 15:47:53 +00:00
|
|
|
Vector<const uint8_t> subject_vector = subject_content.ToOneByteVector();
|
2008-12-09 08:30:49 +00:00
|
|
|
if (start_position != 0) previous_char = subject_vector[start_position - 1];
|
2011-03-18 20:35:07 +00:00
|
|
|
return RawMatch(isolate,
|
|
|
|
code_base,
|
2008-12-09 08:30:49 +00:00
|
|
|
subject_vector,
|
|
|
|
registers,
|
|
|
|
start_position,
|
|
|
|
previous_char);
|
|
|
|
} else {
|
2011-08-23 12:22:12 +00:00
|
|
|
ASSERT(subject_content.IsTwoByte());
|
|
|
|
Vector<const uc16> subject_vector = subject_content.ToUC16Vector();
|
2008-12-09 08:30:49 +00:00
|
|
|
if (start_position != 0) previous_char = subject_vector[start_position - 1];
|
2011-03-18 20:35:07 +00:00
|
|
|
return RawMatch(isolate,
|
|
|
|
code_base,
|
2008-12-09 08:30:49 +00:00
|
|
|
subject_vector,
|
|
|
|
registers,
|
|
|
|
start_position,
|
|
|
|
previous_char);
|
|
|
|
}
|
2008-11-25 11:07:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} } // namespace v8::internal
|