summaryrefslogtreecommitdiffstats
path: root/JavaScriptCore/runtime/RegExp.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'JavaScriptCore/runtime/RegExp.cpp')
-rw-r--r--JavaScriptCore/runtime/RegExp.cpp222
1 files changed, 70 insertions, 152 deletions
diff --git a/JavaScriptCore/runtime/RegExp.cpp b/JavaScriptCore/runtime/RegExp.cpp
index 4e958f4..a33fa91 100644
--- a/JavaScriptCore/runtime/RegExp.cpp
+++ b/JavaScriptCore/runtime/RegExp.cpp
@@ -28,9 +28,6 @@
#include <wtf/Assertions.h>
#include <wtf/OwnArrayPtr.h>
-
-#if ENABLE(YARR)
-
#include "yarr/RegexCompiler.h"
#if ENABLE(YARR_JIT)
#include "yarr/RegexJIT.h"
@@ -38,75 +35,59 @@
#include "yarr/RegexInterpreter.h"
#endif
-#else
-
-#if ENABLE(WREC)
-#include "JIT.h"
-#include "WRECGenerator.h"
-#endif
-#include <pcre/pcre.h>
-
-#endif
-
namespace JSC {
-#if ENABLE(WREC)
-using namespace WREC;
+struct RegExpRepresentation {
+#if ENABLE(YARR_JIT)
+ Yarr::RegexCodeBlock m_regExpJITCode;
+#else
+ OwnPtr<Yarr::BytecodePattern> m_regExpBytecode;
#endif
-
-inline RegExp::RegExp(JSGlobalData* globalData, const UString& pattern)
- : m_pattern(pattern)
- , m_flagBits(0)
- , m_constructionError(0)
- , m_numSubpatterns(0)
-{
- compile(globalData);
-}
+};
inline RegExp::RegExp(JSGlobalData* globalData, const UString& pattern, const UString& flags)
: m_pattern(pattern)
, m_flagBits(0)
, m_constructionError(0)
, m_numSubpatterns(0)
+#if ENABLE(REGEXP_TRACING)
+ , m_rtMatchCallCount(0)
+ , m_rtMatchFoundCount(0)
+#endif
+ , m_representation(adoptPtr(new RegExpRepresentation))
{
// NOTE: The global flag is handled on a case-by-case basis by functions like
// String::match and RegExpObject::match.
- if (flags.find('g') != -1)
- m_flagBits |= Global;
- if (flags.find('i') != -1)
- m_flagBits |= IgnoreCase;
- if (flags.find('m') != -1)
- m_flagBits |= Multiline;
-
+ if (!flags.isNull()) {
+ if (flags.find('g') != notFound)
+ m_flagBits |= Global;
+ if (flags.find('i') != notFound)
+ m_flagBits |= IgnoreCase;
+ if (flags.find('m') != notFound)
+ m_flagBits |= Multiline;
+ }
compile(globalData);
}
-#if !ENABLE(YARR)
RegExp::~RegExp()
{
- jsRegExpFree(m_regExp);
-}
-#endif
-
-PassRefPtr<RegExp> RegExp::create(JSGlobalData* globalData, const UString& pattern)
-{
- return adoptRef(new RegExp(globalData, pattern));
}
PassRefPtr<RegExp> RegExp::create(JSGlobalData* globalData, const UString& pattern, const UString& flags)
{
- return adoptRef(new RegExp(globalData, pattern, flags));
+ RefPtr<RegExp> res = adoptRef(new RegExp(globalData, pattern, flags));
+#if ENABLE(REGEXP_TRACING)
+ globalData->addRegExpToTrace(res);
+#endif
+ return res.release();
}
-#if ENABLE(YARR)
-
void RegExp::compile(JSGlobalData* globalData)
{
#if ENABLE(YARR_JIT)
- Yarr::jitCompileRegex(globalData, m_regExpJITCode, m_pattern, m_numSubpatterns, m_constructionError, ignoreCase(), multiline());
+ Yarr::jitCompileRegex(globalData, m_representation->m_regExpJITCode, m_pattern, m_numSubpatterns, m_constructionError, &globalData->m_regexAllocator, ignoreCase(), multiline());
#else
- UNUSED_PARAM(globalData);
- m_regExpBytecode.set(Yarr::byteCompileRegex(m_pattern, m_numSubpatterns, m_constructionError, ignoreCase(), multiline()));
+ m_representation->m_regExpBytecode = Yarr::byteCompileRegex(m_pattern, m_numSubpatterns, m_constructionError, &globalData->m_regexAllocator, ignoreCase(), multiline());
#endif
}
@@ -114,18 +95,20 @@ int RegExp::match(const UString& s, int startOffset, Vector<int, 32>* ovector)
{
if (startOffset < 0)
startOffset = 0;
- if (ovector)
- ovector->clear();
+
+#if ENABLE(REGEXP_TRACING)
+ m_rtMatchCallCount++;
+#endif
- if (startOffset > s.size() || s.isNull())
+ if (static_cast<unsigned>(startOffset) > s.length() || s.isNull())
return -1;
#if ENABLE(YARR_JIT)
- if (!!m_regExpJITCode) {
+ if (!!m_representation->m_regExpJITCode) {
#else
- if (m_regExpBytecode) {
+ if (m_representation->m_regExpBytecode) {
#endif
- int offsetVectorSize = (m_numSubpatterns + 1) * 3; // FIXME: should be 2 - but adding temporary fallback to pcre.
+ int offsetVectorSize = (m_numSubpatterns + 1) * 2;
int* offsetVector;
Vector<int, 32> nonReturnedOvector;
if (ovector) {
@@ -137,123 +120,58 @@ int RegExp::match(const UString& s, int startOffset, Vector<int, 32>* ovector)
}
ASSERT(offsetVector);
- for (int j = 0; j < offsetVectorSize; ++j)
+ // Initialize offsetVector with the return value (index 0) and the
+ // first subpattern start indicies (even index values) set to -1.
+ // No need to init the subpattern end indicies.
+ for (unsigned j = 0, i = 0; i < m_numSubpatterns + 1; j += 2, i++)
offsetVector[j] = -1;
-
#if ENABLE(YARR_JIT)
- int result = Yarr::executeRegex(m_regExpJITCode, s.data(), startOffset, s.size(), offsetVector, offsetVectorSize);
+ int result = Yarr::executeRegex(m_representation->m_regExpJITCode, s.characters(), startOffset, s.length(), offsetVector);
#else
- int result = Yarr::interpretRegex(m_regExpBytecode.get(), s.data(), startOffset, s.size(), offsetVector);
+ int result = Yarr::interpretRegex(m_representation->m_regExpBytecode.get(), s.characters(), startOffset, s.length(), offsetVector);
#endif
- if (result < 0) {
-#ifndef NDEBUG
- // TODO: define up a symbol, rather than magic -1
- if (result != -1)
- fprintf(stderr, "jsRegExpExecute failed with result %d\n", result);
+ ASSERT(result >= -1);;
+
+#if ENABLE(REGEXP_TRACING)
+ if (result != -1)
+ m_rtMatchFoundCount++;
#endif
- if (ovector)
- ovector->clear();
- }
+
return result;
}
return -1;
}
-#else
-
-void RegExp::compile(JSGlobalData* globalData)
-{
- m_regExp = 0;
-#if ENABLE(WREC)
- m_wrecFunction = Generator::compileRegExp(globalData, m_pattern, &m_numSubpatterns, &m_constructionError, m_executablePool, ignoreCase(), multiline());
- if (m_wrecFunction || m_constructionError)
- return;
- // Fall through to non-WREC case.
-#else
- UNUSED_PARAM(globalData);
-#endif
-
- JSRegExpIgnoreCaseOption ignoreCaseOption = ignoreCase() ? JSRegExpIgnoreCase : JSRegExpDoNotIgnoreCase;
- JSRegExpMultilineOption multilineOption = multiline() ? JSRegExpMultiline : JSRegExpSingleLine;
- m_regExp = jsRegExpCompile(reinterpret_cast<const UChar*>(m_pattern.data()), m_pattern.size(), ignoreCaseOption, multilineOption, &m_numSubpatterns, &m_constructionError);
-}
-
-int RegExp::match(const UString& s, int startOffset, Vector<int, 32>* ovector)
-{
- if (startOffset < 0)
- startOffset = 0;
- if (ovector)
- ovector->clear();
-
- if (startOffset > s.size() || s.isNull())
- return -1;
-
-#if ENABLE(WREC)
- if (m_wrecFunction) {
- int offsetVectorSize = (m_numSubpatterns + 1) * 2;
- int* offsetVector;
- Vector<int, 32> nonReturnedOvector;
- if (ovector) {
- ovector->resize(offsetVectorSize);
- offsetVector = ovector->data();
- } else {
- nonReturnedOvector.resize(offsetVectorSize);
- offsetVector = nonReturnedOvector.data();
- }
- ASSERT(offsetVector);
- for (int j = 0; j < offsetVectorSize; ++j)
- offsetVector[j] = -1;
-
- int result = m_wrecFunction(s.data(), startOffset, s.size(), offsetVector);
+#if ENABLE(REGEXP_TRACING)
+ void RegExp::printTraceData()
+ {
+ char formattedPattern[41];
+ char rawPattern[41];
+
+ strncpy(rawPattern, m_pattern.utf8().data(), 40);
+ rawPattern[40]= '\0';
+
+ int pattLen = strlen(rawPattern);
+
+ snprintf(formattedPattern, 41, (pattLen <= 38) ? "/%.38s/" : "/%.36s...", rawPattern);
- if (result < 0) {
-#ifndef NDEBUG
- // TODO: define up a symbol, rather than magic -1
- if (result != -1)
- fprintf(stderr, "jsRegExpExecute failed with result %d\n", result);
-#endif
- if (ovector)
- ovector->clear();
- }
- return result;
- } else
-#endif
- if (m_regExp) {
- // Set up the offset vector for the result.
- // First 2/3 used for result, the last third used by PCRE.
- int* offsetVector;
- int offsetVectorSize;
- int fixedSizeOffsetVector[3];
- if (!ovector) {
- offsetVectorSize = 3;
- offsetVector = fixedSizeOffsetVector;
- } else {
- offsetVectorSize = (m_numSubpatterns + 1) * 3;
- ovector->resize(offsetVectorSize);
- offsetVector = ovector->data();
- }
+#if ENABLE(YARR_JIT)
+ Yarr::RegexCodeBlock& codeBlock = m_representation->m_regExpJITCode;
- int numMatches = jsRegExpExecute(m_regExp, reinterpret_cast<const UChar*>(s.data()), s.size(), startOffset, offsetVector, offsetVectorSize);
-
- if (numMatches < 0) {
-#ifndef NDEBUG
- if (numMatches != JSRegExpErrorNoMatch)
- fprintf(stderr, "jsRegExpExecute failed with result %d\n", numMatches);
+ char jitAddr[20];
+ if (codeBlock.getFallback())
+ sprintf(jitAddr, "fallback");
+ else
+ sprintf(jitAddr, "0x%014lx", reinterpret_cast<unsigned long int>(codeBlock.getAddr()));
+#else
+ const char* jitAddr = "JIT Off";
#endif
- if (ovector)
- ovector->clear();
- return -1;
- }
-
- return offsetVector[0];
+
+ printf("%-40.40s %16.16s %10d %10d\n", formattedPattern, jitAddr, m_rtMatchCallCount, m_rtMatchFoundCount);
}
-
- return -1;
-}
-
#endif
-
+
} // namespace JSC