<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[180260] trunk/Source/WebCore</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/180260">180260</a></dd>
<dt>Author</dt> <dd>benjamin@webkit.org</dd>
<dt>Date</dt> <dd>2015-02-18 00:57:43 -0800 (Wed, 18 Feb 2015)</dd>
</dl>

<h3>Log Message</h3>
<pre>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.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCoreWebCorexcodeprojprojectpbxproj">trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj</a></li>
<li><a href="#trunkSourceWebCorecontentextensionsContentExtensionsBackendcpp">trunk/Source/WebCore/contentextensions/ContentExtensionsBackend.cpp</a></li>
<li><a href="#trunkSourceWebCorecontentextensionsContentExtensionsBackendh">trunk/Source/WebCore/contentextensions/ContentExtensionsBackend.h</a></li>
<li><a href="#trunkSourceWebCorecontentextensionsDFAcpp">trunk/Source/WebCore/contentextensions/DFA.cpp</a></li>
<li><a href="#trunkSourceWebCorecontentextensionsDFAh">trunk/Source/WebCore/contentextensions/DFA.h</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunkSourceWebCorecontentextensionsDFACompilercpp">trunk/Source/WebCore/contentextensions/DFACompiler.cpp</a></li>
<li><a href="#trunkSourceWebCorecontentextensionsDFACompilerh">trunk/Source/WebCore/contentextensions/DFACompiler.h</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (180259 => 180260)</h4>
<pre class="diff"><span>
<span class="info">--- 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)
</span><span class="lines">@@ -1,3 +1,36 @@
</span><ins>+2015-02-17  Benjamin Poulain  &lt;benjamin@webkit.org&gt;
+
+        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.
+
</ins><span class="cx"> 2015-02-17  Chris Dumez  &lt;cdumez@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Rename ScopedEventQueue::instance() to singleton()
</span></span></pre></div>
<a id="trunkSourceWebCoreWebCorexcodeprojprojectpbxproj"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj (180259 => 180260)</h4>
<pre class="diff"><span>
<span class="info">--- 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)
</span><span class="lines">@@ -1029,6 +1029,8 @@
</span><span class="cx">                 26C15CF71857E15E00F15C03 /* ResourceHandleCFURLConnectionDelegateWithOperationQueue.h in Headers */ = {isa = PBXBuildFile; fileRef = 26C15CF51857E15D00F15C03 /* ResourceHandleCFURLConnectionDelegateWithOperationQueue.h */; };
</span><span class="cx">                 26C17A3E1491D2D400D12BA2 /* FileSystemIOS.h in Headers */ = {isa = PBXBuildFile; fileRef = 26C17A3C1491D2D400D12BA2 /* FileSystemIOS.h */; settings = {ATTRIBUTES = (Private, ); }; };
</span><span class="cx">                 26C17A3F1491D2D400D12BA2 /* FileSystemIOS.mm in Sources */ = {isa = PBXBuildFile; fileRef = 26C17A3D1491D2D400D12BA2 /* FileSystemIOS.mm */; };
</span><ins>+                26C560D31A783EBB008682E4 /* DFACompiler.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 26C560D11A783EBB008682E4 /* DFACompiler.cpp */; };
+                26C560D41A783EBB008682E4 /* DFACompiler.h in Headers */ = {isa = PBXBuildFile; fileRef = 26C560D21A783EBB008682E4 /* DFACompiler.h */; };
</ins><span class="cx">                 26E98A10130A9FCA008EB7B2 /* TextCodecASCIIFastPath.h in Headers */ = {isa = PBXBuildFile; fileRef = 26E98A0F130A9FCA008EB7B2 /* TextCodecASCIIFastPath.h */; };
</span><span class="cx">                 26F0C8971A2E724B002794F8 /* ContentExtensionsManager.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 26F0C8951A2E724B002794F8 /* ContentExtensionsManager.cpp */; };
</span><span class="cx">                 26F0C8981A2E724B002794F8 /* ContentExtensionsManager.h in Headers */ = {isa = PBXBuildFile; fileRef = 26F0C8961A2E724B002794F8 /* ContentExtensionsManager.h */; settings = {ATTRIBUTES = (Private, ); }; };
</span><span class="lines">@@ -8053,6 +8055,8 @@
</span><span class="cx">                 26C15CF51857E15D00F15C03 /* ResourceHandleCFURLConnectionDelegateWithOperationQueue.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ResourceHandleCFURLConnectionDelegateWithOperationQueue.h; sourceTree = &quot;&lt;group&gt;&quot;; };
</span><span class="cx">                 26C17A3C1491D2D400D12BA2 /* FileSystemIOS.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = FileSystemIOS.h; path = ios/FileSystemIOS.h; sourceTree = &quot;&lt;group&gt;&quot;; };
</span><span class="cx">                 26C17A3D1491D2D400D12BA2 /* FileSystemIOS.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = FileSystemIOS.mm; path = ios/FileSystemIOS.mm; sourceTree = &quot;&lt;group&gt;&quot;; };
</span><ins>+                26C560D11A783EBB008682E4 /* DFACompiler.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = DFACompiler.cpp; sourceTree = &quot;&lt;group&gt;&quot;; };
+                26C560D21A783EBB008682E4 /* DFACompiler.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DFACompiler.h; sourceTree = &quot;&lt;group&gt;&quot;; };
</ins><span class="cx">                 26E98A0F130A9FCA008EB7B2 /* TextCodecASCIIFastPath.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TextCodecASCIIFastPath.h; sourceTree = &quot;&lt;group&gt;&quot;; };
</span><span class="cx">                 26F0C8951A2E724B002794F8 /* ContentExtensionsManager.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ContentExtensionsManager.cpp; sourceTree = &quot;&lt;group&gt;&quot;; };
</span><span class="cx">                 26F0C8961A2E724B002794F8 /* ContentExtensionsManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ContentExtensionsManager.h; sourceTree = &quot;&lt;group&gt;&quot;; };
</span><span class="lines">@@ -15372,6 +15376,8 @@
</span><span class="cx">                                 26F0C8961A2E724B002794F8 /* ContentExtensionsManager.h */,
</span><span class="cx">                                 267725F61A5B3AD9003C24DD /* DFA.cpp */,
</span><span class="cx">                                 267725F71A5B3AD9003C24DD /* DFA.h */,
</span><ins>+                                26C560D11A783EBB008682E4 /* DFACompiler.cpp */,
+                                26C560D21A783EBB008682E4 /* DFACompiler.h */,
</ins><span class="cx">                                 267725F91A5B3AD9003C24DD /* DFANode.h */,
</span><span class="cx">                                 269397251A4A5FBD00E8349D /* NFA.cpp */,
</span><span class="cx">                                 269397231A4A5B6400E8349D /* NFA.h */,
</span><span class="lines">@@ -26314,6 +26320,7 @@
</span><span class="cx">                                 B22279C20D00BF220071B782 /* SVGException.h in Headers */,
</span><span class="cx">                                 B22279C50D00BF220071B782 /* SVGExternalResourcesRequired.h in Headers */,
</span><span class="cx">                                 B22279C80D00BF220071B782 /* SVGFEBlendElement.h in Headers */,
</span><ins>+                                26C560D41A783EBB008682E4 /* DFACompiler.h in Headers */,
</ins><span class="cx">                                 B22279CB0D00BF220071B782 /* SVGFEColorMatrixElement.h in Headers */,
</span><span class="cx">                                 B22279CE0D00BF220071B782 /* SVGFEComponentTransferElement.h in Headers */,
</span><span class="cx">                                 B22279D10D00BF220071B782 /* SVGFECompositeElement.h in Headers */,
</span><span class="lines">@@ -29526,6 +29533,7 @@
</span><span class="cx">                                 C5A1EA7C152BCF04004D00B6 /* SimplifyMarkupCommand.cpp in Sources */,
</span><span class="cx">                                 FD00D7A414A3F61900734011 /* SincResampler.cpp in Sources */,
</span><span class="cx">                                 51327D6111A33A2B004F9D65 /* SinkDocument.cpp in Sources */,
</span><ins>+                                26C560D31A783EBB008682E4 /* DFACompiler.cpp in Sources */,
</ins><span class="cx">                                 49E911CC0EF86D47009D0CAF /* SkewTransformOperation.cpp in Sources */,
</span><span class="cx">                                 4150F9F212B6E0E70008C860 /* SliderThumbElement.cpp in Sources */,
</span><span class="cx">                                 1C0939EA1A13E12900B788E5 /* CachedSVGFont.cpp in Sources */,
</span></span></pre></div>
<a id="trunkSourceWebCorecontentextensionsContentExtensionsBackendcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/contentextensions/ContentExtensionsBackend.cpp (180259 => 180260)</h4>
<pre class="diff"><span>
<span class="info">--- 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)
</span><span class="lines">@@ -29,6 +29,7 @@
</span><span class="cx"> #if ENABLE(CONTENT_EXTENSIONS)
</span><span class="cx"> 
</span><span class="cx"> #include &quot;ContentExtensionsDebugging.h&quot;
</span><ins>+#include &quot;DFACompiler.h&quot;
</ins><span class="cx"> #include &quot;NFA.h&quot;
</span><span class="cx"> #include &quot;NFAToDFA.h&quot;
</span><span class="cx"> #include &quot;URL.h&quot;
</span><span class="lines">@@ -42,6 +43,11 @@
</span><span class="cx"> 
</span><span class="cx"> namespace ContentExtensions {
</span><span class="cx"> 
</span><ins>+ContentExtensionsBackend::ContentExtensionsBackend()
+    : m_vm(JSC::VM::sharedInstance())
+{
+}
+
</ins><span class="cx"> void ContentExtensionsBackend::setRuleList(const String&amp; identifier, const Vector&lt;ContentExtensionRule&gt;&amp; ruleList)
</span><span class="cx"> {
</span><span class="cx">     ASSERT(!identifier.isEmpty());
</span><span class="lines">@@ -85,19 +91,24 @@
</span><span class="cx">     double dfaBuildTimeStart = monotonicallyIncreasingTime();
</span><span class="cx"> #endif
</span><span class="cx"> 
</span><del>-    CompiledContentExtension compiledContentExtension = { NFAToDFA::convert(nfa), ruleList };
</del><ins>+    const DFA dfa = NFAToDFA::convert(nfa);
</ins><span class="cx"> 
</span><span class="cx"> #if CONTENT_EXTENSIONS_PERFORMANCE_REPORTING
</span><span class="cx">     double dfaBuildTimeEnd = monotonicallyIncreasingTime();
</span><span class="cx">     dataLogF(&quot;    Time spent building the DFA: %f\n&quot;, (dfaBuildTimeEnd - dfaBuildTimeStart));
</span><span class="cx"> #endif
</span><span class="cx"> 
</span><del>-    // FIXME: never add a DFA that only matches the empty set.
</del><ins>+    JSC::MacroAssemblerCodeRef compiledDfa;
+    {
+        JSC::JSLockHolder locker(m_vm.get());
+        compiledDfa = compileDFA(dfa, m_vm.get());
+    }
</ins><span class="cx"> 
</span><span class="cx"> #if CONTENT_EXTENSIONS_STATE_MACHINE_DEBUGGING
</span><del>-    compiledContentExtension.dfa.debugPrintDot();
</del><ins>+    dfa.debugPrintDot();
</ins><span class="cx"> #endif
</span><span class="cx"> 
</span><ins>+    CompiledContentExtension compiledContentExtension = { compiledDfa, ruleList };
</ins><span class="cx">     m_ruleLists.set(identifier, compiledContentExtension);
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="lines">@@ -111,6 +122,11 @@
</span><span class="cx">     m_ruleLists.clear();
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+static void addActionToHashSet(TriggeredActionSet* triggeredActionSet, unsigned actionId)
+{
+    triggeredActionSet-&gt;add(actionId);
+}
+
</ins><span class="cx"> bool ContentExtensionsBackend::shouldBlockURL(const URL&amp; url)
</span><span class="cx"> {
</span><span class="cx">     const String&amp; urlString = url.string();
</span><span class="lines">@@ -118,21 +134,17 @@
</span><span class="cx"> 
</span><span class="cx">     for (auto&amp; ruleListSlot : m_ruleLists) {
</span><span class="cx">         CompiledContentExtension&amp; compiledContentExtension = ruleListSlot.value;
</span><del>-        unsigned state = compiledContentExtension.dfa.root();
</del><span class="cx"> 
</span><del>-        HashSet&lt;uint64_t, DefaultHash&lt;uint64_t&gt;::Hash, WTF::UnsignedWithZeroKeyHashTraits&lt;uint64_t&gt;&gt; triggeredActions;
</del><ins>+        CString urlCString = urlString.utf8();
+        PotentialPageLoadDescriptor potentialPageLoadDescriptor;
+        potentialPageLoadDescriptor.urlBuffer = urlCString.data();
+        potentialPageLoadDescriptor.urlBufferSize = urlCString.length();
</ins><span class="cx"> 
</span><del>-        for (unsigned i = 0; i &lt; urlString.length(); ++i) {
-            char character = static_cast&lt;char&gt;(urlString[i]);
-            bool ok;
-            state = compiledContentExtension.dfa.nextState(state, character, ok);
-            if (!ok)
-                break;
</del><ins>+        TriggeredActionSet triggeredActions;
</ins><span class="cx"> 
</span><del>-            const Vector&lt;uint64_t&gt;&amp; actions = compiledContentExtension.dfa.actions(state);
-            if (!actions.isEmpty())
-                triggeredActions.add(actions.begin(), actions.end());
-        }
</del><ins>+        void* compiledMatcher = compiledContentExtension.compiledMatcher.code().executableAddress();
+        reinterpret_cast&lt;CompiledRuleMatcher&gt;(compiledMatcher)(&amp;potentialPageLoadDescriptor, addActionToHashSet, &amp;triggeredActions);
+
</ins><span class="cx">         if (!triggeredActions.isEmpty()) {
</span><span class="cx">             Vector&lt;uint64_t&gt; sortedActions;
</span><span class="cx">             copyToVector(triggeredActions, sortedActions);
</span></span></pre></div>
<a id="trunkSourceWebCorecontentextensionsContentExtensionsBackendh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/contentextensions/ContentExtensionsBackend.h (180259 => 180260)</h4>
<pre class="diff"><span>
<span class="info">--- 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)
</span><span class="lines">@@ -30,6 +30,8 @@
</span><span class="cx"> 
</span><span class="cx"> #include &quot;ContentExtensionRule.h&quot;
</span><span class="cx"> #include &quot;DFA.h&quot;
</span><ins>+#include &lt;JavaScriptCore/MacroAssemblerCodeRef.h&gt;
+#include &lt;JavaScriptCore/VM.h&gt;
</ins><span class="cx"> #include &lt;wtf/HashMap.h&gt;
</span><span class="cx"> #include &lt;wtf/text/StringHash.h&gt;
</span><span class="cx"> #include &lt;wtf/text/WTFString.h&gt;
</span><span class="lines">@@ -47,6 +49,8 @@
</span><span class="cx"> // 2) It provides APIs for the WebCore interfaces to use those rules efficiently.
</span><span class="cx"> class ContentExtensionsBackend {
</span><span class="cx"> public:
</span><ins>+    ContentExtensionsBackend();
+
</ins><span class="cx">     // - Rule management interface. This can be used by upper layer.
</span><span class="cx"> 
</span><span class="cx">     // Set a list of rules for a given name. If there were existing rules for the name, they are overriden.
</span><span class="lines">@@ -60,10 +64,11 @@
</span><span class="cx"> 
</span><span class="cx"> private:
</span><span class="cx">     struct CompiledContentExtension {
</span><del>-        DFA dfa;
</del><ins>+        JSC::MacroAssemblerCodeRef compiledMatcher;
</ins><span class="cx">         Vector&lt;ContentExtensionRule&gt; ruleList;
</span><span class="cx">     };
</span><span class="cx"> 
</span><ins>+    Ref&lt;JSC::VM&gt; m_vm;
</ins><span class="cx">     HashMap&lt;String, CompiledContentExtension&gt; m_ruleLists;
</span><span class="cx"> };
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebCorecontentextensionsDFAcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/contentextensions/DFA.cpp (180259 => 180260)</h4>
<pre class="diff"><span>
<span class="info">--- 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)
</span><span class="lines">@@ -59,31 +59,6 @@
</span><span class="cx">     return *this;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-unsigned DFA::nextState(unsigned currentState, char character, bool&amp; ok) const
-{
-    ASSERT(currentState &lt; m_nodes.size());
-
-    const DFANode&amp; node = m_nodes[currentState];
-    auto nextNode = node.transitions.find(character);
-    if (nextNode != node.transitions.end()) {
-        ok = true;
-        return nextNode-&gt;value;
-    }
-    if (node.hasFallbackTransition) {
-        ok = true;
-        return node.fallbackTransition;
-    }
-    ok = false;
-    return 0;
-
-}
-
-const Vector&lt;uint64_t&gt;&amp; DFA::actions(unsigned currentState) const
-{
-    ASSERT(currentState &lt; m_nodes.size());
-    return m_nodes[currentState].actions;
-}
-
</del><span class="cx"> #if CONTENT_EXTENSIONS_STATE_MACHINE_DEBUGGING
</span><span class="cx"> static void printRange(bool firstRange, char rangeStart, char rangeEnd)
</span><span class="cx"> {
</span></span></pre></div>
<a id="trunkSourceWebCorecontentextensionsDFAh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/contentextensions/DFA.h (180259 => 180260)</h4>
<pre class="diff"><span>
<span class="info">--- 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)
</span><span class="lines">@@ -46,10 +46,11 @@
</span><span class="cx">     DFA&amp; operator=(const DFA&amp;);
</span><span class="cx"> 
</span><span class="cx">     unsigned root() const { return m_root; }
</span><del>-    // If there is a transition to a valid state on &quot;character&quot;, 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&amp; ok) const;
-    const Vector&lt;uint64_t&gt;&amp; actions(unsigned currentState) const;
</del><ins>+    unsigned size() const { return m_nodes.size(); }
+    const DFANode&amp; nodeAt(unsigned i) const
+    {
+        return m_nodes[i];
+    }
</ins><span class="cx"> 
</span><span class="cx"> #if CONTENT_EXTENSIONS_STATE_MACHINE_DEBUGGING
</span><span class="cx">     void debugPrintDot() const;
</span></span></pre></div>
<a id="trunkSourceWebCorecontentextensionsDFACompilercpp"></a>
<div class="addfile"><h4>Added: trunk/Source/WebCore/contentextensions/DFACompiler.cpp (0 => 180260)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/contentextensions/DFACompiler.cpp                                (rev 0)
+++ trunk/Source/WebCore/contentextensions/DFACompiler.cpp        2015-02-18 08:57:43 UTC (rev 180260)
</span><span class="lines">@@ -0,0 +1,192 @@
</span><ins>+/*
+ * 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 &quot;config.h&quot;
+#include &quot;DFACompiler.h&quot;
+
+#if ENABLE(CONTENT_EXTENSIONS)
+
+#include &quot;DFA.h&quot;
+#include &quot;DFANode.h&quot;
+#include &quot;RegisterAllocator.h&quot;
+#include &quot;StackAllocator.h&quot;
+#include &lt;JavaScriptCore/GPRInfo.h&gt;
+#include &lt;JavaScriptCore/LinkBuffer.h&gt;
+#include &lt;JavaScriptCore/MacroAssembler.h&gt;
+#include &lt;JavaScriptCore/VM.h&gt;
+
+namespace WebCore {
+
+namespace ContentExtensions {
+
+typedef JSC::MacroAssembler Assembler;
+
+class DFACodeGenerator {
+public:
+    explicit DFACodeGenerator(const DFA&amp;);
+    JSC::MacroAssemblerCodeRef compile(JSC::VM&amp;);
+
+private:
+    void lowerStateMachine();
+    void lowerNode(Assembler::JumpList&amp; failureCases, const DFANode&amp;);
+
+    void tryLoadingNextCharacter(Assembler::JumpList&amp; failureCases, LocalRegister&amp; output);
+    void callAddActionFunction(unsigned argument);
+
+    const DFA&amp; m_dfa;
+
+    Assembler m_assembler;
+    RegisterAllocator m_registerAllocator;
+    StackAllocator m_stackAllocator;
+
+    Assembler::RegisterID m_urlBufferOffsetRegister = InvalidGPRReg;
+    Vector&lt;Assembler::Label&gt; m_stateEntryLabels;
+    Vector&lt;Assembler::JumpList&gt; m_jumpsPerState;
+};
+
+JSC::MacroAssemblerCodeRef compileDFA(const DFA&amp; dfa, JSC::VM&amp; vm)
+{
+    DFACodeGenerator codeGenerator(dfa);
+    return codeGenerator.compile(vm);
+}
+
+DFACodeGenerator::DFACodeGenerator(const DFA&amp; dfa)
+    : m_dfa(dfa)
+    , m_stackAllocator(m_assembler)
+{
+}
+
+JSC::MacroAssemblerCodeRef DFACodeGenerator::compile(JSC::VM&amp; 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(&quot;Compiled Extension&quot;);
+#else
+    return FINALIZE_CODE(linkBuffer, (&quot;Compiled Extension&quot;));
+#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 &lt; dfaSize; ++i) {
+        if (i == root)
+            continue;
+        m_stateEntryLabels[i] = m_assembler.label();
+        lowerNode(failureCases, m_dfa.nodeAt(i));
+    }
+
+    for (unsigned i = 0; i &lt; dfaSize; ++i)
+        m_jumpsPerState[i].linkTo(m_stateEntryLabels[i], &amp;m_assembler);
+
+    failureCases.link(&amp;m_assembler);
+}
+
+void DFACodeGenerator::lowerNode(Assembler::JumpList&amp; failureCases, const DFANode&amp; dfaNode)
+{
+    for (uint64_t actionId : dfaNode.actions)
+        callAddActionFunction(static_cast&lt;unsigned&gt;(actionId));
+
+    LocalRegister characterRegister(m_registerAllocator);
+    tryLoadingNextCharacter(failureCases, characterRegister);
+
+    for (const auto&amp; 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&amp; failureCases, LocalRegister&amp; 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&amp; 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)
</ins></span></pre></div>
<a id="trunkSourceWebCorecontentextensionsDFACompilerh"></a>
<div class="addfile"><h4>Added: trunk/Source/WebCore/contentextensions/DFACompiler.h (0 => 180260)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/contentextensions/DFACompiler.h                                (rev 0)
+++ trunk/Source/WebCore/contentextensions/DFACompiler.h        2015-02-18 08:57:43 UTC (rev 180260)
</span><span class="lines">@@ -0,0 +1,58 @@
</span><ins>+/*
+ * 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 &lt;JavaScriptCore/MacroAssemblerCodeRef.h&gt;
+#include &lt;wtf/HashSet.h&gt;
+
+namespace WebCore {
+
+namespace ContentExtensions {
+
+class DFA;
+
+struct PotentialPageLoadDescriptor {
+    const char* urlBuffer;
+    unsigned urlBufferSize;
+};
+
+typedef HashSet&lt;unsigned, DefaultHash&lt;unsigned&gt;::Hash, WTF::UnsignedWithZeroKeyHashTraits&lt;unsigned&gt;&gt; TriggeredActionSet;
+
+typedef void (*AddActionFunction)(TriggeredActionSet*, unsigned);
+typedef void (*CompiledRuleMatcher)(PotentialPageLoadDescriptor*, AddActionFunction, TriggeredActionSet*);
+
+JSC::MacroAssemblerCodeRef compileDFA(const DFA&amp;, JSC::VM&amp;);
+
+} // namespace ContentExtensions
+
+} // namespace WebCore
+
+#endif // ENABLE(CONTENT_EXTENSIONS)
+
+#endif // DFACompiler_h
</ins></span></pre>
</div>
</div>

</body>
</html>