<!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>[164202] 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/164202">164202</a></dd>
<dt>Author</dt> <dd>benjamin@webkit.org</dd>
<dt>Date</dt> <dd>2014-02-16 19:14:48 -0800 (Sun, 16 Feb 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>Split compilation state between querySelector and CSS matching
https://bugs.webkit.org/show_bug.cgi?id=128869

Reviewed by Antti Koivisto.

Cleanup after recent changes:
-SelectorCompiler now has a SelectorContext defining if the code is compiled for QuerySelector
 or for ElementRuleCollector.
-Generalize m_selectorCannotMatchAnything by making it part of the FunctionType. FunctionType now
 fully represent the type of code generation and we don't rely implicitly on m_selectorFragments being
 empty.

* css/ElementRuleCollector.cpp:
(WebCore::ElementRuleCollector::ruleMatches):

* cssjit/SelectorCompiler.cpp:
(WebCore::SelectorCompiler::compileSelector):
(WebCore::SelectorCompiler::SelectorCodeGenerator::SelectorCodeGenerator):
There is no tree marking for QuerySelector, so we can generate a simple selector
for the sibling selectors.

(WebCore::SelectorCompiler::SelectorCodeGenerator::compile):
The code is split to make it simpler. The classic code generation has been moved
to generateSelectorChecker().
The decision on what to generate is reduced to a simple switch-case.

(WebCore::SelectorCompiler::SelectorCodeGenerator::generateSelectorChecker):
The code that was previously in compile().

(WebCore::SelectorCompiler::SelectorCodeGenerator::markParentElementIfResolvingStyle):
We should not generate tree marking on querySelector traversal. Since the constructor
now generate a SimpleChecker, it would also be incorrect to attempt to access the checkingContext
on the stack.
We can just skip the marking entierly.

* cssjit/SelectorCompiler.h:
* dom/SelectorQuery.cpp:
(WebCore::SelectorDataList::execute):
With the changes of SelectorContext, we can no longer generate a complex checker for querySelector.
This code may come back in the future but at the moment it is useless.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCorecssElementRuleCollectorcpp">trunk/Source/WebCore/css/ElementRuleCollector.cpp</a></li>
<li><a href="#trunkSourceWebCorecssjitSelectorCompilercpp">trunk/Source/WebCore/cssjit/SelectorCompiler.cpp</a></li>
<li><a href="#trunkSourceWebCorecssjitSelectorCompilerh">trunk/Source/WebCore/cssjit/SelectorCompiler.h</a></li>
<li><a href="#trunkSourceWebCoredomSelectorQuerycpp">trunk/Source/WebCore/dom/SelectorQuery.cpp</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (164201 => 164202)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog        2014-02-17 03:11:41 UTC (rev 164201)
+++ trunk/Source/WebCore/ChangeLog        2014-02-17 03:14:48 UTC (rev 164202)
</span><span class="lines">@@ -1,3 +1,46 @@
</span><ins>+2014-02-16  Benjamin Poulain  &lt;benjamin@webkit.org&gt;
+
+        Split compilation state between querySelector and CSS matching
+        https://bugs.webkit.org/show_bug.cgi?id=128869
+
+        Reviewed by Antti Koivisto.
+
+        Cleanup after recent changes:
+        -SelectorCompiler now has a SelectorContext defining if the code is compiled for QuerySelector
+         or for ElementRuleCollector.
+        -Generalize m_selectorCannotMatchAnything by making it part of the FunctionType. FunctionType now
+         fully represent the type of code generation and we don't rely implicitly on m_selectorFragments being
+         empty.
+
+        * css/ElementRuleCollector.cpp:
+        (WebCore::ElementRuleCollector::ruleMatches):
+
+        * cssjit/SelectorCompiler.cpp:
+        (WebCore::SelectorCompiler::compileSelector):
+        (WebCore::SelectorCompiler::SelectorCodeGenerator::SelectorCodeGenerator):
+        There is no tree marking for QuerySelector, so we can generate a simple selector
+        for the sibling selectors.
+
+        (WebCore::SelectorCompiler::SelectorCodeGenerator::compile):
+        The code is split to make it simpler. The classic code generation has been moved
+        to generateSelectorChecker().
+        The decision on what to generate is reduced to a simple switch-case.
+
+        (WebCore::SelectorCompiler::SelectorCodeGenerator::generateSelectorChecker):
+        The code that was previously in compile().
+
+        (WebCore::SelectorCompiler::SelectorCodeGenerator::markParentElementIfResolvingStyle):
+        We should not generate tree marking on querySelector traversal. Since the constructor
+        now generate a SimpleChecker, it would also be incorrect to attempt to access the checkingContext
+        on the stack.
+        We can just skip the marking entierly.
+
+        * cssjit/SelectorCompiler.h:
+        * dom/SelectorQuery.cpp:
+        (WebCore::SelectorDataList::execute):
+        With the changes of SelectorContext, we can no longer generate a complex checker for querySelector.
+        This code may come back in the future but at the moment it is useless.
+
</ins><span class="cx"> 2014-02-16  Benjamin Poulain  &lt;bpoulain@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         The FTP view is squished to the left
</span></span></pre></div>
<a id="trunkSourceWebCorecssElementRuleCollectorcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/css/ElementRuleCollector.cpp (164201 => 164202)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/css/ElementRuleCollector.cpp        2014-02-17 03:11:41 UTC (rev 164201)
+++ trunk/Source/WebCore/css/ElementRuleCollector.cpp        2014-02-17 03:14:48 UTC (rev 164202)
</span><span class="lines">@@ -295,7 +295,7 @@
</span><span class="cx">         JSC::VM* vm = m_element.document().scriptExecutionContext()-&gt;vm();
</span><span class="cx">         SelectorCompilationStatus compilationStatus;
</span><span class="cx">         JSC::MacroAssemblerCodeRef compiledSelectorCodeRef;
</span><del>-        compilationStatus = SelectorCompiler::compileSelector(ruleData.selector(), vm, compiledSelectorCodeRef);
</del><ins>+        compilationStatus = SelectorCompiler::compileSelector(ruleData.selector(), vm, SelectorCompiler::SelectorContext::RuleCollector, compiledSelectorCodeRef);
</ins><span class="cx"> 
</span><span class="cx">         ruleData.setCompiledSelector(compilationStatus, compiledSelectorCodeRef);
</span><span class="cx">         compiledSelectorChecker = ruleData.compiledSelectorCodeRef().code().executableAddress();
</span></span></pre></div>
<a id="trunkSourceWebCorecssjitSelectorCompilercpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/cssjit/SelectorCompiler.cpp (164201 => 164202)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/cssjit/SelectorCompiler.cpp        2014-02-17 03:11:41 UTC (rev 164201)
+++ trunk/Source/WebCore/cssjit/SelectorCompiler.cpp        2014-02-17 03:14:48 UTC (rev 164202)
</span><span class="lines">@@ -86,6 +86,7 @@
</span><span class="cx"> enum class FunctionType {
</span><span class="cx">     SimpleSelectorChecker,
</span><span class="cx">     SelectorCheckerWithCheckingContext,
</span><ins>+    CannotMatchAnything,
</ins><span class="cx">     CannotCompile
</span><span class="cx"> };
</span><span class="cx"> 
</span><span class="lines">@@ -118,7 +119,7 @@
</span><span class="cx"> 
</span><span class="cx"> class SelectorCodeGenerator {
</span><span class="cx"> public:
</span><del>-    SelectorCodeGenerator(const CSSSelector*);
</del><ins>+    SelectorCodeGenerator(const CSSSelector*, SelectorContext);
</ins><span class="cx">     SelectorCompilationStatus compile(JSC::VM*, JSC::MacroAssemblerCodeRef&amp;);
</span><span class="cx"> 
</span><span class="cx"> private:
</span><span class="lines">@@ -129,6 +130,7 @@
</span><span class="cx"> #endif
</span><span class="cx"> 
</span><span class="cx">     void computeBacktrackingInformation();
</span><ins>+    void generateSelectorChecker();
</ins><span class="cx"> 
</span><span class="cx">     // Element relations tree walker.
</span><span class="cx">     void generateWalkToParentElement(Assembler::JumpList&amp; failureCases, Assembler::RegisterID targetRegister);
</span><span class="lines">@@ -163,9 +165,9 @@
</span><span class="cx">     StackAllocator m_stackAllocator;
</span><span class="cx">     Vector&lt;std::pair&lt;Assembler::Call, JSC::FunctionPtr&gt;&gt; m_functionCalls;
</span><span class="cx"> 
</span><ins>+    SelectorContext m_selectorContext;
</ins><span class="cx">     FunctionType m_functionType;
</span><span class="cx">     SelectorFragmentList m_selectorFragments;
</span><del>-    bool m_selectorCannotMatchAnything;
</del><span class="cx"> 
</span><span class="cx">     StackAllocator::StackReference m_checkingContextStackReference;
</span><span class="cx"> 
</span><span class="lines">@@ -183,11 +185,11 @@
</span><span class="cx"> #endif
</span><span class="cx"> };
</span><span class="cx"> 
</span><del>-SelectorCompilationStatus compileSelector(const CSSSelector* lastSelector, JSC::VM* vm, JSC::MacroAssemblerCodeRef&amp; codeRef)
</del><ins>+SelectorCompilationStatus compileSelector(const CSSSelector* lastSelector, JSC::VM* vm, SelectorContext selectorContext, JSC::MacroAssemblerCodeRef&amp; codeRef)
</ins><span class="cx"> {
</span><span class="cx">     if (!vm-&gt;canUseJIT())
</span><span class="cx">         return SelectorCompilationStatus::CannotCompile;
</span><del>-    SelectorCodeGenerator codeGenerator(lastSelector);
</del><ins>+    SelectorCodeGenerator codeGenerator(lastSelector, selectorContext);
</ins><span class="cx">     return codeGenerator.compile(vm, codeRef);
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="lines">@@ -287,10 +289,10 @@
</span><span class="cx">     return FunctionType::CannotCompile;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-inline SelectorCodeGenerator::SelectorCodeGenerator(const CSSSelector* rootSelector)
</del><ins>+inline SelectorCodeGenerator::SelectorCodeGenerator(const CSSSelector* rootSelector, SelectorContext selectorContext)
</ins><span class="cx">     : m_stackAllocator(m_assembler)
</span><ins>+    , m_selectorContext(selectorContext)
</ins><span class="cx">     , m_functionType(FunctionType::SimpleSelectorChecker)
</span><del>-    , m_selectorCannotMatchAnything(false)
</del><span class="cx"> #if CSS_SELECTOR_JIT_DEBUGGING
</span><span class="cx">     , m_originalSelector(rootSelector)
</span><span class="cx"> #endif
</span><span class="lines">@@ -310,8 +312,10 @@
</span><span class="cx">         case CSSSelector::Id: {
</span><span class="cx">             const AtomicString&amp; id = selector-&gt;value();
</span><span class="cx">             if (fragment.id) {
</span><del>-                if (id != *fragment.id)
-                    goto InconsistentSelector;
</del><ins>+                if (id != *fragment.id) {
+                    m_functionType = FunctionType::CannotMatchAnything;
+                    return;
+                }
</ins><span class="cx">             } else
</span><span class="cx">                 fragment.id = &amp;(selector-&gt;value());
</span><span class="cx">             break;
</span><span class="lines">@@ -321,8 +325,8 @@
</span><span class="cx">             break;
</span><span class="cx">         case CSSSelector::PseudoClass:
</span><span class="cx">             m_functionType = mostRestrictiveFunctionType(m_functionType, addPseudoType(selector-&gt;pseudoType(), fragment));
</span><del>-            if (m_functionType == FunctionType::CannotCompile)
-                goto CannotHandleSelector;
</del><ins>+            if (m_functionType == FunctionType::CannotCompile || m_functionType == FunctionType::CannotMatchAnything)
+                return;
</ins><span class="cx">             break;
</span><span class="cx">         case CSSSelector::Set:
</span><span class="cx">             fragment.attributes.append(selector);
</span><span class="lines">@@ -346,8 +350,12 @@
</span><span class="cx">         if (relation == CSSSelector::ShadowDescendant &amp;&amp; !selector-&gt;isLastInTagHistory())
</span><span class="cx">             goto CannotHandleSelector;
</span><span class="cx"> 
</span><del>-        if (relation == CSSSelector::DirectAdjacent || relation == CSSSelector::IndirectAdjacent)
-            m_functionType = std::max(m_functionType, FunctionType::SelectorCheckerWithCheckingContext);
</del><ins>+        if (relation == CSSSelector::DirectAdjacent || relation == CSSSelector::IndirectAdjacent) {
+            FunctionType relationFunctionType = FunctionType::SelectorCheckerWithCheckingContext;
+            if (m_selectorContext == SelectorContext::QuerySelector)
+                relationFunctionType = FunctionType::SimpleSelectorChecker;
+            m_functionType = std::max(m_functionType, relationFunctionType);
+        }
</ins><span class="cx"> 
</span><span class="cx">         fragment.relationToLeftFragment = fragmentRelationForSelectorRelation(relation);
</span><span class="cx">         fragment.relationToRightFragment = relationToPreviousFragment;
</span><span class="lines">@@ -360,11 +368,8 @@
</span><span class="cx">     computeBacktrackingInformation();
</span><span class="cx"> 
</span><span class="cx">     return;
</span><del>-InconsistentSelector:
-    m_functionType = FunctionType::SimpleSelectorChecker;
-    m_selectorCannotMatchAnything = true;
</del><span class="cx"> CannotHandleSelector:
</span><del>-    m_selectorFragments.clear();
</del><ins>+    m_functionType = FunctionType::CannotCompile;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> static inline unsigned minimumRegisterRequirements(const SelectorFragmentList&amp; selectorFragments)
</span><span class="lines">@@ -401,102 +406,17 @@
</span><span class="cx"> 
</span><span class="cx"> inline SelectorCompilationStatus SelectorCodeGenerator::compile(JSC::VM* vm, JSC::MacroAssemblerCodeRef&amp; codeRef)
</span><span class="cx"> {
</span><del>-    if (m_selectorFragments.isEmpty() &amp;&amp; !m_selectorCannotMatchAnything)
-        return SelectorCompilationStatus::CannotCompile;
-
-    bool reservedCalleeSavedRegisters = false;
-    unsigned availableRegisterCount = m_registerAllocator.availableRegisterCount();
-    unsigned minimumRegisterCountForAttributes = minimumRegisterRequirements(m_selectorFragments);
-    if (availableRegisterCount &lt; minimumRegisterCountForAttributes) {
-        reservedCalleeSavedRegisters = true;
-        m_registerAllocator.reserveCalleeSavedRegisters(m_stackAllocator, minimumRegisterCountForAttributes - availableRegisterCount);
-    }
-
-    m_registerAllocator.allocateRegister(elementAddressRegister);
-
-    if (m_functionType == FunctionType::SelectorCheckerWithCheckingContext)
-        m_checkingContextStackReference = m_stackAllocator.push(checkingContextRegister);
-
-    Assembler::JumpList failureCases;
-
-    for (unsigned i = 0; i &lt; m_selectorFragments.size(); ++i) {
-        const SelectorFragment&amp; fragment = m_selectorFragments[i];
-        switch (fragment.relationToRightFragment) {
-        case FragmentRelation::Rightmost:
-            generateElementMatching(failureCases, fragment);
-            break;
-        case FragmentRelation::Descendant:
-            generateAncestorTreeWalker(failureCases, fragment);
-            break;
-        case FragmentRelation::Child:
-            generateParentElementTreeWalker(failureCases, fragment);
-            break;
-        case FragmentRelation::DirectAdjacent:
-            generateDirectAdjacentTreeWalker(failureCases, fragment);
-            break;
-        case FragmentRelation::IndirectAdjacent:
-            generateIndirectAdjacentTreeWalker(failureCases, fragment);
-            break;
-        }
-        generateBacktrackingTailsIfNeeded(fragment);
-    }
-
-    m_registerAllocator.deallocateRegister(elementAddressRegister);
-
-    if (m_functionType == FunctionType::SimpleSelectorChecker &amp;&amp; m_selectorCannotMatchAnything) {
</del><ins>+    switch (m_functionType) {
+    case FunctionType::SimpleSelectorChecker:
+    case FunctionType::SelectorCheckerWithCheckingContext:
+        generateSelectorChecker();
+        break;
+    case FunctionType::CannotMatchAnything:
</ins><span class="cx">         m_assembler.move(Assembler::TrustedImm32(0), returnRegister);
</span><span class="cx">         m_assembler.ret();
</span><del>-    } else if (m_functionType == FunctionType::SimpleSelectorChecker) {
-        // Success.
-        m_assembler.move(Assembler::TrustedImm32(1), returnRegister);
-        if (!reservedCalleeSavedRegisters)
-            m_assembler.ret();
-
-        // Failure.
-        if (!failureCases.empty()) {
-            Assembler::Jump skipFailureCase;
-            if (reservedCalleeSavedRegisters)
-                skipFailureCase = m_assembler.jump();
-
-            failureCases.link(&amp;m_assembler);
-            m_assembler.move(Assembler::TrustedImm32(0), returnRegister);
-
-            if (!reservedCalleeSavedRegisters)
-                m_assembler.ret();
-            else
-                skipFailureCase.link(&amp;m_assembler);
-        }
-        if (reservedCalleeSavedRegisters) {
-            m_registerAllocator.restoreCalleeSavedRegisters(m_stackAllocator);
-            m_assembler.ret();
-        }
-    } else {
-        ASSERT(m_functionType == FunctionType::SelectorCheckerWithCheckingContext);
-        ASSERT(!m_selectorCannotMatchAnything);
-
-        // Success.
-        m_assembler.move(Assembler::TrustedImm32(1), returnRegister);
-
-        StackAllocator successStack = m_stackAllocator;
-        StackAllocator failureStack = m_stackAllocator;
-
-        LocalRegister checkingContextRegister(m_registerAllocator);
-        successStack.pop(m_checkingContextStackReference, checkingContextRegister);
-
-        // Failure.
-        if (!failureCases.empty()) {
-            Assembler::Jump skipFailureCase = m_assembler.jump();
-
-            failureCases.link(&amp;m_assembler);
-            failureStack.discard();
-            m_assembler.move(Assembler::TrustedImm32(0), returnRegister);
-
-            skipFailureCase.link(&amp;m_assembler);
-        }
-
-        m_stackAllocator.merge(std::move(successStack), std::move(failureStack));
-        m_registerAllocator.restoreCalleeSavedRegisters(m_stackAllocator);
-        m_assembler.ret();
</del><ins>+        break;
+    case FunctionType::CannotCompile:
+        return SelectorCompilationStatus::CannotCompile;
</ins><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     JSC::LinkBuffer linkBuffer(*vm, &amp;m_assembler, CSS_CODE_ID);
</span><span class="lines">@@ -509,7 +429,7 @@
</span><span class="cx">     codeRef = FINALIZE_CODE(linkBuffer, (&quot;CSS Selector JIT&quot;));
</span><span class="cx"> #endif
</span><span class="cx"> 
</span><del>-    if (m_functionType == FunctionType::SimpleSelectorChecker)
</del><ins>+    if (m_functionType == FunctionType::SimpleSelectorChecker || m_functionType == FunctionType::CannotMatchAnything)
</ins><span class="cx">         return SelectorCompilationStatus::SimpleSelectorChecker;
</span><span class="cx">     return SelectorCompilationStatus::SelectorCheckerWithCheckingContext;
</span><span class="cx"> }
</span><span class="lines">@@ -662,6 +582,100 @@
</span><span class="cx">     }
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+void SelectorCodeGenerator::generateSelectorChecker()
+{
+    bool reservedCalleeSavedRegisters = false;
+    unsigned availableRegisterCount = m_registerAllocator.availableRegisterCount();
+    unsigned minimumRegisterCountForAttributes = minimumRegisterRequirements(m_selectorFragments);
+    if (availableRegisterCount &lt; minimumRegisterCountForAttributes) {
+        reservedCalleeSavedRegisters = true;
+        m_registerAllocator.reserveCalleeSavedRegisters(m_stackAllocator, minimumRegisterCountForAttributes - availableRegisterCount);
+    }
+
+    m_registerAllocator.allocateRegister(elementAddressRegister);
+
+    if (m_functionType == FunctionType::SelectorCheckerWithCheckingContext)
+        m_checkingContextStackReference = m_stackAllocator.push(checkingContextRegister);
+
+    Assembler::JumpList failureCases;
+
+    for (unsigned i = 0; i &lt; m_selectorFragments.size(); ++i) {
+        const SelectorFragment&amp; fragment = m_selectorFragments[i];
+        switch (fragment.relationToRightFragment) {
+        case FragmentRelation::Rightmost:
+            generateElementMatching(failureCases, fragment);
+            break;
+        case FragmentRelation::Descendant:
+            generateAncestorTreeWalker(failureCases, fragment);
+            break;
+        case FragmentRelation::Child:
+            generateParentElementTreeWalker(failureCases, fragment);
+            break;
+        case FragmentRelation::DirectAdjacent:
+            generateDirectAdjacentTreeWalker(failureCases, fragment);
+            break;
+        case FragmentRelation::IndirectAdjacent:
+            generateIndirectAdjacentTreeWalker(failureCases, fragment);
+            break;
+        }
+        generateBacktrackingTailsIfNeeded(fragment);
+    }
+
+    m_registerAllocator.deallocateRegister(elementAddressRegister);
+
+    if (m_functionType == FunctionType::SimpleSelectorChecker) {
+        // Success.
+        m_assembler.move(Assembler::TrustedImm32(1), returnRegister);
+        if (!reservedCalleeSavedRegisters)
+            m_assembler.ret();
+
+        // Failure.
+        if (!failureCases.empty()) {
+            Assembler::Jump skipFailureCase;
+            if (reservedCalleeSavedRegisters)
+                skipFailureCase = m_assembler.jump();
+
+            failureCases.link(&amp;m_assembler);
+            m_assembler.move(Assembler::TrustedImm32(0), returnRegister);
+
+            if (!reservedCalleeSavedRegisters)
+                m_assembler.ret();
+            else
+                skipFailureCase.link(&amp;m_assembler);
+        }
+        if (reservedCalleeSavedRegisters) {
+            m_registerAllocator.restoreCalleeSavedRegisters(m_stackAllocator);
+            m_assembler.ret();
+        }
+    } else {
+        ASSERT(m_functionType == FunctionType::SelectorCheckerWithCheckingContext);
+
+        // Success.
+        m_assembler.move(Assembler::TrustedImm32(1), returnRegister);
+
+        StackAllocator successStack = m_stackAllocator;
+        StackAllocator failureStack = m_stackAllocator;
+
+        LocalRegister checkingContextRegister(m_registerAllocator);
+        successStack.pop(m_checkingContextStackReference, checkingContextRegister);
+
+        // Failure.
+        if (!failureCases.empty()) {
+            Assembler::Jump skipFailureCase = m_assembler.jump();
+
+            failureCases.link(&amp;m_assembler);
+            failureStack.discard();
+            m_assembler.move(Assembler::TrustedImm32(0), returnRegister);
+
+            skipFailureCase.link(&amp;m_assembler);
+        }
+
+        m_stackAllocator.merge(std::move(successStack), std::move(failureStack));
+        m_registerAllocator.restoreCalleeSavedRegisters(m_stackAllocator);
+        m_assembler.ret();
+    }
+}
+
</ins><span class="cx"> static inline Assembler::Jump testIsElementFlagOnNode(Assembler::ResultCondition condition, Assembler&amp; assembler, Assembler::RegisterID nodeAddress)
</span><span class="cx"> {
</span><span class="cx">     return assembler.branchTest32(condition, Assembler::Address(nodeAddress, Node::nodeFlagsMemoryOffset()), Assembler::TrustedImm32(Node::flagIsElement()));
</span><span class="lines">@@ -778,6 +792,9 @@
</span><span class="cx"> 
</span><span class="cx"> void SelectorCodeGenerator::markParentElementIfResolvingStyle(JSC::FunctionPtr markingFunction)
</span><span class="cx"> {
</span><ins>+    if (m_selectorContext == SelectorContext::QuerySelector)
+        return;
+
</ins><span class="cx">     //     if (checkingContext.resolvingMode == ResolvingStyle) {
</span><span class="cx">     //         Element* parent = element-&gt;parentNode();
</span><span class="cx">     //         markingFunction(parent);
</span></span></pre></div>
<a id="trunkSourceWebCorecssjitSelectorCompilerh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/cssjit/SelectorCompiler.h (164201 => 164202)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/cssjit/SelectorCompiler.h        2014-02-17 03:11:41 UTC (rev 164201)
+++ trunk/Source/WebCore/cssjit/SelectorCompiler.h        2014-02-17 03:14:48 UTC (rev 164202)
</span><span class="lines">@@ -72,9 +72,17 @@
</span><span class="cx">     RenderStyle* elementStyle;
</span><span class="cx"> };
</span><span class="cx"> 
</span><ins>+enum class SelectorContext {
+    // Rule Collector needs a resolvingMode and can modify the tree as it matches.
+    RuleCollector,
+
+    // Query Selector does not modify the tree and never match :visited.
+    QuerySelector
+};
+
</ins><span class="cx"> typedef unsigned (*SimpleSelectorChecker)(Element*);
</span><span class="cx"> typedef unsigned (*SelectorCheckerWithCheckingContext)(Element*, const CheckingContext*);
</span><del>-SelectorCompilationStatus compileSelector(const CSSSelector*, JSC::VM*, JSC::MacroAssemblerCodeRef&amp; outputCodeRef);
</del><ins>+SelectorCompilationStatus compileSelector(const CSSSelector*, JSC::VM*, SelectorContext, JSC::MacroAssemblerCodeRef&amp; outputCodeRef);
</ins><span class="cx"> 
</span><span class="cx"> inline SimpleSelectorChecker simpleSelectorCheckerFunction(void* executableAddress, SelectorCompilationStatus compilationStatus)
</span><span class="cx"> {
</span></span></pre></div>
<a id="trunkSourceWebCoredomSelectorQuerycpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/dom/SelectorQuery.cpp (164201 => 164202)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/dom/SelectorQuery.cpp        2014-02-17 03:11:41 UTC (rev 164201)
+++ trunk/Source/WebCore/dom/SelectorQuery.cpp        2014-02-17 03:14:48 UTC (rev 164202)
</span><span class="lines">@@ -308,23 +308,14 @@
</span><span class="cx">             void* compiledSelectorChecker = selectorData.compiledSelectorCodeRef.code().executableAddress();
</span><span class="cx">             if (!compiledSelectorChecker &amp;&amp; selectorData.compilationStatus == SelectorCompilationStatus::NotCompiled) {
</span><span class="cx">                 JSC::VM* vm = rootNode.document().scriptExecutionContext()-&gt;vm();
</span><del>-                selectorData.compilationStatus = SelectorCompiler::compileSelector(selectorData.selector, vm, selectorData.compiledSelectorCodeRef);
</del><ins>+                selectorData.compilationStatus = SelectorCompiler::compileSelector(selectorData.selector, vm, SelectorCompiler::SelectorContext::QuerySelector, selectorData.compiledSelectorCodeRef);
+                RELEASE_ASSERT(selectorData.compilationStatus != SelectorCompilationStatus::SelectorCheckerWithCheckingContext);
</ins><span class="cx">                 compiledSelectorChecker = selectorData.compiledSelectorCodeRef.code().executableAddress();
</span><span class="cx">             }
</span><span class="cx"> 
</span><span class="cx">             if (compiledSelectorChecker) {
</span><del>-                if (selectorData.compilationStatus == SelectorCompilationStatus::SimpleSelectorChecker) {
-                    SelectorCompiler::SimpleSelectorChecker selectorChecker = SelectorCompiler::simpleSelectorCheckerFunction(compiledSelectorChecker, selectorData.compilationStatus);
-                    executeCompiledSimpleSelectorChecker&lt;SelectorQueryTrait&gt;(rootNode, selectorChecker, output);
-                } else {
-                    ASSERT(selectorData.compilationStatus == SelectorCompilationStatus::SelectorCheckerWithCheckingContext);
-                    SelectorCompiler::SelectorCheckerWithCheckingContext selectorChecker = SelectorCompiler::selectorCheckerFunctionWithCheckingContext(compiledSelectorChecker, selectorData.compilationStatus);
-
-                    SelectorCompiler::CheckingContext context;
-                    context.elementStyle = nullptr;
-                    context.resolvingMode = SelectorChecker::QueryingRules;
-                    executeCompiledSelectorCheckerWithContext&lt;SelectorQueryTrait&gt;(rootNode, selectorChecker, context, output);
-                }
</del><ins>+                SelectorCompiler::SimpleSelectorChecker selectorChecker = SelectorCompiler::simpleSelectorCheckerFunction(compiledSelectorChecker, selectorData.compilationStatus);
+                executeCompiledSimpleSelectorChecker&lt;SelectorQueryTrait&gt;(rootNode, selectorChecker, output);
</ins><span class="cx">                 return;
</span><span class="cx">             }
</span><span class="cx"> #endif // ENABLE(CSS_SELECTOR_JIT)
</span></span></pre>
</div>
</div>

</body>
</html>