<!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>[189917] 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/189917">189917</a></dd>
<dt>Author</dt> <dd>commit-queue@webkit.org</dd>
<dt>Date</dt> <dd>2015-09-17 10:38:08 -0700 (Thu, 17 Sep 2015)</dd>
</dl>

<h3>Log Message</h3>
<pre>Save and restore callee save registers in WebAssembly
https://bugs.webkit.org/show_bug.cgi?id=149247

Patch by Sukolsak Sakshuwong &lt;sukolsak@gmail.com&gt; on 2015-09-17
Reviewed by Michael Saboff.

Save callee save registers when entering WebAssembly functions
and restore them when returning.

* jit/RegisterSet.cpp:
(JSC::RegisterSet::webAssemblyCalleeSaveRegisters):
* jit/RegisterSet.h:
* wasm/WASMFunctionCompiler.h:
(JSC::WASMFunctionCompiler::startFunction):
(JSC::WASMFunctionCompiler::endFunction):
(JSC::WASMFunctionCompiler::buildReturn):
(JSC::WASMFunctionCompiler::localAddress):
(JSC::WASMFunctionCompiler::temporaryAddress):
(JSC::WASMFunctionCompiler::boxArgumentsAndAdjustStackPointer):
(JSC::WASMFunctionCompiler::callAndUnboxResult):</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceJavaScriptCoreChangeLog">trunk/Source/JavaScriptCore/ChangeLog</a></li>
<li><a href="#trunkSourceJavaScriptCorejitRegisterSetcpp">trunk/Source/JavaScriptCore/jit/RegisterSet.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCorejitRegisterSeth">trunk/Source/JavaScriptCore/jit/RegisterSet.h</a></li>
<li><a href="#trunkSourceJavaScriptCorewasmWASMFunctionCompilerh">trunk/Source/JavaScriptCore/wasm/WASMFunctionCompiler.h</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceJavaScriptCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/ChangeLog (189916 => 189917)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/ChangeLog        2015-09-17 17:03:27 UTC (rev 189916)
+++ trunk/Source/JavaScriptCore/ChangeLog        2015-09-17 17:38:08 UTC (rev 189917)
</span><span class="lines">@@ -1,3 +1,25 @@
</span><ins>+2015-09-17  Sukolsak Sakshuwong  &lt;sukolsak@gmail.com&gt;
+
+        Save and restore callee save registers in WebAssembly
+        https://bugs.webkit.org/show_bug.cgi?id=149247
+
+        Reviewed by Michael Saboff.
+
+        Save callee save registers when entering WebAssembly functions
+        and restore them when returning.
+
+        * jit/RegisterSet.cpp:
+        (JSC::RegisterSet::webAssemblyCalleeSaveRegisters):
+        * jit/RegisterSet.h:
+        * wasm/WASMFunctionCompiler.h:
+        (JSC::WASMFunctionCompiler::startFunction):
+        (JSC::WASMFunctionCompiler::endFunction):
+        (JSC::WASMFunctionCompiler::buildReturn):
+        (JSC::WASMFunctionCompiler::localAddress):
+        (JSC::WASMFunctionCompiler::temporaryAddress):
+        (JSC::WASMFunctionCompiler::boxArgumentsAndAdjustStackPointer):
+        (JSC::WASMFunctionCompiler::callAndUnboxResult):
+
</ins><span class="cx"> 2015-09-16  Sukolsak Sakshuwong  &lt;sukolsak@gmail.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Implement indirect calls in WebAssembly
</span></span></pre></div>
<a id="trunkSourceJavaScriptCorejitRegisterSetcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/jit/RegisterSet.cpp (189916 => 189917)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/jit/RegisterSet.cpp        2015-09-17 17:03:27 UTC (rev 189916)
+++ trunk/Source/JavaScriptCore/jit/RegisterSet.cpp        2015-09-17 17:38:08 UTC (rev 189917)
</span><span class="lines">@@ -274,6 +274,39 @@
</span><span class="cx">     return result;
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+#if ENABLE(WEBASSEMBLY)
+RegisterSet RegisterSet::webAssemblyCalleeSaveRegisters()
+{
+    RegisterSet result;
+#if CPU(X86)
+#elif CPU(X86_64)
+#if !OS(WINDOWS)
+    ASSERT(GPRInfo::regCS3 == GPRInfo::tagTypeNumberRegister);
+    ASSERT(GPRInfo::regCS4 == GPRInfo::tagMaskRegister);
+    result.set(GPRInfo::regCS3);
+    result.set(GPRInfo::regCS4);
+#else
+    ASSERT(GPRInfo::regCS5 == GPRInfo::tagTypeNumberRegister);
+    ASSERT(GPRInfo::regCS6 == GPRInfo::tagMaskRegister);
+    result.set(GPRInfo::regCS5);
+    result.set(GPRInfo::regCS6);
+#endif
+#elif CPU(ARM_THUMB2)
+#elif CPU(ARM_TRADITIONAL)
+#elif CPU(ARM64)
+    ASSERT(GPRInfo::regCS8 == GPRInfo::tagTypeNumberRegister);
+    ASSERT(GPRInfo::regCS9 == GPRInfo::tagMaskRegister);
+    result.set(GPRInfo::regCS8);
+    result.set(GPRInfo::regCS9);
+#elif CPU(MIPS)
+#elif CPU(SH4)
+#else
+    UNREACHABLE_FOR_PLATFORM();
+#endif
+    return result;
+}
+#endif
+
</ins><span class="cx"> RegisterSet RegisterSet::allGPRs()
</span><span class="cx"> {
</span><span class="cx">     RegisterSet result;
</span></span></pre></div>
<a id="trunkSourceJavaScriptCorejitRegisterSeth"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/jit/RegisterSet.h (189916 => 189917)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/jit/RegisterSet.h        2015-09-17 17:03:27 UTC (rev 189916)
+++ trunk/Source/JavaScriptCore/jit/RegisterSet.h        2015-09-17 17:38:08 UTC (rev 189917)
</span><span class="lines">@@ -54,6 +54,9 @@
</span><span class="cx">     static RegisterSet llintBaselineCalleeSaveRegisters(); // Registers saved and used by the LLInt.
</span><span class="cx">     static RegisterSet dfgCalleeSaveRegisters(); // Registers saved and used by the DFG JIT.
</span><span class="cx">     static RegisterSet ftlCalleeSaveRegisters(); // Registers that might be saved and used by the FTL JIT.
</span><ins>+#if ENABLE(WEBASSEMBLY)
+    static RegisterSet webAssemblyCalleeSaveRegisters(); // Registers saved and used by the WebAssembly JIT.
+#endif
</ins><span class="cx">     static RegisterSet stubUnavailableRegisters(); // The union of callee saves and special registers.
</span><span class="cx">     static RegisterSet allGPRs();
</span><span class="cx">     static RegisterSet allFPRs();
</span></span></pre></div>
<a id="trunkSourceJavaScriptCorewasmWASMFunctionCompilerh"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/wasm/WASMFunctionCompiler.h (189916 => 189917)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/wasm/WASMFunctionCompiler.h        2015-09-17 17:03:27 UTC (rev 189916)
+++ trunk/Source/JavaScriptCore/wasm/WASMFunctionCompiler.h        2015-09-17 17:38:08 UTC (rev 189917)
</span><span class="lines">@@ -91,17 +91,23 @@
</span><span class="cx"> 
</span><span class="cx">     void startFunction(const Vector&lt;WASMType&gt;&amp; arguments, uint32_t numberOfI32LocalVariables, uint32_t numberOfF32LocalVariables, uint32_t numberOfF64LocalVariables)
</span><span class="cx">     {
</span><ins>+        m_calleeSaveSpace = WTF::roundUpToMultipleOf(sizeof(StackSlot), RegisterSet::webAssemblyCalleeSaveRegisters().numberOfSetRegisters() * sizeof(void*));
+        m_codeBlock-&gt;setCalleeSaveRegisters(RegisterSet::webAssemblyCalleeSaveRegisters());
+
</ins><span class="cx">         emitFunctionPrologue();
</span><span class="cx">         emitPutImmediateToCallFrameHeader(m_codeBlock, JSStack::CodeBlock);
</span><span class="cx"> 
</span><span class="cx">         m_beginLabel = label();
</span><span class="cx"> 
</span><del>-        addPtr(TrustedImm32(-WTF::roundUpToMultipleOf(stackAlignmentRegisters(), m_stackHeight) * sizeof(StackSlot) - maxFrameExtentForSlowPathCall), GPRInfo::callFrameRegister, GPRInfo::regT1);
</del><ins>+        addPtr(TrustedImm32(-m_calleeSaveSpace - WTF::roundUpToMultipleOf(stackAlignmentRegisters(), m_stackHeight) * sizeof(StackSlot) - maxFrameExtentForSlowPathCall), GPRInfo::callFrameRegister, GPRInfo::regT1);
</ins><span class="cx">         m_stackOverflow = branchPtr(Above, AbsoluteAddress(m_vm-&gt;addressOfStackLimit()), GPRInfo::regT1);
</span><span class="cx"> 
</span><span class="cx">         move(GPRInfo::regT1, stackPointerRegister);
</span><span class="cx">         checkStackPointerAlignment();
</span><span class="cx"> 
</span><ins>+        emitSaveCalleeSaves();
+        emitMaterializeTagCheckRegisters();
+
</ins><span class="cx">         m_numberOfLocals = arguments.size() + numberOfI32LocalVariables + numberOfF32LocalVariables + numberOfF64LocalVariables;
</span><span class="cx"> 
</span><span class="cx">         unsigned localIndex = 0;
</span><span class="lines">@@ -159,6 +165,7 @@
</span><span class="cx">         JSValueRegs returnValueRegs(GPRInfo::returnValueGPR2, GPRInfo::returnValueGPR);
</span><span class="cx"> #endif
</span><span class="cx">         moveTrustedValue(jsUndefined(), returnValueRegs);
</span><ins>+        emitRestoreCalleeSaves();
</ins><span class="cx">         emitFunctionEpilogue();
</span><span class="cx">         ret();
</span><span class="cx"> 
</span><span class="lines">@@ -184,6 +191,8 @@
</span><span class="cx">         if (!m_exceptionChecks.empty()) {
</span><span class="cx">             m_exceptionChecks.link(this);
</span><span class="cx"> 
</span><ins>+            copyCalleeSavesToVMCalleeSavesBuffer();
+
</ins><span class="cx">             // lookupExceptionHandler is passed two arguments, the VM and the exec (the CallFrame*).
</span><span class="cx">             move(TrustedImmPtr(vm()), GPRInfo::argumentGPR0);
</span><span class="cx">             move(GPRInfo::callFrameRegister, GPRInfo::argumentGPR1);
</span><span class="lines">@@ -289,6 +298,7 @@
</span><span class="cx">         default:
</span><span class="cx">             ASSERT_NOT_REACHED();
</span><span class="cx">         }
</span><ins>+        emitRestoreCalleeSaves();
</ins><span class="cx">         emitFunctionEpilogue();
</span><span class="cx">         ret();
</span><span class="cx">     }
</span><span class="lines">@@ -804,13 +814,13 @@
</span><span class="cx">     Address localAddress(unsigned localIndex) const
</span><span class="cx">     {
</span><span class="cx">         ASSERT(localIndex &lt; m_numberOfLocals);
</span><del>-        return Address(GPRInfo::callFrameRegister, -(localIndex + 1) * sizeof(StackSlot));
</del><ins>+        return Address(GPRInfo::callFrameRegister, -m_calleeSaveSpace - (localIndex + 1) * sizeof(StackSlot));
</ins><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     Address temporaryAddress(unsigned temporaryIndex) const
</span><span class="cx">     {
</span><span class="cx">         ASSERT(m_numberOfLocals + temporaryIndex &lt; m_stackHeight);
</span><del>-        return Address(GPRInfo::callFrameRegister, -(m_numberOfLocals + temporaryIndex + 1) * sizeof(StackSlot));
</del><ins>+        return Address(GPRInfo::callFrameRegister, -m_calleeSaveSpace - (m_numberOfLocals + temporaryIndex + 1) * sizeof(StackSlot));
</ins><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     void appendCall(const FunctionPtr&amp; function)
</span><span class="lines">@@ -913,7 +923,7 @@
</span><span class="cx">     void boxArgumentsAndAdjustStackPointer(const Vector&lt;WASMType&gt;&amp; arguments)
</span><span class="cx">     {
</span><span class="cx">         size_t argumentCount = arguments.size();
</span><del>-        int stackOffset = -WTF::roundUpToMultipleOf(stackAlignmentRegisters(), m_numberOfLocals + m_tempStackTop + argumentCount + 1 + JSStack::CallFrameHeaderSize);
</del><ins>+        int stackOffset = -m_calleeSaveSpace - WTF::roundUpToMultipleOf(stackAlignmentRegisters(), m_numberOfLocals + m_tempStackTop + argumentCount + 1 + JSStack::CallFrameHeaderSize);
</ins><span class="cx"> 
</span><span class="cx">         storeTrustedValue(jsUndefined(), Address(GPRInfo::callFrameRegister, (stackOffset + CallFrame::thisArgumentOffset()) * sizeof(Register)));
</span><span class="cx"> 
</span><span class="lines">@@ -966,7 +976,7 @@
</span><span class="cx">         m_callCompilationInfo.last().callReturnLocation = emitNakedCall(m_vm-&gt;getCTIStub(linkCallThunkGenerator).code());
</span><span class="cx"> 
</span><span class="cx">         end.link(this);
</span><del>-        addPtr(TrustedImm32(-WTF::roundUpToMultipleOf(stackAlignmentRegisters(), m_stackHeight) * sizeof(StackSlot) - maxFrameExtentForSlowPathCall), GPRInfo::callFrameRegister, stackPointerRegister);
</del><ins>+        addPtr(TrustedImm32(-m_calleeSaveSpace - WTF::roundUpToMultipleOf(stackAlignmentRegisters(), m_stackHeight) * sizeof(StackSlot) - maxFrameExtentForSlowPathCall), GPRInfo::callFrameRegister, stackPointerRegister);
</ins><span class="cx">         checkStackPointerAlignment();
</span><span class="cx"> 
</span><span class="cx">         switch (returnType) {
</span><span class="lines">@@ -1048,6 +1058,7 @@
</span><span class="cx">     unsigned m_stackHeight;
</span><span class="cx">     unsigned m_numberOfLocals;
</span><span class="cx">     unsigned m_tempStackTop { 0 };
</span><ins>+    unsigned m_calleeSaveSpace;
</ins><span class="cx"> 
</span><span class="cx">     Vector&lt;JumpTarget&gt; m_breakTargets;
</span><span class="cx">     Vector&lt;JumpTarget&gt; m_continueTargets;
</span></span></pre>
</div>
</div>

</body>
</html>