Title: [180260] trunk/Source/WebCore
Revision
180260
Author
benja...@webkit.org
Date
2015-02-18 00:57:43 -0800 (Wed, 18 Feb 2015)

Log Message

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.

Modified Paths

Added Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (180259 => 180260)


--- trunk/Source/WebCore/ChangeLog	2015-02-18 08:57:36 UTC (rev 180259)
+++ trunk/Source/WebCore/ChangeLog	2015-02-18 08:57:43 UTC (rev 180260)
@@ -1,3 +1,36 @@
+2015-02-17  Benjamin Poulain  <benja...@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  <cdu...@apple.com>
 
         Rename ScopedEventQueue::instance() to singleton()

Modified: trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj (180259 => 180260)


--- trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj	2015-02-18 08:57:36 UTC (rev 180259)
+++ trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj	2015-02-18 08:57:43 UTC (rev 180260)
@@ -1029,6 +1029,8 @@
 		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, ); }; };
@@ -8053,6 +8055,8 @@
 		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>"; };
@@ -15372,6 +15376,8 @@
 				26F0C8961A2E724B002794F8 /* ContentExtensionsManager.h */,
 				267725F61A5B3AD9003C24DD /* DFA.cpp */,
 				267725F71A5B3AD9003C24DD /* DFA.h */,
+				26C560D11A783EBB008682E4 /* DFACompiler.cpp */,
+				26C560D21A783EBB008682E4 /* DFACompiler.h */,
 				267725F91A5B3AD9003C24DD /* DFANode.h */,
 				269397251A4A5FBD00E8349D /* NFA.cpp */,
 				269397231A4A5B6400E8349D /* NFA.h */,
@@ -26314,6 +26320,7 @@
 				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 */,
@@ -29526,6 +29533,7 @@
 				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 */,

Modified: trunk/Source/WebCore/contentextensions/ContentExtensionsBackend.cpp (180259 => 180260)


--- trunk/Source/WebCore/contentextensions/ContentExtensionsBackend.cpp	2015-02-18 08:57:36 UTC (rev 180259)
+++ trunk/Source/WebCore/contentextensions/ContentExtensionsBackend.cpp	2015-02-18 08:57:43 UTC (rev 180260)
@@ -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 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 @@
     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 @@
     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 @@
 
     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;
 
-            const Vector<uint64_t>& actions = compiledContentExtension.dfa.actions(state);
-            if (!actions.isEmpty())
-                triggeredActions.add(actions.begin(), actions.end());
-        }
+        void* compiledMatcher = compiledContentExtension.compiledMatcher.code().executableAddress();
+        reinterpret_cast<CompiledRuleMatcher>(compiledMatcher)(&potentialPageLoadDescriptor, addActionToHashSet, &triggeredActions);
+
         if (!triggeredActions.isEmpty()) {
             Vector<uint64_t> sortedActions;
             copyToVector(triggeredActions, sortedActions);

Modified: trunk/Source/WebCore/contentextensions/ContentExtensionsBackend.h (180259 => 180260)


--- trunk/Source/WebCore/contentextensions/ContentExtensionsBackend.h	2015-02-18 08:57:36 UTC (rev 180259)
+++ trunk/Source/WebCore/contentextensions/ContentExtensionsBackend.h	2015-02-18 08:57:43 UTC (rev 180260)
@@ -30,6 +30,8 @@
 
 #include "ContentExtensionRule.h"
 #include "DFA.h"
+#include <_javascript_Core/MacroAssemblerCodeRef.h>
+#include <_javascript_Core/VM.h>
 #include <wtf/HashMap.h>
 #include <wtf/text/StringHash.h>
 #include <wtf/text/WTFString.h>
@@ -47,6 +49,8 @@
 // 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 @@
 
 private:
     struct CompiledContentExtension {
-        DFA dfa;
+        JSC::MacroAssemblerCodeRef compiledMatcher;
         Vector<ContentExtensionRule> ruleList;
     };
 
+    Ref<JSC::VM> m_vm;
     HashMap<String, CompiledContentExtension> m_ruleLists;
 };
 

Modified: trunk/Source/WebCore/contentextensions/DFA.cpp (180259 => 180260)


--- trunk/Source/WebCore/contentextensions/DFA.cpp	2015-02-18 08:57:36 UTC (rev 180259)
+++ trunk/Source/WebCore/contentextensions/DFA.cpp	2015-02-18 08:57:43 UTC (rev 180260)
@@ -59,31 +59,6 @@
     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)
 {

Modified: trunk/Source/WebCore/contentextensions/DFA.h (180259 => 180260)


--- trunk/Source/WebCore/contentextensions/DFA.h	2015-02-18 08:57:36 UTC (rev 180259)
+++ trunk/Source/WebCore/contentextensions/DFA.h	2015-02-18 08:57:43 UTC (rev 180260)
@@ -46,10 +46,11 @@
     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;

Added: trunk/Source/WebCore/contentextensions/DFACompiler.cpp (0 => 180260)


--- trunk/Source/WebCore/contentextensions/DFACompiler.cpp	                        (rev 0)
+++ trunk/Source/WebCore/contentextensions/DFACompiler.cpp	2015-02-18 08:57:43 UTC (rev 180260)
@@ -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 <_javascript_Core/GPRInfo.h>
+#include <_javascript_Core/LinkBuffer.h>
+#include <_javascript_Core/MacroAssembler.h>
+#include <_javascript_Core/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)

Added: trunk/Source/WebCore/contentextensions/DFACompiler.h (0 => 180260)


--- trunk/Source/WebCore/contentextensions/DFACompiler.h	                        (rev 0)
+++ trunk/Source/WebCore/contentextensions/DFACompiler.h	2015-02-18 08:57:43 UTC (rev 180260)
@@ -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 <_javascript_Core/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
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to