Commit 50e042df authored by lrn@chromium.org's avatar lrn@chromium.org

All RegExp data are set on a single FixedArray instead of nesting them three deep.


git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@1398 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent 4678719d
...@@ -826,12 +826,13 @@ Handle<Map> Factory::ObjectLiteralMapFromCache(Handle<Context> context, ...@@ -826,12 +826,13 @@ Handle<Map> Factory::ObjectLiteralMapFromCache(Handle<Context> context,
} }
void Factory::SetRegExpData(Handle<JSRegExp> regexp, void Factory::SetRegExpAtomData(Handle<JSRegExp> regexp,
JSRegExp::Type type, JSRegExp::Type type,
Handle<String> source, Handle<String> source,
JSRegExp::Flags flags, JSRegExp::Flags flags,
Handle<Object> data) { Handle<Object> data) {
Handle<FixedArray> store = NewFixedArray(JSRegExp::kDataSize); Handle<FixedArray> store = NewFixedArray(JSRegExp::kAtomDataSize);
store->set(JSRegExp::kTagIndex, Smi::FromInt(type)); store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
store->set(JSRegExp::kSourceIndex, *source); store->set(JSRegExp::kSourceIndex, *source);
store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value())); store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
...@@ -839,6 +840,25 @@ void Factory::SetRegExpData(Handle<JSRegExp> regexp, ...@@ -839,6 +840,25 @@ void Factory::SetRegExpData(Handle<JSRegExp> regexp,
regexp->set_data(*store); regexp->set_data(*store);
} }
void Factory::SetRegExpIrregexpData(Handle<JSRegExp> regexp,
JSRegExp::Type type,
Handle<String> source,
JSRegExp::Flags flags,
int capture_count) {
Handle<FixedArray> store = NewFixedArray(JSRegExp::kIrregexpDataSize);
store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
store->set(JSRegExp::kSourceIndex, *source);
store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
store->set(JSRegExp::kIrregexpASCIICodeIndex, Heap::the_hole_value());
store->set(JSRegExp::kIrregexpUC16CodeIndex, Heap::the_hole_value());
store->set(JSRegExp::kIrregexpMaxRegisterCountIndex, Smi::FromInt(0));
store->set(JSRegExp::kIrregexpCaptureCountIndex,
Smi::FromInt(capture_count));
regexp->set_data(*store);
}
void Factory::ConfigureInstance(Handle<FunctionTemplateInfo> desc, void Factory::ConfigureInstance(Handle<FunctionTemplateInfo> desc,
Handle<JSObject> instance, Handle<JSObject> instance,
......
...@@ -316,12 +316,20 @@ class Factory : public AllStatic { ...@@ -316,12 +316,20 @@ class Factory : public AllStatic {
Handle<FixedArray> keys); Handle<FixedArray> keys);
// Creates a new FixedArray that holds the data associated with the // Creates a new FixedArray that holds the data associated with the
// regexp and stores it in the regexp. // atom regexp and stores it in the regexp.
static void SetRegExpData(Handle<JSRegExp> regexp, static void SetRegExpAtomData(Handle<JSRegExp> regexp,
JSRegExp::Type type, JSRegExp::Type type,
Handle<String> source, Handle<String> source,
JSRegExp::Flags flags, JSRegExp::Flags flags,
Handle<Object> data); Handle<Object> match_pattern);
// Creates a new FixedArray that holds the data associated with the
// irregexp regexp and stores it in the regexp.
static void SetRegExpIrregexpData(Handle<JSRegExp> regexp,
JSRegExp::Type type,
Handle<String> source,
JSRegExp::Flags flags,
int capture_count);
private: private:
static Handle<JSFunction> NewFunctionHelper(Handle<String> name, static Handle<JSFunction> NewFunctionHelper(Handle<String> name,
......
...@@ -213,8 +213,8 @@ Handle<Object> RegExpImpl::Compile(Handle<JSRegExp> re, ...@@ -213,8 +213,8 @@ Handle<Object> RegExpImpl::Compile(Handle<JSRegExp> re,
Handle<Object> result; Handle<Object> result;
if (in_cache) { if (in_cache) {
re->set_data(*cached); re->set_data(*cached);
result = re; return re;
} else { }
FlattenString(pattern); FlattenString(pattern);
ZoneScope zone_scope(DELETE_ON_EXIT); ZoneScope zone_scope(DELETE_ON_EXIT);
RegExpCompileData parse_result; RegExpCompileData parse_result;
...@@ -230,27 +230,24 @@ Handle<Object> RegExpImpl::Compile(Handle<JSRegExp> re, ...@@ -230,27 +230,24 @@ Handle<Object> RegExpImpl::Compile(Handle<JSRegExp> re,
if (parse_result.simple && !flags.is_ignore_case()) { if (parse_result.simple && !flags.is_ignore_case()) {
// Parse-tree is a single atom that is equal to the pattern. // Parse-tree is a single atom that is equal to the pattern.
result = AtomCompile(re, pattern, flags, pattern); AtomCompile(re, pattern, flags, pattern);
} else if (parse_result.tree->IsAtom() && } else if (parse_result.tree->IsAtom() &&
!flags.is_ignore_case() && !flags.is_ignore_case() &&
parse_result.capture_count == 0) { parse_result.capture_count == 0) {
RegExpAtom* atom = parse_result.tree->AsAtom(); RegExpAtom* atom = parse_result.tree->AsAtom();
Vector<const uc16> atom_pattern = atom->data(); Vector<const uc16> atom_pattern = atom->data();
Handle<String> atom_string = Factory::NewStringFromTwoByte(atom_pattern); Handle<String> atom_string = Factory::NewStringFromTwoByte(atom_pattern);
result = AtomCompile(re, pattern, flags, atom_string); AtomCompile(re, pattern, flags, atom_string);
} else { } else {
result = IrregexpPrepare(re, pattern, flags); IrregexpPrepare(re, pattern, flags, parse_result.capture_count);
} }
Object* data = re->data(); ASSERT(re->data()->IsFixedArray());
if (data->IsFixedArray()) { // Compilation succeeded so the data is set on the regexp
// If compilation succeeded then the data is set on the regexp
// and we can store it in the cache. // and we can store it in the cache.
Handle<FixedArray> data(FixedArray::cast(re->data())); Handle<FixedArray> data(FixedArray::cast(re->data()));
CompilationCache::PutRegExp(pattern, flags, data); CompilationCache::PutRegExp(pattern, flags, data);
}
}
return result; return re;
} }
...@@ -296,12 +293,15 @@ Handle<Object> RegExpImpl::ExecGlobal(Handle<JSRegExp> regexp, ...@@ -296,12 +293,15 @@ Handle<Object> RegExpImpl::ExecGlobal(Handle<JSRegExp> regexp,
// RegExp Atom implementation: Simple string search using indexOf. // RegExp Atom implementation: Simple string search using indexOf.
Handle<Object> RegExpImpl::AtomCompile(Handle<JSRegExp> re, void RegExpImpl::AtomCompile(Handle<JSRegExp> re,
Handle<String> pattern, Handle<String> pattern,
JSRegExp::Flags flags, JSRegExp::Flags flags,
Handle<String> match_pattern) { Handle<String> match_pattern) {
Factory::SetRegExpData(re, JSRegExp::ATOM, pattern, flags, match_pattern); Factory::SetRegExpAtomData(re,
return re; JSRegExp::ATOM,
pattern,
flags,
match_pattern);
} }
...@@ -386,23 +386,29 @@ Handle<Object> RegExpImpl::AtomExecGlobal(Handle<JSRegExp> re, ...@@ -386,23 +386,29 @@ Handle<Object> RegExpImpl::AtomExecGlobal(Handle<JSRegExp> re,
// Irregexp implementation. // Irregexp implementation.
// Retrieves a compiled version of the regexp for either ASCII or non-ASCII // Ensures that the regexp object contains a compiled version of the
// strings. If the compiled version doesn't already exist, it is compiled // source for either ASCII or non-ASCII strings.
// If the compiled version doesn't already exist, it is compiled
// from the source pattern. // from the source pattern.
// Irregexp is not feature complete yet. If there is something in the // If compilation fails, an exception is thrown and this function
// regexp that the compiler cannot currently handle, an empty // returns false.
// handle is returned, but no exception is thrown. bool RegExpImpl::EnsureCompiledIrregexp(Handle<JSRegExp> re,
static Handle<FixedArray> GetCompiledIrregexp(Handle<JSRegExp> re,
bool is_ascii) { bool is_ascii) {
ASSERT(re->DataAt(JSRegExp::kIrregexpDataIndex)->IsFixedArray()); int index;
Handle<FixedArray> alternatives( if (is_ascii) {
FixedArray::cast(re->DataAt(JSRegExp::kIrregexpDataIndex))); index = JSRegExp::kIrregexpASCIICodeIndex;
ASSERT_EQ(2, alternatives->length()); } else {
index = JSRegExp::kIrregexpUC16CodeIndex;
int index = is_ascii ? 0 : 1; }
Object* entry = alternatives->get(index); Object* entry = re->DataAt(index);
if (!entry->IsNull()) { if (!entry->IsTheHole()) {
return Handle<FixedArray>(FixedArray::cast(entry)); // A value has already been compiled.
if (entry->IsJSObject()) {
// If it's a JS value, it's an error.
Top::Throw(entry);
return false;
}
return true;
} }
// Compile the RegExp. // Compile the RegExp.
...@@ -424,54 +430,101 @@ static Handle<FixedArray> GetCompiledIrregexp(Handle<JSRegExp> re, ...@@ -424,54 +430,101 @@ static Handle<FixedArray> GetCompiledIrregexp(Handle<JSRegExp> re,
pattern, pattern,
compile_data.error, compile_data.error,
"malformed_regexp"); "malformed_regexp");
return Handle<FixedArray>::null(); return false;
} }
Handle<FixedArray> compiled_entry = RegExpEngine::CompilationResult result =
RegExpEngine::Compile(&compile_data, RegExpEngine::Compile(&compile_data,
flags.is_ignore_case(), flags.is_ignore_case(),
flags.is_multiline(), flags.is_multiline(),
pattern, pattern,
is_ascii); is_ascii);
if (!compiled_entry.is_null()) { if (result.error_message != NULL) {
alternatives->set(index, *compiled_entry); // Unable to compile regexp.
Handle<JSArray> array = Factory::NewJSArray(2);
SetElement(array, 0, pattern);
SetElement(array,
1,
Factory::NewStringFromUtf8(CStrVector(result.error_message)));
Handle<Object> regexp_err =
Factory::NewSyntaxError("malformed_regexp", array);
Top::Throw(*regexp_err);
re->SetDataAt(index, *regexp_err);
return false;
} }
return compiled_entry;
Handle<FixedArray> data(FixedArray::cast(re->data()));
data->set(index, result.code);
int register_max = IrregexpMaxRegisterCount(data);
if (result.num_registers > register_max) {
SetIrregexpMaxRegisterCount(data, result.num_registers);
}
return true;
} }
int RegExpImpl::IrregexpNumberOfCaptures(Handle<FixedArray> irre) { int RegExpImpl::IrregexpMaxRegisterCount(Handle<FixedArray> re) {
return Smi::cast(irre->get(kIrregexpNumberOfCapturesIndex))->value(); return Smi::cast(
re->get(JSRegExp::kIrregexpMaxRegisterCountIndex))->value();
} }
int RegExpImpl::IrregexpNumberOfRegisters(Handle<FixedArray> irre) { void RegExpImpl::SetIrregexpMaxRegisterCount(Handle<FixedArray> re, int value) {
return Smi::cast(irre->get(kIrregexpNumberOfRegistersIndex))->value(); re->set(JSRegExp::kIrregexpMaxRegisterCountIndex,
Smi::FromInt(value));
} }
Handle<ByteArray> RegExpImpl::IrregexpByteCode(Handle<FixedArray> irre) { int RegExpImpl::IrregexpNumberOfCaptures(Handle<FixedArray> re) {
ASSERT(Smi::cast(irre->get(kIrregexpImplementationIndex))->value() return Smi::cast(
== RegExpMacroAssembler::kBytecodeImplementation); re->get(JSRegExp::kIrregexpCaptureCountIndex))->value();
return Handle<ByteArray>(ByteArray::cast(irre->get(kIrregexpCodeIndex)));
} }
Handle<Code> RegExpImpl::IrregexpNativeCode(Handle<FixedArray> irre) { int RegExpImpl::IrregexpNumberOfRegisters(Handle<FixedArray> re) {
ASSERT(Smi::cast(irre->get(kIrregexpImplementationIndex))->value() return Smi::cast(
!= RegExpMacroAssembler::kBytecodeImplementation); re->get(JSRegExp::kIrregexpMaxRegisterCountIndex))->value();
return Handle<Code>(Code::cast(irre->get(kIrregexpCodeIndex)));
} }
Handle<Object>RegExpImpl::IrregexpPrepare(Handle<JSRegExp> re, Handle<ByteArray> RegExpImpl::IrregexpByteCode(Handle<FixedArray> re,
bool is_ascii) {
int index;
if (is_ascii) {
index = JSRegExp::kIrregexpASCIICodeIndex;
} else {
index = JSRegExp::kIrregexpUC16CodeIndex;
}
Object* value = re->get(index);
ASSERT(value->IsByteArray());
return Handle<ByteArray>(ByteArray::cast(value));
}
Handle<Code> RegExpImpl::IrregexpNativeCode(Handle<FixedArray> re,
bool is_ascii) {
int index;
if (is_ascii) {
index = JSRegExp::kIrregexpASCIICodeIndex;
} else {
index = JSRegExp::kIrregexpUC16CodeIndex;
}
Object* value = re->get(index);
ASSERT(value->IsCode());
return Handle<Code>(Code::cast(value));
}
void RegExpImpl::IrregexpPrepare(Handle<JSRegExp> re,
Handle<String> pattern, Handle<String> pattern,
JSRegExp::Flags flags) { JSRegExp::Flags flags,
// Make space for ASCII and UC16 versions. int capture_count) {
Handle<FixedArray> alternatives = Factory::NewFixedArray(2); // Initialize compiled code entries to null.
alternatives->set_null(0); Factory::SetRegExpIrregexpData(re,
alternatives->set_null(1); JSRegExp::IRREGEXP,
Factory::SetRegExpData(re, JSRegExp::IRREGEXP, pattern, flags, alternatives); pattern,
return re; flags,
capture_count);
} }
...@@ -480,18 +533,16 @@ Handle<Object> RegExpImpl::IrregexpExec(Handle<JSRegExp> regexp, ...@@ -480,18 +533,16 @@ Handle<Object> RegExpImpl::IrregexpExec(Handle<JSRegExp> regexp,
int index, int index,
Handle<JSArray> last_match_info) { Handle<JSArray> last_match_info) {
ASSERT_EQ(regexp->TypeTag(), JSRegExp::IRREGEXP); ASSERT_EQ(regexp->TypeTag(), JSRegExp::IRREGEXP);
ASSERT(regexp->DataAt(JSRegExp::kIrregexpDataIndex)->IsFixedArray());
bool is_ascii = StringShape(*subject).IsAsciiRepresentation(); bool is_ascii = StringShape(*subject).IsAsciiRepresentation();
Handle<FixedArray> irregexp = GetCompiledIrregexp(regexp, is_ascii); if (!EnsureCompiledIrregexp(regexp, is_ascii)) {
if (irregexp.is_null()) {
// We can't handle the RegExp with IRRegExp.
return Handle<Object>::null(); return Handle<Object>::null();
} }
// Prepare space for the return values. // Prepare space for the return values.
Handle<FixedArray> re_data(FixedArray::cast(regexp->data()));
int number_of_capture_registers = int number_of_capture_registers =
(IrregexpNumberOfCaptures(irregexp) + 1) * 2; (IrregexpNumberOfCaptures(re_data) + 1) * 2;
OffsetsVector offsets(number_of_capture_registers); OffsetsVector offsets(number_of_capture_registers);
int previous_index = index; int previous_index = index;
...@@ -510,7 +561,7 @@ Handle<Object> RegExpImpl::IrregexpExec(Handle<JSRegExp> regexp, ...@@ -510,7 +561,7 @@ Handle<Object> RegExpImpl::IrregexpExec(Handle<JSRegExp> regexp,
last_match_info->EnsureSize(number_of_capture_registers + kLastMatchOverhead); last_match_info->EnsureSize(number_of_capture_registers + kLastMatchOverhead);
return IrregexpExecOnce(irregexp, return IrregexpExecOnce(re_data,
number_of_capture_registers, number_of_capture_registers,
last_match_info, last_match_info,
subject, subject,
...@@ -524,10 +575,10 @@ Handle<Object> RegExpImpl::IrregexpExecGlobal(Handle<JSRegExp> regexp, ...@@ -524,10 +575,10 @@ Handle<Object> RegExpImpl::IrregexpExecGlobal(Handle<JSRegExp> regexp,
Handle<String> subject, Handle<String> subject,
Handle<JSArray> last_match_info) { Handle<JSArray> last_match_info) {
ASSERT_EQ(regexp->TypeTag(), JSRegExp::IRREGEXP); ASSERT_EQ(regexp->TypeTag(), JSRegExp::IRREGEXP);
Handle<FixedArray> irregexp(FixedArray::cast(regexp->data()));
bool is_ascii = StringShape(*subject).IsAsciiRepresentation(); bool is_ascii = StringShape(*subject).IsAsciiRepresentation();
Handle<FixedArray> irregexp = GetCompiledIrregexp(regexp, is_ascii); if (!EnsureCompiledIrregexp(regexp, is_ascii)) {
if (irregexp.is_null()) {
return Handle<Object>::null(); return Handle<Object>::null();
} }
...@@ -605,24 +656,21 @@ Handle<Object> RegExpImpl::IrregexpExecGlobal(Handle<JSRegExp> regexp, ...@@ -605,24 +656,21 @@ Handle<Object> RegExpImpl::IrregexpExecGlobal(Handle<JSRegExp> regexp,
} }
Handle<Object> RegExpImpl::IrregexpExecOnce(Handle<FixedArray> irregexp, Handle<Object> RegExpImpl::IrregexpExecOnce(Handle<FixedArray> regexp,
int number_of_capture_registers, int number_of_capture_registers,
Handle<JSArray> last_match_info, Handle<JSArray> last_match_info,
Handle<String> subject, Handle<String> subject,
int previous_index, int previous_index,
int* offsets_vector, int* offsets_vector,
int offsets_vector_length) { int offsets_vector_length) {
ASSERT(subject->IsFlat(StringShape(*subject))); StringShape shape(*subject);
ASSERT(subject->IsFlat(shape));
bool is_ascii = shape.IsAsciiRepresentation();
bool rc; bool rc;
int tag = Smi::cast(irregexp->get(kIrregexpImplementationIndex))->value(); if (FLAG_regexp_native) {
switch (tag) {
case RegExpMacroAssembler::kIA32Implementation: {
#ifndef ARM #ifndef ARM
Handle<Code> code = IrregexpNativeCode(irregexp); Handle<Code> code(IrregexpNativeCode(regexp, is_ascii));
StringShape shape(*subject);
// Character offsets into string. // Character offsets into string.
int start_offset = previous_index; int start_offset = previous_index;
...@@ -689,51 +737,43 @@ Handle<Object> RegExpImpl::IrregexpExecOnce(Handle<FixedArray> irregexp, ...@@ -689,51 +737,43 @@ Handle<Object> RegExpImpl::IrregexpExecOnce(Handle<FixedArray> irregexp,
} }
} }
} }
break; } else {
#else #else
UNIMPLEMENTED(); // Unimplemented on ARM, fall through to bytecode.
rc = false;
break;
#endif
} }
case RegExpMacroAssembler::kBytecodeImplementation: { {
#endif
for (int i = number_of_capture_registers - 1; i >= 0; i--) { for (int i = number_of_capture_registers - 1; i >= 0; i--) {
offsets_vector[i] = -1; offsets_vector[i] = -1;
} }
Handle<ByteArray> byte_codes = IrregexpByteCode(irregexp); Handle<ByteArray> byte_codes = IrregexpByteCode(regexp, is_ascii);
rc = IrregexpInterpreter::Match(byte_codes, rc = IrregexpInterpreter::Match(byte_codes,
subject, subject,
offsets_vector, offsets_vector,
previous_index); previous_index);
break;
}
case RegExpMacroAssembler::kARMImplementation:
default:
UNREACHABLE();
rc = false;
break;
} }
if (!rc) { if (!rc) {
return Factory::null_value(); return Factory::null_value();
} }
Handle<FixedArray> array(last_match_info->elements()); FixedArray* array = last_match_info->elements();
ASSERT(array->length() >= number_of_capture_registers + kLastMatchOverhead);
// The captures come in (start, end+1) pairs. // The captures come in (start, end+1) pairs.
for (int i = 0; i < number_of_capture_registers; i += 2) { for (int i = 0; i < number_of_capture_registers; i += 2) {
SetCapture(*array, i, offsets_vector[i]); SetCapture(array, i, offsets_vector[i]);
SetCapture(*array, i + 1, offsets_vector[i + 1]); SetCapture(array, i + 1, offsets_vector[i + 1]);
} }
SetLastCaptureCount(*array, number_of_capture_registers); SetLastCaptureCount(array, number_of_capture_registers);
SetLastSubject(*array, *subject); SetLastSubject(array, *subject);
SetLastInput(*array, *subject); SetLastInput(array, *subject);
return last_match_info; return last_match_info;
} }
// ------------------------------------------------------------------- // -------------------------------------------------------------------
// Implmentation of the Irregexp regular expression engine. // Implementation of the Irregexp regular expression engine.
// //
// The Irregexp regular expression engine is intended to be a complete // The Irregexp regular expression engine is intended to be a complete
// implementation of ECMAScript regular expressions. It generates either // implementation of ECMAScript regular expressions. It generates either
...@@ -950,7 +990,7 @@ class RegExpCompiler { ...@@ -950,7 +990,7 @@ class RegExpCompiler {
return next_register_++; return next_register_++;
} }
Handle<FixedArray> Assemble(RegExpMacroAssembler* assembler, RegExpEngine::CompilationResult Assemble(RegExpMacroAssembler* assembler,
RegExpNode* start, RegExpNode* start,
int capture_count, int capture_count,
Handle<String> pattern); Handle<String> pattern);
...@@ -998,15 +1038,8 @@ class RecursionCheck { ...@@ -998,15 +1038,8 @@ class RecursionCheck {
}; };
static Handle<FixedArray> IrregexpRegExpTooBig(Handle<String> pattern) { static RegExpEngine::CompilationResult IrregexpRegExpTooBig() {
Handle<JSArray> array = Factory::NewJSArray(2); return RegExpEngine::CompilationResult("RegExp too big");
SetElement(array, 0, pattern);
const char* message = "RegExp too big";
SetElement(array, 1, Factory::NewStringFromUtf8(CStrVector(message)));
Handle<Object> regexp_err =
Factory::NewSyntaxError("malformed_regexp", array);
Top::Throw(*regexp_err);
return Handle<FixedArray>();
} }
...@@ -1024,7 +1057,7 @@ RegExpCompiler::RegExpCompiler(int capture_count, bool ignore_case, bool ascii) ...@@ -1024,7 +1057,7 @@ RegExpCompiler::RegExpCompiler(int capture_count, bool ignore_case, bool ascii)
} }
Handle<FixedArray> RegExpCompiler::Assemble( RegExpEngine::CompilationResult RegExpCompiler::Assemble(
RegExpMacroAssembler* macro_assembler, RegExpMacroAssembler* macro_assembler,
RegExpNode* start, RegExpNode* start,
int capture_count, int capture_count,
...@@ -1046,24 +1079,17 @@ Handle<FixedArray> RegExpCompiler::Assemble( ...@@ -1046,24 +1079,17 @@ Handle<FixedArray> RegExpCompiler::Assemble(
while (!work_list.is_empty()) { while (!work_list.is_empty()) {
work_list.RemoveLast()->Emit(this, &new_trace); work_list.RemoveLast()->Emit(this, &new_trace);
} }
if (reg_exp_too_big_) return IrregexpRegExpTooBig(pattern); if (reg_exp_too_big_) return IrregexpRegExpTooBig();
Handle<FixedArray> array =
Factory::NewFixedArray(RegExpImpl::kIrregexpDataLength);
array->set(RegExpImpl::kIrregexpImplementationIndex,
Smi::FromInt(macro_assembler_->Implementation()));
array->set(RegExpImpl::kIrregexpNumberOfRegistersIndex,
Smi::FromInt(next_register_));
array->set(RegExpImpl::kIrregexpNumberOfCapturesIndex,
Smi::FromInt(capture_count));
Handle<Object> code = macro_assembler_->GetCode(pattern); Handle<Object> code = macro_assembler_->GetCode(pattern);
array->set(RegExpImpl::kIrregexpCodeIndex, *code);
work_list_ = NULL; work_list_ = NULL;
#ifdef DEBUG #ifdef DEBUG
if (FLAG_trace_regexp_assembler) { if (FLAG_trace_regexp_assembler) {
delete macro_assembler_; delete macro_assembler_;
} }
#endif #endif
return array; return RegExpEngine::CompilationResult(*code, next_register_);
} }
...@@ -4647,13 +4673,13 @@ void DispatchTableConstructor::VisitAction(ActionNode* that) { ...@@ -4647,13 +4673,13 @@ void DispatchTableConstructor::VisitAction(ActionNode* that) {
} }
Handle<FixedArray> RegExpEngine::Compile(RegExpCompileData* data, RegExpEngine::CompilationResult RegExpEngine::Compile(RegExpCompileData* data,
bool ignore_case, bool ignore_case,
bool is_multiline, bool is_multiline,
Handle<String> pattern, Handle<String> pattern,
bool is_ascii) { bool is_ascii) {
if ((data->capture_count + 1) * 2 - 1 > RegExpMacroAssembler::kMaxRegister) { if ((data->capture_count + 1) * 2 - 1 > RegExpMacroAssembler::kMaxRegister) {
return IrregexpRegExpTooBig(pattern); return IrregexpRegExpTooBig();
} }
RegExpCompiler compiler(data->capture_count, ignore_case, is_ascii); RegExpCompiler compiler(data->capture_count, ignore_case, is_ascii);
// Wrap the body of the regexp in capture #0. // Wrap the body of the regexp in capture #0.
......
...@@ -51,6 +51,7 @@ class RegExpImpl { ...@@ -51,6 +51,7 @@ class RegExpImpl {
// Parses the RegExp pattern and prepares the JSRegExp object with // Parses the RegExp pattern and prepares the JSRegExp object with
// generic data and choice of implementation - as well as what // generic data and choice of implementation - as well as what
// the implementation wants to store in the data field. // the implementation wants to store in the data field.
// Returns false if compilation fails.
static Handle<Object> Compile(Handle<JSRegExp> re, static Handle<Object> Compile(Handle<JSRegExp> re,
Handle<String> pattern, Handle<String> pattern,
Handle<String> flags); Handle<String> flags);
...@@ -70,12 +71,13 @@ class RegExpImpl { ...@@ -70,12 +71,13 @@ class RegExpImpl {
Handle<JSArray> lastMatchInfo); Handle<JSArray> lastMatchInfo);
// Prepares a JSRegExp object with Irregexp-specific data. // Prepares a JSRegExp object with Irregexp-specific data.
static Handle<Object> IrregexpPrepare(Handle<JSRegExp> re, static void IrregexpPrepare(Handle<JSRegExp> re,
Handle<String> pattern, Handle<String> pattern,
JSRegExp::Flags flags); JSRegExp::Flags flags,
int capture_register_count);
static Handle<Object> AtomCompile(Handle<JSRegExp> re, static void AtomCompile(Handle<JSRegExp> re,
Handle<String> pattern, Handle<String> pattern,
JSRegExp::Flags flags, JSRegExp::Flags flags,
Handle<String> match_pattern); Handle<String> match_pattern);
...@@ -107,12 +109,6 @@ class RegExpImpl { ...@@ -107,12 +109,6 @@ class RegExpImpl {
static Handle<String> StringToTwoByte(Handle<String> pattern); static Handle<String> StringToTwoByte(Handle<String> pattern);
static Handle<String> CachedStringToTwoByte(Handle<String> pattern); static Handle<String> CachedStringToTwoByte(Handle<String> pattern);
static const int kIrregexpImplementationIndex = 0;
static const int kIrregexpNumberOfCapturesIndex = 1;
static const int kIrregexpNumberOfRegistersIndex = 2;
static const int kIrregexpCodeIndex = 3;
static const int kIrregexpDataLength = 4;
// Offsets in the lastMatchInfo array. // Offsets in the lastMatchInfo array.
static const int kLastCaptureCount = 0; static const int kLastCaptureCount = 0;
static const int kLastSubject = 1; static const int kLastSubject = 1;
...@@ -141,10 +137,15 @@ class RegExpImpl { ...@@ -141,10 +137,15 @@ class RegExpImpl {
static String* last_ascii_string_; static String* last_ascii_string_;
static String* two_byte_cached_string_; static String* two_byte_cached_string_;
static bool EnsureCompiledIrregexp(Handle<JSRegExp> re, bool is_ascii);
static int IrregexpMaxRegisterCount(Handle<FixedArray> re);
static void SetIrregexpMaxRegisterCount(Handle<FixedArray> re, int value);
static int IrregexpNumberOfCaptures(Handle<FixedArray> re); static int IrregexpNumberOfCaptures(Handle<FixedArray> re);
static int IrregexpNumberOfRegisters(Handle<FixedArray> re); static int IrregexpNumberOfRegisters(Handle<FixedArray> re);
static Handle<ByteArray> IrregexpByteCode(Handle<FixedArray> re); static Handle<ByteArray> IrregexpByteCode(Handle<FixedArray> re,
static Handle<Code> IrregexpNativeCode(Handle<FixedArray> re); bool is_ascii);
static Handle<Code> IrregexpNativeCode(Handle<FixedArray> re, bool is_ascii);
// On a successful match, the result is a JSArray containing // On a successful match, the result is a JSArray containing
// captured positions. On a failure, the result is the null value. // captured positions. On a failure, the result is the null value.
...@@ -1354,7 +1355,21 @@ struct RegExpCompileData { ...@@ -1354,7 +1355,21 @@ struct RegExpCompileData {
class RegExpEngine: public AllStatic { class RegExpEngine: public AllStatic {
public: public:
static Handle<FixedArray> Compile(RegExpCompileData* input, struct CompilationResult {
explicit CompilationResult(const char* error_message)
: error_message(error_message),
code(Heap::the_hole_value()),
num_registers(0) {}
CompilationResult(Object* code, int registers)
: error_message(NULL),
code(code),
num_registers(registers) {}
const char* error_message;
Object* code;
int num_registers;
};
static CompilationResult Compile(RegExpCompileData* input,
bool ignore_case, bool ignore_case,
bool multiline, bool multiline,
Handle<String> pattern, Handle<String> pattern,
......
...@@ -697,8 +697,18 @@ void JSRegExp::JSRegExpVerify() { ...@@ -697,8 +697,18 @@ void JSRegExp::JSRegExpVerify() {
} }
case JSRegExp::IRREGEXP: { case JSRegExp::IRREGEXP: {
FixedArray* arr = FixedArray::cast(data()); FixedArray* arr = FixedArray::cast(data());
Object* irregexp_data = arr->get(JSRegExp::kIrregexpDataIndex); Object* ascii_data = arr->get(JSRegExp::kIrregexpASCIICodeIndex);
ASSERT(irregexp_data->IsFixedArray()); ASSERT(ascii_data->IsTheHole()
|| (FLAG_regexp_native ?
ascii_data->IsCode()
: ascii_data->IsByteArray()));
Object* uc16_data = arr->get(JSRegExp::kIrregexpUC16CodeIndex);
ASSERT(uc16_data->IsTheHole()
|| (FLAG_regexp_native ?
uc16_data->IsCode()
: uc16_data->IsByteArray()));
ASSERT(arr->get(JSRegExp::kIrregexpCaptureCountIndex)->IsSmi());
ASSERT(arr->get(JSRegExp::kIrregexpMaxRegisterCountIndex)->IsSmi());
break; break;
} }
default: default:
......
...@@ -2337,6 +2337,13 @@ Object* JSRegExp::DataAt(int index) { ...@@ -2337,6 +2337,13 @@ Object* JSRegExp::DataAt(int index) {
} }
void JSRegExp::SetDataAt(int index, Object* value) {
ASSERT(TypeTag() != NOT_COMPILED);
ASSERT(index >= kDataIndex); // Only implementation data can be set this way.
FixedArray::cast(data())->set(index, value);
}
bool JSObject::HasFastElements() { bool JSObject::HasFastElements() {
return !elements()->IsDictionary(); return !elements()->IsDictionary();
} }
......
...@@ -2946,6 +2946,19 @@ class JSValue: public JSObject { ...@@ -2946,6 +2946,19 @@ class JSValue: public JSObject {
}; };
// Regular expressions // Regular expressions
// The regular expression holds a single reference to a FixedArray in
// the kDataOffset field.
// The FixedArray contains the following data:
// - tag : type of regexp implementation (not compiled yet, atom or irregexp)
// - reference to the original source string
// - reference to the original flag string
// If it is an atom regexp
// - a reference to a literal string to search for
// If it is an irregexp regexp:
// - a reference to code for ASCII inputs (bytecode or compiled).
// - a reference to code for UC16 inputs (bytecode or compiled).
// - max number of registers used by irregexp implementations.
// - number of capture registers (output values) of the regexp.
class JSRegExp: public JSObject { class JSRegExp: public JSObject {
public: public:
// Meaning of Type: // Meaning of Type:
...@@ -2973,6 +2986,8 @@ class JSRegExp: public JSObject { ...@@ -2973,6 +2986,8 @@ class JSRegExp: public JSObject {
inline Flags GetFlags(); inline Flags GetFlags();
inline String* Pattern(); inline String* Pattern();
inline Object* DataAt(int index); inline Object* DataAt(int index);
// Set implementation data after the object has been prepared.
inline void SetDataAt(int index, Object* value);
static inline JSRegExp* cast(Object* obj); static inline JSRegExp* cast(Object* obj);
...@@ -2984,14 +2999,29 @@ class JSRegExp: public JSObject { ...@@ -2984,14 +2999,29 @@ class JSRegExp: public JSObject {
static const int kDataOffset = JSObject::kHeaderSize; static const int kDataOffset = JSObject::kHeaderSize;
static const int kSize = kDataOffset + kIntSize; static const int kSize = kDataOffset + kIntSize;
// Indices in the data array.
static const int kTagIndex = 0; static const int kTagIndex = 0;
static const int kSourceIndex = kTagIndex + 1; static const int kSourceIndex = kTagIndex + 1;
static const int kFlagsIndex = kSourceIndex + 1; static const int kFlagsIndex = kSourceIndex + 1;
// These two are the same since the same entry is shared for static const int kDataIndex = kFlagsIndex + 1;
// different purposes in different types of regexps. // The data fields are used in different ways depending on the
static const int kAtomPatternIndex = kFlagsIndex + 1; // value of the tag.
static const int kIrregexpDataIndex = kFlagsIndex + 1; // Atom regexps (literal strings).
static const int kDataSize = kAtomPatternIndex + 1; static const int kAtomPatternIndex = kDataIndex;
static const int kAtomDataSize = kAtomPatternIndex + 1;
// Irregexp compiled code or bytecode for ASCII.
static const int kIrregexpASCIICodeIndex = kDataIndex;
// Irregexp compiled code or bytecode for UC16.
static const int kIrregexpUC16CodeIndex = kDataIndex + 1;
// Maximal number of registers used by either ASCII or UC16.
// Only used to check that there is enough stack space
static const int kIrregexpMaxRegisterCountIndex = kDataIndex + 2;
// Number of captures in the compiled regexp.
static const int kIrregexpCaptureCountIndex = kDataIndex + 3;
static const int kIrregexpDataSize = kIrregexpCaptureCountIndex + 1;
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment