<!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>[189325] trunk/Source/JavaScriptCore</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/189325">189325</a></dd>
<dt>Author</dt> <dd>basile_clement@apple.com</dd>
<dt>Date</dt> <dd>2015-09-03 17:25:19 -0700 (Thu, 03 Sep 2015)</dd>
</dl>

<h3>Log Message</h3>
<pre>JavaScript functions should restore the stack pointer after a call
https://bugs.webkit.org/show_bug.cgi?id=148659

Reviewed by Michael Saboff.

This patch makes it so that the various places where we are making a
JS-to-JS call restore the stack pointer afterwards. This allows us to
no longer rely on the stack pointer still being valid after a call, and
is a prerequisite for getting rid of the arity fixup return thunk.

* dfg/DFGSpeculativeJIT32_64.cpp:
(JSC::DFG::SpeculativeJIT::emitCall):
* dfg/DFGSpeculativeJIT64.cpp:
(JSC::DFG::SpeculativeJIT::emitCall):
* ftl/FTLCompile.cpp:
(JSC::FTL::mmAllocateDataSection):
* ftl/FTLInlineCacheSize.cpp:
(JSC::FTL::sizeOfCall):
* ftl/FTLJSCall.cpp:
(JSC::FTL::JSCall::emit):
* ftl/FTLJSCall.h:
* ftl/FTLStackMaps.h:
(JSC::FTL::StackMaps::stackSizeForLocals):
* jit/Repatch.cpp:
(JSC::generateByIdStub):
* tests/stress/tail-call-in-inline-cache.js: Added.
(tail):
(obj.get x):</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceJavaScriptCoreChangeLog">trunk/Source/JavaScriptCore/ChangeLog</a></li>
<li><a href="#trunkSourceJavaScriptCoredfgDFGSpeculativeJIT32_64cpp">trunk/Source/JavaScriptCore/dfg/DFGSpeculativeJIT32_64.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCoredfgDFGSpeculativeJIT64cpp">trunk/Source/JavaScriptCore/dfg/DFGSpeculativeJIT64.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCoreftlFTLCompilecpp">trunk/Source/JavaScriptCore/ftl/FTLCompile.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCoreftlFTLInlineCacheSizecpp">trunk/Source/JavaScriptCore/ftl/FTLInlineCacheSize.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCoreftlFTLJSCallcpp">trunk/Source/JavaScriptCore/ftl/FTLJSCall.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCoreftlFTLJSCallh">trunk/Source/JavaScriptCore/ftl/FTLJSCall.h</a></li>
<li><a href="#trunkSourceJavaScriptCoreftlFTLStackMapsh">trunk/Source/JavaScriptCore/ftl/FTLStackMaps.h</a></li>
<li><a href="#trunkSourceJavaScriptCorejitRepatchcpp">trunk/Source/JavaScriptCore/jit/Repatch.cpp</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunkSourceJavaScriptCoretestsstresstailcallininlinecachejs">trunk/Source/JavaScriptCore/tests/stress/tail-call-in-inline-cache.js</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceJavaScriptCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/ChangeLog (189324 => 189325)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/ChangeLog        2015-09-04 00:09:00 UTC (rev 189324)
+++ trunk/Source/JavaScriptCore/ChangeLog        2015-09-04 00:25:19 UTC (rev 189325)
</span><span class="lines">@@ -1,3 +1,34 @@
</span><ins>+2015-09-03  Basile Clement  &lt;basile_clement@apple.com&gt;
+
+        JavaScript functions should restore the stack pointer after a call
+        https://bugs.webkit.org/show_bug.cgi?id=148659
+
+        Reviewed by Michael Saboff.
+
+        This patch makes it so that the various places where we are making a
+        JS-to-JS call restore the stack pointer afterwards. This allows us to
+        no longer rely on the stack pointer still being valid after a call, and
+        is a prerequisite for getting rid of the arity fixup return thunk.
+
+        * dfg/DFGSpeculativeJIT32_64.cpp:
+        (JSC::DFG::SpeculativeJIT::emitCall):
+        * dfg/DFGSpeculativeJIT64.cpp:
+        (JSC::DFG::SpeculativeJIT::emitCall):
+        * ftl/FTLCompile.cpp:
+        (JSC::FTL::mmAllocateDataSection):
+        * ftl/FTLInlineCacheSize.cpp:
+        (JSC::FTL::sizeOfCall):
+        * ftl/FTLJSCall.cpp:
+        (JSC::FTL::JSCall::emit):
+        * ftl/FTLJSCall.h:
+        * ftl/FTLStackMaps.h:
+        (JSC::FTL::StackMaps::stackSizeForLocals):
+        * jit/Repatch.cpp:
+        (JSC::generateByIdStub):
+        * tests/stress/tail-call-in-inline-cache.js: Added.
+        (tail):
+        (obj.get x):
+
</ins><span class="cx"> 2015-09-03  Filip Pizlo  &lt;fpizlo@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         StructureStubInfo should be able to reset itself without going through CodeBlock
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoredfgDFGSpeculativeJIT32_64cpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/dfg/DFGSpeculativeJIT32_64.cpp (189324 => 189325)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/dfg/DFGSpeculativeJIT32_64.cpp        2015-09-04 00:09:00 UTC (rev 189324)
+++ trunk/Source/JavaScriptCore/dfg/DFGSpeculativeJIT32_64.cpp        2015-09-04 00:25:19 UTC (rev 189325)
</span><span class="lines">@@ -821,9 +821,10 @@
</span><span class="cx">     info-&gt;setUpCall(callType, node-&gt;origin.semantic, calleePayloadGPR);
</span><span class="cx">     m_jit.addJSCall(fastCall, slowCall, targetToCheck, info);
</span><span class="cx">     
</span><del>-    // If we were varargs, then after the calls are done, we need to reestablish our stack pointer.
-    if (isVarargs || isForwardVarargs)
-        m_jit.addPtr(TrustedImm32(m_jit.graph().stackPointerOffset() * sizeof(Register)), GPRInfo::callFrameRegister, JITCompiler::stackPointerRegister);
</del><ins>+    // After the calls are done, we need to reestablish our stack
+    // pointer. We rely on this for varargs calls, calls with arity
+    // mismatch (the callframe is slided) and tail calls.
+    m_jit.addPtr(TrustedImm32(m_jit.graph().stackPointerOffset() * sizeof(Register)), GPRInfo::callFrameRegister, JITCompiler::stackPointerRegister);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> template&lt;bool strict&gt;
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoredfgDFGSpeculativeJIT64cpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/dfg/DFGSpeculativeJIT64.cpp (189324 => 189325)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/dfg/DFGSpeculativeJIT64.cpp        2015-09-04 00:09:00 UTC (rev 189324)
+++ trunk/Source/JavaScriptCore/dfg/DFGSpeculativeJIT64.cpp        2015-09-04 00:25:19 UTC (rev 189325)
</span><span class="lines">@@ -776,10 +776,11 @@
</span><span class="cx">     
</span><span class="cx">     callLinkInfo-&gt;setUpCall(callType, m_currentNode-&gt;origin.semantic,  calleeGPR);    
</span><span class="cx">     m_jit.addJSCall(fastCall, slowCall, targetToCheck, callLinkInfo);
</span><del>-    
-    // If we were varargs, then after the calls are done, we need to reestablish our stack pointer.
-    if (isVarargs || isForwardVarargs)
-        m_jit.addPtr(TrustedImm32(m_jit.graph().stackPointerOffset() * sizeof(Register)), GPRInfo::callFrameRegister, JITCompiler::stackPointerRegister);
</del><ins>+
+    // After the calls are done, we need to reestablish our stack
+    // pointer. We rely on this for varargs calls, calls with arity
+    // mismatch (the callframe is slided) and tail calls.
+    m_jit.addPtr(TrustedImm32(m_jit.graph().stackPointerOffset() * sizeof(Register)), GPRInfo::callFrameRegister, JITCompiler::stackPointerRegister);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> // Clang should allow unreachable [[clang::fallthrough]] in template functions if any template expansion uses it
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreftlFTLCompilecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/ftl/FTLCompile.cpp (189324 => 189325)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/ftl/FTLCompile.cpp        2015-09-04 00:09:00 UTC (rev 189324)
+++ trunk/Source/JavaScriptCore/ftl/FTLCompile.cpp        2015-09-04 00:25:19 UTC (rev 189325)
</span><span class="lines">@@ -593,7 +593,7 @@
</span><span class="cx">         JSCall&amp; call = state.jsCalls[i];
</span><span class="cx"> 
</span><span class="cx">         CCallHelpers fastPathJIT(&amp;vm, codeBlock);
</span><del>-        call.emit(fastPathJIT);
</del><ins>+        call.emit(fastPathJIT, state.jitCode-&gt;stackmaps.stackSizeForLocals());
</ins><span class="cx"> 
</span><span class="cx">         char* startOfIC = bitwise_cast&lt;char*&gt;(generatedFunction) + call.m_instructionOffset;
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreftlFTLInlineCacheSizecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/ftl/FTLInlineCacheSize.cpp (189324 => 189325)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/ftl/FTLInlineCacheSize.cpp        2015-09-04 00:09:00 UTC (rev 189324)
+++ trunk/Source/JavaScriptCore/ftl/FTLInlineCacheSize.cpp        2015-09-04 00:25:19 UTC (rev 189325)
</span><span class="lines">@@ -67,9 +67,9 @@
</span><span class="cx"> size_t sizeOfCall()
</span><span class="cx"> {
</span><span class="cx"> #if CPU(ARM64)
</span><del>-    return 56;
</del><ins>+    return 60;
</ins><span class="cx"> #else
</span><del>-    return 53;
</del><ins>+    return 60;
</ins><span class="cx"> #endif
</span><span class="cx"> }
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreftlFTLJSCallcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/ftl/FTLJSCall.cpp (189324 => 189325)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/ftl/FTLJSCall.cpp        2015-09-04 00:09:00 UTC (rev 189324)
+++ trunk/Source/JavaScriptCore/ftl/FTLJSCall.cpp        2015-09-04 00:25:19 UTC (rev 189325)
</span><span class="lines">@@ -51,6 +51,13 @@
</span><span class="cx">     ASSERT(node-&gt;op() == Call || node-&gt;op() == Construct);
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+void JSCall::emit(CCallHelpers&amp; jit, unsigned stackSizeForLocals)
+{
+    JSCallBase::emit(jit);
+
+    jit.addPtr(CCallHelpers::TrustedImm32(- static_cast&lt;int64_t&gt;(stackSizeForLocals)), CCallHelpers::framePointerRegister, CCallHelpers::stackPointerRegister);
+}
+
</ins><span class="cx"> } } // namespace JSC::FTL
</span><span class="cx"> 
</span><span class="cx"> #endif // ENABLE(FTL_JIT)
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreftlFTLJSCallh"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/ftl/FTLJSCall.h (189324 => 189325)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/ftl/FTLJSCall.h        2015-09-04 00:09:00 UTC (rev 189324)
+++ trunk/Source/JavaScriptCore/ftl/FTLJSCall.h        2015-09-04 00:25:19 UTC (rev 189325)
</span><span class="lines">@@ -42,6 +42,8 @@
</span><span class="cx"> public:
</span><span class="cx">     JSCall();
</span><span class="cx">     JSCall(unsigned stackmapID, DFG::Node*);
</span><ins>+
+    void emit(CCallHelpers&amp;, unsigned stackSizeForLocals);
</ins><span class="cx">     
</span><span class="cx">     unsigned stackmapID() const { return m_stackmapID; }
</span><span class="cx">     
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreftlFTLStackMapsh"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/ftl/FTLStackMaps.h (189324 => 189325)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/ftl/FTLStackMaps.h        2015-09-04 00:09:00 UTC (rev 189324)
+++ trunk/Source/JavaScriptCore/ftl/FTLStackMaps.h        2015-09-04 00:25:19 UTC (rev 189325)
</span><span class="lines">@@ -127,6 +127,19 @@
</span><span class="cx">     RecordMap computeRecordMap() const;
</span><span class="cx"> 
</span><span class="cx">     unsigned stackSize() const;
</span><ins>+
+    unsigned stackSizeForLocals() const
+    {
+#if CPU(X86_64)
+        // LLVM will store fp in the call frame
+        return stackSize() - sizeof(void*);
+#elif CPU(ARM64)
+        // LLVM will store fp &amp; lr in the call frame
+        return stackSize() - 2 * sizeof(void*);
+#else
+        UNREACHABLE_FOR_PLATFORM();
+#endif
+    }
</ins><span class="cx"> };
</span><span class="cx"> 
</span><span class="cx"> } } // namespace JSC::FTL
</span></span></pre></div>
<a id="trunkSourceJavaScriptCorejitRepatchcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/jit/Repatch.cpp (189324 => 189325)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/jit/Repatch.cpp        2015-09-04 00:09:00 UTC (rev 189324)
+++ trunk/Source/JavaScriptCore/jit/Repatch.cpp        2015-09-04 00:25:19 UTC (rev 189325)
</span><span class="lines">@@ -492,9 +492,10 @@
</span><span class="cx">                 MacroAssembler::TrustedImmPtr(0));
</span><span class="cx">             
</span><span class="cx">             fastPathCall = stubJit.nearCall();
</span><del>-            
</del><ins>+
</ins><span class="cx">             stubJit.addPtr(
</span><del>-                MacroAssembler::TrustedImm32(alignedNumberOfBytesForCall),
</del><ins>+                MacroAssembler::TrustedImm32(codeBlock-&gt;stackPointerOffset() * sizeof(Register)),
+                GPRInfo::callFrameRegister,
</ins><span class="cx">                 MacroAssembler::stackPointerRegister);
</span><span class="cx">             if (kind == CallGetter)
</span><span class="cx">                 stubJit.setupResults(valueRegs);
</span><span class="lines">@@ -510,7 +511,8 @@
</span><span class="cx">             slowPathCall = stubJit.nearCall();
</span><span class="cx">             
</span><span class="cx">             stubJit.addPtr(
</span><del>-                MacroAssembler::TrustedImm32(alignedNumberOfBytesForCall),
</del><ins>+                MacroAssembler::TrustedImm32(codeBlock-&gt;stackPointerOffset() * sizeof(Register)),
+                GPRInfo::callFrameRegister,
</ins><span class="cx">                 MacroAssembler::stackPointerRegister);
</span><span class="cx">             if (kind == CallGetter)
</span><span class="cx">                 stubJit.setupResults(valueRegs);
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoretestsstresstailcallininlinecachejs"></a>
<div class="addfile"><h4>Added: trunk/Source/JavaScriptCore/tests/stress/tail-call-in-inline-cache.js (0 => 189325)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/tests/stress/tail-call-in-inline-cache.js                                (rev 0)
+++ trunk/Source/JavaScriptCore/tests/stress/tail-call-in-inline-cache.js        2015-09-04 00:25:19 UTC (rev 189325)
</span><span class="lines">@@ -0,0 +1,10 @@
</span><ins>+&quot;use strict&quot;;
+
+function tail() { }
+
+var obj = {
+    get x() { return tail(0); }
+};
+
+for (var i = 0; i &lt; 10; ++i)
+    obj.x;
</ins></span></pre>
</div>
</div>

</body>
</html>