Add a trivial code generator for the DFA
authorbenjamin@webkit.org <benjamin@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 18 Feb 2015 08:57:43 +0000 (08:57 +0000)
committerbenjamin@webkit.org <benjamin@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 18 Feb 2015 08:57:43 +0000 (08:57 +0000)
https://bugs.webkit.org/show_bug.cgi?id=141017

Reviewed by Andreas Kling.

Nothing fancy yet, this is just doing a literal translation from the DFA
to machine code. It is extremely inefficient at the moment.

* WebCore.xcodeproj/project.pbxproj:
* contentextensions/ContentExtensionsBackend.cpp:
(WebCore::ContentExtensions::ContentExtensionsBackend::ContentExtensionsBackend):
(WebCore::ContentExtensions::ContentExtensionsBackend::setRuleList):
(WebCore::ContentExtensions::addActionToHashSet):
(WebCore::ContentExtensions::ContentExtensionsBackend::shouldBlockURL):
* contentextensions/ContentExtensionsBackend.h:
* contentextensions/DFA.cpp:
(WebCore::ContentExtensions::DFA::nextState): Deleted.
(WebCore::ContentExtensions::DFA::actions): Deleted.
* contentextensions/DFA.h:
(WebCore::ContentExtensions::DFA::size):
(WebCore::ContentExtensions::DFA::nodeAt):
* contentextensions/DFACompiler.cpp: Added.
(WebCore::ContentExtensions::compileDFA):
(WebCore::ContentExtensions::DFACodeGenerator::DFACodeGenerator):
(WebCore::ContentExtensions::DFACodeGenerator::compile):
(WebCore::ContentExtensions::DFACodeGenerator::lowerStateMachine):
(WebCore::ContentExtensions::DFACodeGenerator::lowerNode):
(WebCore::ContentExtensions::DFACodeGenerator::getNextCharacter):
(WebCore::ContentExtensions::DFACodeGenerator::callAddActionFunction):
* contentextensions/DFACompiler.h: Copied from Source/WebCore/contentextensions/DFA.h.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@180260 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebCore/ChangeLog
Source/WebCore/WebCore.xcodeproj/project.pbxproj
Source/WebCore/contentextensions/ContentExtensionsBackend.cpp
Source/WebCore/contentextensions/ContentExtensionsBackend.h
Source/WebCore/contentextensions/DFA.cpp
Source/WebCore/contentextensions/DFA.h
Source/WebCore/contentextensions/DFACompiler.cpp [new file with mode: 0644]
Source/WebCore/contentextensions/DFACompiler.h [new file with mode: 0644]

index ebc2040..0ea23c5 100644 (file)
@@ -1,3 +1,36 @@
+2015-02-17  Benjamin Poulain  <benjamin@webkit.org>
+
+        Add a trivial code generator for the DFA
+        https://bugs.webkit.org/show_bug.cgi?id=141017
+
+        Reviewed by Andreas Kling.
+
+        Nothing fancy yet, this is just doing a literal translation from the DFA
+        to machine code. It is extremely inefficient at the moment.
+
+        * WebCore.xcodeproj/project.pbxproj:
+        * contentextensions/ContentExtensionsBackend.cpp:
+        (WebCore::ContentExtensions::ContentExtensionsBackend::ContentExtensionsBackend):
+        (WebCore::ContentExtensions::ContentExtensionsBackend::setRuleList):
+        (WebCore::ContentExtensions::addActionToHashSet):
+        (WebCore::ContentExtensions::ContentExtensionsBackend::shouldBlockURL):
+        * contentextensions/ContentExtensionsBackend.h:
+        * contentextensions/DFA.cpp:
+        (WebCore::ContentExtensions::DFA::nextState): Deleted.
+        (WebCore::ContentExtensions::DFA::actions): Deleted.
+        * contentextensions/DFA.h:
+        (WebCore::ContentExtensions::DFA::size):
+        (WebCore::ContentExtensions::DFA::nodeAt):
+        * contentextensions/DFACompiler.cpp: Added.
+        (WebCore::ContentExtensions::compileDFA):
+        (WebCore::ContentExtensions::DFACodeGenerator::DFACodeGenerator):
+        (WebCore::ContentExtensions::DFACodeGenerator::compile):
+        (WebCore::ContentExtensions::DFACodeGenerator::lowerStateMachine):
+        (WebCore::ContentExtensions::DFACodeGenerator::lowerNode):
+        (WebCore::ContentExtensions::DFACodeGenerator::getNextCharacter):
+        (WebCore::ContentExtensions::DFACodeGenerator::callAddActionFunction):
+        * contentextensions/DFACompiler.h: Copied from Source/WebCore/contentextensions/DFA.h.
+
 2015-02-17  Chris Dumez  <cdumez@apple.com>
 
         Rename ScopedEventQueue::instance() to singleton()
index 881ace6..0f1e0a2 100644 (file)
                26C15CF71857E15E00F15C03 /* ResourceHandleCFURLConnectionDelegateWithOperationQueue.h in Headers */ = {isa = PBXBuildFile; fileRef = 26C15CF51857E15D00F15C03 /* ResourceHandleCFURLConnectionDelegateWithOperationQueue.h */; };
                26C17A3E1491D2D400D12BA2 /* FileSystemIOS.h in Headers */ = {isa = PBXBuildFile; fileRef = 26C17A3C1491D2D400D12BA2 /* FileSystemIOS.h */; settings = {ATTRIBUTES = (Private, ); }; };
                26C17A3F1491D2D400D12BA2 /* FileSystemIOS.mm in Sources */ = {isa = PBXBuildFile; fileRef = 26C17A3D1491D2D400D12BA2 /* FileSystemIOS.mm */; };
+               26C560D31A783EBB008682E4 /* DFACompiler.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 26C560D11A783EBB008682E4 /* DFACompiler.cpp */; };
+               26C560D41A783EBB008682E4 /* DFACompiler.h in Headers */ = {isa = PBXBuildFile; fileRef = 26C560D21A783EBB008682E4 /* DFACompiler.h */; };
                26E98A10130A9FCA008EB7B2 /* TextCodecASCIIFastPath.h in Headers */ = {isa = PBXBuildFile; fileRef = 26E98A0F130A9FCA008EB7B2 /* TextCodecASCIIFastPath.h */; };
                26F0C8971A2E724B002794F8 /* ContentExtensionsManager.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 26F0C8951A2E724B002794F8 /* ContentExtensionsManager.cpp */; };
                26F0C8981A2E724B002794F8 /* ContentExtensionsManager.h in Headers */ = {isa = PBXBuildFile; fileRef = 26F0C8961A2E724B002794F8 /* ContentExtensionsManager.h */; settings = {ATTRIBUTES = (Private, ); }; };
                26C15CF51857E15D00F15C03 /* ResourceHandleCFURLConnectionDelegateWithOperationQueue.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ResourceHandleCFURLConnectionDelegateWithOperationQueue.h; sourceTree = "<group>"; };
                26C17A3C1491D2D400D12BA2 /* FileSystemIOS.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = FileSystemIOS.h; path = ios/FileSystemIOS.h; sourceTree = "<group>"; };
                26C17A3D1491D2D400D12BA2 /* FileSystemIOS.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = FileSystemIOS.mm; path = ios/FileSystemIOS.mm; sourceTree = "<group>"; };
+               26C560D11A783EBB008682E4 /* DFACompiler.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = DFACompiler.cpp; sourceTree = "<group>"; };
+               26C560D21A783EBB008682E4 /* DFACompiler.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DFACompiler.h; sourceTree = "<group>"; };
                26E98A0F130A9FCA008EB7B2 /* TextCodecASCIIFastPath.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TextCodecASCIIFastPath.h; sourceTree = "<group>"; };
                26F0C8951A2E724B002794F8 /* ContentExtensionsManager.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ContentExtensionsManager.cpp; sourceTree = "<group>"; };
                26F0C8961A2E724B002794F8 /* ContentExtensionsManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ContentExtensionsManager.h; sourceTree = "<group>"; };
                                26F0C8961A2E724B002794F8 /* ContentExtensionsManager.h */,
                                267725F61A5B3AD9003C24DD /* DFA.cpp */,
                                267725F71A5B3AD9003C24DD /* DFA.h */,
+                               26C560D11A783EBB008682E4 /* DFACompiler.cpp */,
+                               26C560D21A783EBB008682E4 /* DFACompiler.h */,
                                267725F91A5B3AD9003C24DD /* DFANode.h */,
                                269397251A4A5FBD00E8349D /* NFA.cpp */,
                                269397231A4A5B6400E8349D /* NFA.h */,
                                B22279C20D00BF220071B782 /* SVGException.h in Headers */,
                                B22279C50D00BF220071B782 /* SVGExternalResourcesRequired.h in Headers */,
                                B22279C80D00BF220071B782 /* SVGFEBlendElement.h in Headers */,
+                               26C560D41A783EBB008682E4 /* DFACompiler.h in Headers */,
                                B22279CB0D00BF220071B782 /* SVGFEColorMatrixElement.h in Headers */,
                                B22279CE0D00BF220071B782 /* SVGFEComponentTransferElement.h in Headers */,
                                B22279D10D00BF220071B782 /* SVGFECompositeElement.h in Headers */,
                                C5A1EA7C152BCF04004D00B6 /* SimplifyMarkupCommand.cpp in Sources */,
                                FD00D7A414A3F61900734011 /* SincResampler.cpp in Sources */,
                                51327D6111A33A2B004F9D65 /* SinkDocument.cpp in Sources */,
+                               26C560D31A783EBB008682E4 /* DFACompiler.cpp in Sources */,
                                49E911CC0EF86D47009D0CAF /* SkewTransformOperation.cpp in Sources */,
                                4150F9F212B6E0E70008C860 /* SliderThumbElement.cpp in Sources */,
                                1C0939EA1A13E12900B788E5 /* CachedSVGFont.cpp in Sources */,
index b0019c9..1257deb 100644 (file)
@@ -29,6 +29,7 @@
 #if ENABLE(CONTENT_EXTENSIONS)
 
 #include "ContentExtensionsDebugging.h"
+#include "DFACompiler.h"
 #include "NFA.h"
 #include "NFAToDFA.h"
 #include "URL.h"
@@ -42,6 +43,11 @@ namespace WebCore {
 
 namespace ContentExtensions {
 
+ContentExtensionsBackend::ContentExtensionsBackend()
+    : m_vm(JSC::VM::sharedInstance())
+{
+}
+
 void ContentExtensionsBackend::setRuleList(const String& identifier, const Vector<ContentExtensionRule>& ruleList)
 {
     ASSERT(!identifier.isEmpty());
@@ -85,19 +91,24 @@ void ContentExtensionsBackend::setRuleList(const String& identifier, const Vecto
     double dfaBuildTimeStart = monotonicallyIncreasingTime();
 #endif
 
-    CompiledContentExtension compiledContentExtension = { NFAToDFA::convert(nfa), ruleList };
+    const DFA dfa = NFAToDFA::convert(nfa);
 
 #if CONTENT_EXTENSIONS_PERFORMANCE_REPORTING
     double dfaBuildTimeEnd = monotonicallyIncreasingTime();
     dataLogF("    Time spent building the DFA: %f\n", (dfaBuildTimeEnd - dfaBuildTimeStart));
 #endif
 
-    // FIXME: never add a DFA that only matches the empty set.
+    JSC::MacroAssemblerCodeRef compiledDfa;
+    {
+        JSC::JSLockHolder locker(m_vm.get());
+        compiledDfa = compileDFA(dfa, m_vm.get());
+    }
 
 #if CONTENT_EXTENSIONS_STATE_MACHINE_DEBUGGING
-    compiledContentExtension.dfa.debugPrintDot();
+    dfa.debugPrintDot();
 #endif
 
+    CompiledContentExtension compiledContentExtension = { compiledDfa, ruleList };
     m_ruleLists.set(identifier, compiledContentExtension);
 }
 
@@ -111,6 +122,11 @@ void ContentExtensionsBackend::removeAllRuleLists()
     m_ruleLists.clear();
 }
 
+static void addActionToHashSet(TriggeredActionSet* triggeredActionSet, unsigned actionId)
+{
+    triggeredActionSet->add(actionId);
+}
+
 bool ContentExtensionsBackend::shouldBlockURL(const URL& url)
 {
     const String& urlString = url.string();
@@ -118,21 +134,17 @@ bool ContentExtensionsBackend::shouldBlockURL(const URL& url)
 
     for (auto& ruleListSlot : m_ruleLists) {
         CompiledContentExtension& compiledContentExtension = ruleListSlot.value;
-        unsigned state = compiledContentExtension.dfa.root();
 
-        HashSet<uint64_t, DefaultHash<uint64_t>::Hash, WTF::UnsignedWithZeroKeyHashTraits<uint64_t>> triggeredActions;
+        CString urlCString = urlString.utf8();
+        PotentialPageLoadDescriptor potentialPageLoadDescriptor;
+        potentialPageLoadDescriptor.urlBuffer = urlCString.data();
+        potentialPageLoadDescriptor.urlBufferSize = urlCString.length();
 
-        for (unsigned i = 0; i < urlString.length(); ++i) {
-            char character = static_cast<char>(urlString[i]);
-            bool ok;
-            state = compiledContentExtension.dfa.nextState(state, character, ok);
-            if (!ok)
-                break;
+        TriggeredActionSet triggeredActions;
+
+        void* compiledMatcher = compiledContentExtension.compiledMatcher.code().executableAddress();
+        reinterpret_cast<CompiledRuleMatcher>(compiledMatcher)(&potentialPageLoadDescriptor, addActionToHashSet, &triggeredActions);
 
-            const Vector<uint64_t>& actions = compiledContentExtension.dfa.actions(state);
-            if (!actions.isEmpty())
-                triggeredActions.add(actions.begin(), actions.end());
-        }
         if (!triggeredActions.isEmpty()) {
             Vector<uint64_t> sortedActions;
             copyToVector(triggeredActions, sortedActions);
index 4c4ab00..43157d1 100644 (file)
@@ -30,6 +30,8 @@
 
 #include "ContentExtensionRule.h"
 #include "DFA.h"
+#include <JavaScriptCore/MacroAssemblerCodeRef.h>
+#include <JavaScriptCore/VM.h>
 #include <wtf/HashMap.h>
 #include <wtf/text/StringHash.h>
 #include <wtf/text/WTFString.h>
@@ -47,6 +49,8 @@ namespace ContentExtensions {
 // 2) It provides APIs for the WebCore interfaces to use those rules efficiently.
 class ContentExtensionsBackend {
 public:
+    ContentExtensionsBackend();
+
     // - Rule management interface. This can be used by upper layer.
 
     // Set a list of rules for a given name. If there were existing rules for the name, they are overriden.
@@ -60,10 +64,11 @@ public:
 
 private:
     struct CompiledContentExtension {
-        DFA dfa;
+        JSC::MacroAssemblerCodeRef compiledMatcher;
         Vector<ContentExtensionRule> ruleList;
     };
 
+    Ref<JSC::VM> m_vm;
     HashMap<String, CompiledContentExtension> m_ruleLists;
 };
 
index 4f43e44..23c6221 100644 (file)
@@ -59,31 +59,6 @@ DFA& DFA::operator=(const DFA& dfa)
     return *this;
 }
 
-unsigned DFA::nextState(unsigned currentState, char character, bool& ok) const
-{
-    ASSERT(currentState < m_nodes.size());
-
-    const DFANode& node = m_nodes[currentState];
-    auto nextNode = node.transitions.find(character);
-    if (nextNode != node.transitions.end()) {
-        ok = true;
-        return nextNode->value;
-    }
-    if (node.hasFallbackTransition) {
-        ok = true;
-        return node.fallbackTransition;
-    }
-    ok = false;
-    return 0;
-
-}
-
-const Vector<uint64_t>& DFA::actions(unsigned currentState) const
-{
-    ASSERT(currentState < m_nodes.size());
-    return m_nodes[currentState].actions;
-}
-
 #if CONTENT_EXTENSIONS_STATE_MACHINE_DEBUGGING
 static void printRange(bool firstRange, char rangeStart, char rangeEnd)
 {
index cc80d86..6acda5a 100644 (file)
@@ -46,10 +46,11 @@ public:
     DFA& operator=(const DFA&);
 
     unsigned root() const { return m_root; }
-    // If there is a transition to a valid state on "character", return that state and set ok to true.
-    // Otherwise, the return value is undefined and ok is false.
-    unsigned nextState(unsigned currentState, char character, bool& ok) const;
-    const Vector<uint64_t>& actions(unsigned currentState) const;
+    unsigned size() const { return m_nodes.size(); }
+    const DFANode& nodeAt(unsigned i) const
+    {
+        return m_nodes[i];
+    }
 
 #if CONTENT_EXTENSIONS_STATE_MACHINE_DEBUGGING
     void debugPrintDot() const;
diff --git a/Source/WebCore/contentextensions/DFACompiler.cpp b/Source/WebCore/contentextensions/DFACompiler.cpp
new file mode 100644 (file)
index 0000000..f5eda00
--- /dev/null
@@ -0,0 +1,192 @@
+/*
+ * Copyright (C) 2015 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. 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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS 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 APPLE INC. OR ITS 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 "config.h"
+#include "DFACompiler.h"
+
+#if ENABLE(CONTENT_EXTENSIONS)
+
+#include "DFA.h"
+#include "DFANode.h"
+#include "RegisterAllocator.h"
+#include "StackAllocator.h"
+#include <JavaScriptCore/GPRInfo.h>
+#include <JavaScriptCore/LinkBuffer.h>
+#include <JavaScriptCore/MacroAssembler.h>
+#include <JavaScriptCore/VM.h>
+
+namespace WebCore {
+
+namespace ContentExtensions {
+
+typedef JSC::MacroAssembler Assembler;
+
+class DFACodeGenerator {
+public:
+    explicit DFACodeGenerator(const DFA&);
+    JSC::MacroAssemblerCodeRef compile(JSC::VM&);
+
+private:
+    void lowerStateMachine();
+    void lowerNode(Assembler::JumpList& failureCases, const DFANode&);
+
+    void tryLoadingNextCharacter(Assembler::JumpList& failureCases, LocalRegister& output);
+    void callAddActionFunction(unsigned argument);
+
+    const DFA& m_dfa;
+
+    Assembler m_assembler;
+    RegisterAllocator m_registerAllocator;
+    StackAllocator m_stackAllocator;
+
+    Assembler::RegisterID m_urlBufferOffsetRegister = InvalidGPRReg;
+    Vector<Assembler::Label> m_stateEntryLabels;
+    Vector<Assembler::JumpList> m_jumpsPerState;
+};
+
+JSC::MacroAssemblerCodeRef compileDFA(const DFA& dfa, JSC::VM& vm)
+{
+    DFACodeGenerator codeGenerator(dfa);
+    return codeGenerator.compile(vm);
+}
+
+DFACodeGenerator::DFACodeGenerator(const DFA& dfa)
+    : m_dfa(dfa)
+    , m_stackAllocator(m_assembler)
+{
+}
+
+JSC::MacroAssemblerCodeRef DFACodeGenerator::compile(JSC::VM& vm)
+{
+    m_registerAllocator.reserveCallerSavedRegisters(WTF_ARRAY_LENGTH(callerSavedRegisters));
+
+    m_registerAllocator.allocateRegister(JSC::GPRInfo::argumentGPR0);
+    m_registerAllocator.allocateRegister(JSC::GPRInfo::argumentGPR1);
+    m_registerAllocator.allocateRegister(JSC::GPRInfo::argumentGPR2);
+
+    m_urlBufferOffsetRegister = m_registerAllocator.allocateRegister();
+    m_assembler.move(Assembler::TrustedImm32(0), m_urlBufferOffsetRegister);
+
+    lowerStateMachine();
+
+    m_registerAllocator.deallocateRegister(m_urlBufferOffsetRegister);
+    m_urlBufferOffsetRegister = InvalidGPRReg;
+
+    m_assembler.ret();
+
+    m_registerAllocator.deallocateRegister(JSC::GPRInfo::argumentGPR0);
+    m_registerAllocator.deallocateRegister(JSC::GPRInfo::argumentGPR1);
+    m_registerAllocator.deallocateRegister(JSC::GPRInfo::argumentGPR2);
+
+    JSC::LinkBuffer linkBuffer(vm, m_assembler, GLOBAL_THUNK_ID);
+#if CONTENT_EXTENSIONS_STATE_MACHINE_DEBUGGING
+    return linkBuffer.finalizeCodeWithDisassembly("Compiled Extension");
+#else
+    return FINALIZE_CODE(linkBuffer, ("Compiled Extension"));
+#endif
+}
+
+void DFACodeGenerator::lowerStateMachine()
+{
+    Assembler::JumpList failureCases;
+
+    unsigned dfaSize = m_dfa.size();
+
+    m_stateEntryLabels.resize(dfaSize);
+    m_jumpsPerState.resize(dfaSize);
+
+    unsigned root = m_dfa.root();
+    m_stateEntryLabels[root] = m_assembler.label();
+    lowerNode(failureCases, m_dfa.nodeAt(root));
+
+    for (unsigned i = 0; i < dfaSize; ++i) {
+        if (i == root)
+            continue;
+        m_stateEntryLabels[i] = m_assembler.label();
+        lowerNode(failureCases, m_dfa.nodeAt(i));
+    }
+
+    for (unsigned i = 0; i < dfaSize; ++i)
+        m_jumpsPerState[i].linkTo(m_stateEntryLabels[i], &m_assembler);
+
+    failureCases.link(&m_assembler);
+}
+
+void DFACodeGenerator::lowerNode(Assembler::JumpList& failureCases, const DFANode& dfaNode)
+{
+    for (uint64_t actionId : dfaNode.actions)
+        callAddActionFunction(static_cast<unsigned>(actionId));
+
+    LocalRegister characterRegister(m_registerAllocator);
+    tryLoadingNextCharacter(failureCases, characterRegister);
+
+    for (const auto& transition : dfaNode.transitions) {
+        Assembler::Jump jumpOnCharacter = m_assembler.branch32(Assembler::Equal, characterRegister, Assembler::TrustedImm32(transition.key));
+        m_jumpsPerState[transition.value].append(jumpOnCharacter);
+    }
+
+    if (dfaNode.hasFallbackTransition)
+        m_jumpsPerState[dfaNode.fallbackTransition].append(m_assembler.jump());
+    else
+        failureCases.append(m_assembler.jump());
+}
+
+void DFACodeGenerator::tryLoadingNextCharacter(Assembler::JumpList& failureCases, LocalRegister& character)
+{
+    failureCases.append(m_assembler.branch32(Assembler::Equal, Assembler::Address(JSC::GPRInfo::argumentGPR0, OBJECT_OFFSETOF(PotentialPageLoadDescriptor, urlBufferSize)), m_urlBufferOffsetRegister));
+
+    LocalRegister bufferPointer(m_registerAllocator);
+    m_assembler.loadPtr(Assembler::Address(JSC::GPRInfo::argumentGPR0, OBJECT_OFFSETOF(PotentialPageLoadDescriptor, urlBuffer)), bufferPointer);
+    m_assembler.load8(Assembler::BaseIndex(bufferPointer, m_urlBufferOffsetRegister, Assembler::TimesOne), character);
+
+    m_assembler.add32(Assembler::TrustedImm32(1), m_urlBufferOffsetRegister);
+}
+
+void DFACodeGenerator::callAddActionFunction(unsigned argument)
+{
+    const RegisterVector& allocatedRegisters = m_registerAllocator.allocatedRegisters();
+    StackAllocator::StackReferenceVector savedRegisterStackReferences = m_stackAllocator.push(allocatedRegisters);
+
+    m_stackAllocator.alignStackPreFunctionCall();
+
+    {
+        LocalRegister functionAddress(m_registerAllocator);
+        m_assembler.move(JSC::GPRInfo::argumentGPR1, functionAddress);
+        m_assembler.move(JSC::GPRInfo::argumentGPR2, JSC::GPRInfo::argumentGPR0);
+        m_assembler.move(Assembler::TrustedImm32(argument), JSC::GPRInfo::argumentGPR1);
+
+        m_assembler.call(functionAddress);
+    }
+
+    m_stackAllocator.unalignStackPostFunctionCall();
+
+    m_stackAllocator.pop(savedRegisterStackReferences, allocatedRegisters);
+}
+
+} // namespace ContentExtensions
+
+} // namespace WebCore
+
+#endif // ENABLE(CONTENT_EXTENSIONS)
diff --git a/Source/WebCore/contentextensions/DFACompiler.h b/Source/WebCore/contentextensions/DFACompiler.h
new file mode 100644 (file)
index 0000000..01e4ed8
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * Copyright (C) 2015 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. 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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS 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 APPLE INC. OR ITS 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.
+ */
+
+#ifndef DFACompiler_h
+#define DFACompiler_h
+
+#if ENABLE(CONTENT_EXTENSIONS)
+
+#include <JavaScriptCore/MacroAssemblerCodeRef.h>
+#include <wtf/HashSet.h>
+
+namespace WebCore {
+
+namespace ContentExtensions {
+
+class DFA;
+
+struct PotentialPageLoadDescriptor {
+    const char* urlBuffer;
+    unsigned urlBufferSize;
+};
+
+typedef HashSet<unsigned, DefaultHash<unsigned>::Hash, WTF::UnsignedWithZeroKeyHashTraits<unsigned>> TriggeredActionSet;
+
+typedef void (*AddActionFunction)(TriggeredActionSet*, unsigned);
+typedef void (*CompiledRuleMatcher)(PotentialPageLoadDescriptor*, AddActionFunction, TriggeredActionSet*);
+
+JSC::MacroAssemblerCodeRef compileDFA(const DFA&, JSC::VM&);
+
+} // namespace ContentExtensions
+
+} // namespace WebCore
+
+#endif // ENABLE(CONTENT_EXTENSIONS)
+
+#endif // DFACompiler_h