<!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>[214602] 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/214602">214602</a></dd>
<dt>Author</dt> <dd>sbarati@apple.com</dd>
<dt>Date</dt> <dd>2017-03-30 00:29:02 -0700 (Thu, 30 Mar 2017)</dd>
</dl>

<h3>Log Message</h3>
<pre>WebAssembly: pass Wasm::Context* to vmEntryToWasm when not using fast TLS
https://bugs.webkit.org/show_bug.cgi?id=170182

Reviewed by Mark Lam.

This is one more step in the direction of PIC-ified Wasm.
I'm removing assumptions that a wasm callee is a cell. We used to use
the callee to get the WasmContext off the callee's VM. Instead,
this patch makes it so that we pass in the context as a parameter
to the JS entrypoint.

* heap/MarkedBlock.h:
(JSC::MarkedBlock::offsetOfVM): Deleted.
* jit/AssemblyHelpers.cpp:
(JSC::AssemblyHelpers::loadWasmContext):
(JSC::AssemblyHelpers::storeWasmContext):
(JSC::AssemblyHelpers::loadWasmContextNeedsMacroScratchRegister):
(JSC::AssemblyHelpers::storeWasmContextNeedsMacroScratchRegister):
* jsc.cpp:
(functionTestWasmModuleFunctions):
* runtime/VM.h:
(JSC::VM::wasmContextOffset): Deleted.
* wasm/WasmB3IRGenerator.cpp:
(JSC::Wasm::B3IRGenerator::materializeWasmContext):
(JSC::Wasm::B3IRGenerator::restoreWasmContext):
(JSC::Wasm::B3IRGenerator::B3IRGenerator):
(JSC::Wasm::createJSToWasmWrapper):
* wasm/WasmContext.cpp:
(JSC::Wasm::loadContext):
(JSC::Wasm::storeContext):
(JSC::loadWasmContext): Deleted.
(JSC::storeWasmContext): Deleted.
* wasm/WasmContext.h:
(JSC::Wasm::useFastTLS):
(JSC::Wasm::useFastTLSForContext):
* wasm/WasmMemoryInformation.cpp:
(JSC::Wasm::PinnedRegisterInfo::get):
* wasm/WasmMemoryInformation.h:
(JSC::Wasm::useFastTLS): Deleted.
(JSC::Wasm::useFastTLSForWasmContext): Deleted.
* wasm/js/WebAssemblyFunction.cpp:
(JSC::callWebAssemblyFunction):</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceJavaScriptCoreChangeLog">trunk/Source/JavaScriptCore/ChangeLog</a></li>
<li><a href="#trunkSourceJavaScriptCoreheapMarkedBlockh">trunk/Source/JavaScriptCore/heap/MarkedBlock.h</a></li>
<li><a href="#trunkSourceJavaScriptCorejitAssemblyHelperscpp">trunk/Source/JavaScriptCore/jit/AssemblyHelpers.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCorejsccpp">trunk/Source/JavaScriptCore/jsc.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCoreruntimeVMh">trunk/Source/JavaScriptCore/runtime/VM.h</a></li>
<li><a href="#trunkSourceJavaScriptCorewasmWasmB3IRGeneratorcpp">trunk/Source/JavaScriptCore/wasm/WasmB3IRGenerator.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCorewasmWasmContextcpp">trunk/Source/JavaScriptCore/wasm/WasmContext.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCorewasmWasmContexth">trunk/Source/JavaScriptCore/wasm/WasmContext.h</a></li>
<li><a href="#trunkSourceJavaScriptCorewasmWasmMemoryInformationcpp">trunk/Source/JavaScriptCore/wasm/WasmMemoryInformation.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCorewasmWasmMemoryInformationh">trunk/Source/JavaScriptCore/wasm/WasmMemoryInformation.h</a></li>
<li><a href="#trunkSourceJavaScriptCorewasmjsWebAssemblyFunctioncpp">trunk/Source/JavaScriptCore/wasm/js/WebAssemblyFunction.cpp</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceJavaScriptCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/ChangeLog (214601 => 214602)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/ChangeLog        2017-03-30 07:16:27 UTC (rev 214601)
+++ trunk/Source/JavaScriptCore/ChangeLog        2017-03-30 07:29:02 UTC (rev 214602)
</span><span class="lines">@@ -1,3 +1,48 @@
</span><ins>+2017-03-30  Saam Barati  &lt;sbarati@apple.com&gt;
+
+        WebAssembly: pass Wasm::Context* to vmEntryToWasm when not using fast TLS
+        https://bugs.webkit.org/show_bug.cgi?id=170182
+
+        Reviewed by Mark Lam.
+
+        This is one more step in the direction of PIC-ified Wasm.
+        I'm removing assumptions that a wasm callee is a cell. We used to use
+        the callee to get the WasmContext off the callee's VM. Instead,
+        this patch makes it so that we pass in the context as a parameter
+        to the JS entrypoint.
+
+        * heap/MarkedBlock.h:
+        (JSC::MarkedBlock::offsetOfVM): Deleted.
+        * jit/AssemblyHelpers.cpp:
+        (JSC::AssemblyHelpers::loadWasmContext):
+        (JSC::AssemblyHelpers::storeWasmContext):
+        (JSC::AssemblyHelpers::loadWasmContextNeedsMacroScratchRegister):
+        (JSC::AssemblyHelpers::storeWasmContextNeedsMacroScratchRegister):
+        * jsc.cpp:
+        (functionTestWasmModuleFunctions):
+        * runtime/VM.h:
+        (JSC::VM::wasmContextOffset): Deleted.
+        * wasm/WasmB3IRGenerator.cpp:
+        (JSC::Wasm::B3IRGenerator::materializeWasmContext):
+        (JSC::Wasm::B3IRGenerator::restoreWasmContext):
+        (JSC::Wasm::B3IRGenerator::B3IRGenerator):
+        (JSC::Wasm::createJSToWasmWrapper):
+        * wasm/WasmContext.cpp:
+        (JSC::Wasm::loadContext):
+        (JSC::Wasm::storeContext):
+        (JSC::loadWasmContext): Deleted.
+        (JSC::storeWasmContext): Deleted.
+        * wasm/WasmContext.h:
+        (JSC::Wasm::useFastTLS):
+        (JSC::Wasm::useFastTLSForContext):
+        * wasm/WasmMemoryInformation.cpp:
+        (JSC::Wasm::PinnedRegisterInfo::get):
+        * wasm/WasmMemoryInformation.h:
+        (JSC::Wasm::useFastTLS): Deleted.
+        (JSC::Wasm::useFastTLSForWasmContext): Deleted.
+        * wasm/js/WebAssemblyFunction.cpp:
+        (JSC::callWebAssemblyFunction):
+
</ins><span class="cx"> 2017-03-30  JF Bastien  &lt;jfbastien@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         WebAssembly: fix misc JS API implementation inconsistencies
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreheapMarkedBlockh"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/heap/MarkedBlock.h (214601 => 214602)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/heap/MarkedBlock.h        2017-03-30 07:16:27 UTC (rev 214601)
+++ trunk/Source/JavaScriptCore/heap/MarkedBlock.h        2017-03-30 07:29:02 UTC (rev 214602)
</span><span class="lines">@@ -299,11 +299,6 @@
</span><span class="cx">     bool isMarkedRaw(const void* p);
</span><span class="cx">     HeapVersion markingVersion() const { return m_markingVersion; }
</span><span class="cx"> 
</span><del>-    static ptrdiff_t offsetOfVM()
-    {
-        return OBJECT_OFFSETOF(MarkedBlock, m_vm);
-    }
-    
</del><span class="cx"> private:
</span><span class="cx">     static const size_t atomAlignmentMask = atomSize - 1;
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceJavaScriptCorejitAssemblyHelperscpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/jit/AssemblyHelpers.cpp (214601 => 214602)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/jit/AssemblyHelpers.cpp        2017-03-30 07:16:27 UTC (rev 214601)
+++ trunk/Source/JavaScriptCore/jit/AssemblyHelpers.cpp        2017-03-30 07:29:02 UTC (rev 214602)
</span><span class="lines">@@ -33,6 +33,7 @@
</span><span class="cx"> #include &quot;LinkBuffer.h&quot;
</span><span class="cx"> 
</span><span class="cx"> #if ENABLE(WEBASSEMBLY)
</span><ins>+#include &quot;WasmContext.h&quot;
</ins><span class="cx"> #include &quot;WasmMemoryInformation.h&quot;
</span><span class="cx"> #endif
</span><span class="cx"> 
</span><span class="lines">@@ -777,7 +778,7 @@
</span><span class="cx"> void AssemblyHelpers::loadWasmContext(GPRReg dst)
</span><span class="cx"> {
</span><span class="cx"> #if ENABLE(FAST_TLS_JIT)
</span><del>-    if (Wasm::useFastTLSForWasmContext()) {
</del><ins>+    if (Wasm::useFastTLSForContext()) {
</ins><span class="cx">         loadFromTLSPtr(fastTLSOffsetForKey(WTF_WASM_CONTEXT_KEY), dst);
</span><span class="cx">         return;
</span><span class="cx">     }
</span><span class="lines">@@ -788,7 +789,7 @@
</span><span class="cx"> void AssemblyHelpers::storeWasmContext(GPRReg src)
</span><span class="cx"> {
</span><span class="cx"> #if ENABLE(FAST_TLS_JIT)
</span><del>-    if (Wasm::useFastTLSForWasmContext()) {
</del><ins>+    if (Wasm::useFastTLSForContext()) {
</ins><span class="cx">         storeToTLSPtr(src, fastTLSOffsetForKey(WTF_WASM_CONTEXT_KEY));
</span><span class="cx">         return;
</span><span class="cx">     }
</span><span class="lines">@@ -799,7 +800,7 @@
</span><span class="cx"> bool AssemblyHelpers::loadWasmContextNeedsMacroScratchRegister()
</span><span class="cx"> {
</span><span class="cx"> #if ENABLE(FAST_TLS_JIT)
</span><del>-    if (Wasm::useFastTLSForWasmContext())
</del><ins>+    if (Wasm::useFastTLSForContext())
</ins><span class="cx">         return loadFromTLSPtrNeedsMacroScratchRegister();
</span><span class="cx"> #endif
</span><span class="cx">     return false;
</span><span class="lines">@@ -808,7 +809,7 @@
</span><span class="cx"> bool AssemblyHelpers::storeWasmContextNeedsMacroScratchRegister()
</span><span class="cx"> {
</span><span class="cx"> #if ENABLE(FAST_TLS_JIT)
</span><del>-    if (Wasm::useFastTLSForWasmContext())
</del><ins>+    if (Wasm::useFastTLSForContext())
</ins><span class="cx">         return storeToTLSPtrNeedsMacroScratchRegister();
</span><span class="cx"> #endif
</span><span class="cx">     return false;
</span></span></pre></div>
<a id="trunkSourceJavaScriptCorejsccpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/jsc.cpp (214601 => 214602)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/jsc.cpp        2017-03-30 07:16:27 UTC (rev 214601)
+++ trunk/Source/JavaScriptCore/jsc.cpp        2017-03-30 07:29:02 UTC (rev 214602)
</span><span class="lines">@@ -75,6 +75,7 @@
</span><span class="cx"> #include &quot;SuperSampler.h&quot;
</span><span class="cx"> #include &quot;TestRunnerUtils.h&quot;
</span><span class="cx"> #include &quot;TypeProfilerLog.h&quot;
</span><ins>+#include &quot;WasmContext.h&quot;
</ins><span class="cx"> #include &quot;WasmFaultSignalHandler.h&quot;
</span><span class="cx"> #include &quot;WasmMemory.h&quot;
</span><span class="cx"> #include &quot;WasmPlanInlines.h&quot;
</span><span class="lines">@@ -3224,6 +3225,13 @@
</span><span class="cx">             JSArray* arguments = jsCast&lt;JSArray*&gt;(test-&gt;getIndexQuickly(1));
</span><span class="cx"> 
</span><span class="cx">             MarkedArgumentBuffer boxedArgs;
</span><ins>+            if (!Wasm::useFastTLSForContext()) {
+                // When not using fast TLS, the code we emit expects Wasm::Context*
+                // as the first argument. These tests that this API supports don't ever
+                // use a Context. So this is just a hack to get it to not barf.
+                // We really need to remove this API.
+                boxedArgs.append(jsNumber(0xbadbeef));
+            }
</ins><span class="cx">             for (unsigned argIndex = 0; argIndex &lt; arguments-&gt;length(); ++argIndex)
</span><span class="cx">                 boxedArgs.append(box(exec, vm, arguments-&gt;getIndexQuickly(argIndex)));
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreruntimeVMh"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/runtime/VM.h (214601 => 214602)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/runtime/VM.h        2017-03-30 07:16:27 UTC (rev 214601)
+++ trunk/Source/JavaScriptCore/runtime/VM.h        2017-03-30 07:29:02 UTC (rev 214602)
</span><span class="lines">@@ -483,11 +483,6 @@
</span><span class="cx">         return OBJECT_OFFSETOF(VM, targetMachinePCForThrow);
</span><span class="cx">     }
</span><span class="cx"> 
</span><del>-    static ptrdiff_t wasmContextOffset()
-    {
-        return OBJECT_OFFSETOF(VM, wasmContext);
-    }
-
</del><span class="cx">     void restorePreviousException(Exception* exception) { setException(exception); }
</span><span class="cx"> 
</span><span class="cx">     void clearLastException() { m_lastException = nullptr; }
</span></span></pre></div>
<a id="trunkSourceJavaScriptCorewasmWasmB3IRGeneratorcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/wasm/WasmB3IRGenerator.cpp (214601 => 214602)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/wasm/WasmB3IRGenerator.cpp        2017-03-30 07:16:27 UTC (rev 214601)
+++ trunk/Source/JavaScriptCore/wasm/WasmB3IRGenerator.cpp        2017-03-30 07:29:02 UTC (rev 214602)
</span><span class="lines">@@ -52,6 +52,7 @@
</span><span class="cx"> #include &quot;JSWebAssemblyRuntimeError.h&quot;
</span><span class="cx"> #include &quot;VirtualRegister.h&quot;
</span><span class="cx"> #include &quot;WasmCallingConvention.h&quot;
</span><ins>+#include &quot;WasmContext.h&quot;
</ins><span class="cx"> #include &quot;WasmExceptionType.h&quot;
</span><span class="cx"> #include &quot;WasmFunctionParser.h&quot;
</span><span class="cx"> #include &quot;WasmMemory.h&quot;
</span><span class="lines">@@ -249,7 +250,7 @@
</span><span class="cx"> 
</span><span class="cx"> Value* B3IRGenerator::materializeWasmContext(Procedure&amp; proc, BasicBlock* block)
</span><span class="cx"> {
</span><del>-    if (useFastTLSForWasmContext()) {
</del><ins>+    if (useFastTLSForContext()) {
</ins><span class="cx">         PatchpointValue* patchpoint = block-&gt;appendNew&lt;PatchpointValue&gt;(proc, pointerType(), Origin());
</span><span class="cx">         if (CCallHelpers::loadWasmContextNeedsMacroScratchRegister())
</span><span class="cx">             patchpoint-&gt;clobber(RegisterSet::macroScratchRegisters());
</span><span class="lines">@@ -273,7 +274,7 @@
</span><span class="cx"> 
</span><span class="cx"> void B3IRGenerator::restoreWasmContext(Procedure&amp; proc, BasicBlock* block, Value* arg)
</span><span class="cx"> {
</span><del>-    if (useFastTLSForWasmContext()) {
</del><ins>+    if (useFastTLSForContext()) {
</ins><span class="cx">         PatchpointValue* patchpoint = block-&gt;appendNew&lt;PatchpointValue&gt;(proc, B3::Void, Origin());
</span><span class="cx">         if (CCallHelpers::storeWasmContextNeedsMacroScratchRegister())
</span><span class="cx">             patchpoint-&gt;clobber(RegisterSet::macroScratchRegisters());
</span><span class="lines">@@ -316,7 +317,7 @@
</span><span class="cx">     m_memoryBaseGPR = pinnedRegs.baseMemoryPointer;
</span><span class="cx">     m_wasmContextGPR = pinnedRegs.wasmContextPointer;
</span><span class="cx">     m_proc.pinRegister(m_memoryBaseGPR);
</span><del>-    if (!useFastTLSForWasmContext())
</del><ins>+    if (!useFastTLSForContext())
</ins><span class="cx">         m_proc.pinRegister(m_wasmContextGPR);
</span><span class="cx">     ASSERT(!pinnedRegs.sizeRegisters[0].sizeOffset);
</span><span class="cx">     m_memorySizeGPR = pinnedRegs.sizeRegisters[0].sizeRegister;
</span><span class="lines">@@ -1147,6 +1148,8 @@
</span><span class="cx">         jit.storePtr(reg, CCallHelpers::Address(GPRInfo::callFrameRegister, offset));
</span><span class="cx">     }
</span><span class="cx"> 
</span><ins>+    GPRReg wasmContextGPR = pinnedRegs.wasmContextPointer;
+
</ins><span class="cx">     {
</span><span class="cx">         CCallHelpers::Address calleeFrame = CCallHelpers::Address(MacroAssembler::stackPointerRegister, -static_cast&lt;ptrdiff_t&gt;(sizeof(CallerFrameAndPC)));
</span><span class="cx">         numGPRs = 0;
</span><span class="lines">@@ -1155,7 +1158,15 @@
</span><span class="cx">         // we can use this as a scratch for now since we saved it above.
</span><span class="cx">         GPRReg scratchReg = pinnedRegs.baseMemoryPointer;
</span><span class="cx"> 
</span><del>-        ptrdiff_t jsOffset = CallFrameSlot::thisArgument * sizeof(void*);
</del><ins>+        ptrdiff_t jsOffset = CallFrameSlot::thisArgument * sizeof(EncodedJSValue);
+
+        // vmEntryToWasm passes Wasm::Context* as the first JS argument when we're
+        // not using fast TLS to hold the Wasm::Context*.
+        if (!useFastTLSForContext()) {
+            jit.loadPtr(CCallHelpers::Address(GPRInfo::callFrameRegister, jsOffset), wasmContextGPR);
+            jsOffset += sizeof(EncodedJSValue);
+        }
+
</ins><span class="cx">         ptrdiff_t wasmOffset = CallFrame::headerSizeInRegisters * sizeof(void*);
</span><span class="cx">         for (unsigned i = 0; i &lt; signature-&gt;argumentCount(); i++) {
</span><span class="cx">             switch (signature-&gt;argument(i)) {
</span><span class="lines">@@ -1201,25 +1212,15 @@
</span><span class="cx">                 RELEASE_ASSERT_NOT_REACHED();
</span><span class="cx">             }
</span><span class="cx"> 
</span><del>-            jsOffset += sizeof(void*);
</del><ins>+            jsOffset += sizeof(EncodedJSValue);
</ins><span class="cx">         }
</span><span class="cx">     }
</span><span class="cx"> 
</span><del>-    // FIXME: JStoWasm wrapper should take JSWebAssemblyInstance pointer as an argument directly.
-    // https://bugs.webkit.org/show_bug.cgi?id=170182
-    GPRReg wasmContext = pinnedRegs.wasmContextPointer;
-    if (!useFastTLSForWasmContext()) {
-        jit.loadPtr(CCallHelpers::Address(GPRInfo::callFrameRegister, CallFrameSlot::callee * static_cast&lt;int&gt;(sizeof(Register))), wasmContext);
-        jit.andPtr(CCallHelpers::TrustedImmPtr(MarkedBlock::blockMask), wasmContext);
-        jit.loadPtr(CCallHelpers::Address(wasmContext, MarkedBlock::offsetOfVM()), wasmContext);
-        jit.loadPtr(CCallHelpers::Address(wasmContext, VM::wasmContextOffset()), wasmContext);
-    }
-
</del><span class="cx">     if (!!info.memory) {
</span><span class="cx">         GPRReg baseMemory = pinnedRegs.baseMemoryPointer;
</span><span class="cx"> 
</span><del>-        if (!useFastTLSForWasmContext())
-            jit.loadPtr(CCallHelpers::Address(wasmContext, JSWebAssemblyInstance::offsetOfMemory()), baseMemory);
</del><ins>+        if (!useFastTLSForContext())
+            jit.loadPtr(CCallHelpers::Address(wasmContextGPR, JSWebAssemblyInstance::offsetOfMemory()), baseMemory);
</ins><span class="cx">         else {
</span><span class="cx">             jit.loadWasmContext(baseMemory);
</span><span class="cx">             jit.loadPtr(CCallHelpers::Address(baseMemory, JSWebAssemblyInstance::offsetOfMemory()), baseMemory);
</span></span></pre></div>
<a id="trunkSourceJavaScriptCorewasmWasmContextcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/wasm/WasmContext.cpp (214601 => 214602)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/wasm/WasmContext.cpp        2017-03-30 07:16:27 UTC (rev 214601)
+++ trunk/Source/JavaScriptCore/wasm/WasmContext.cpp        2017-03-30 07:29:02 UTC (rev 214602)
</span><span class="lines">@@ -32,28 +32,28 @@
</span><span class="cx"> #include &lt;mutex&gt;
</span><span class="cx"> #include &lt;wtf/FastTLS.h&gt;
</span><span class="cx"> 
</span><del>-namespace JSC {
</del><ins>+namespace JSC { namespace Wasm {
</ins><span class="cx"> 
</span><del>-WasmContext* loadWasmContext(VM&amp; vm)
</del><ins>+Context* loadContext(VM&amp; vm)
</ins><span class="cx"> {
</span><span class="cx"> #if ENABLE(FAST_TLS_JIT)
</span><del>-    if (Options::useWebAssemblyFastTLS())
-        return bitwise_cast&lt;WasmContext*&gt;(_pthread_getspecific_direct(WTF_WASM_CONTEXT_KEY));
</del><ins>+    if (useFastTLSForContext())
+        return bitwise_cast&lt;Context*&gt;(_pthread_getspecific_direct(WTF_WASM_CONTEXT_KEY));
</ins><span class="cx"> #endif
</span><span class="cx">     // FIXME: Save this state elsewhere to allow PIC. https://bugs.webkit.org/show_bug.cgi?id=169773
</span><span class="cx">     return vm.wasmContext;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-void storeWasmContext(VM&amp; vm, WasmContext* instance)
</del><ins>+void storeContext(VM&amp; vm, Context* context)
</ins><span class="cx"> {
</span><span class="cx"> #if ENABLE(FAST_TLS_JIT)
</span><del>-    if (Options::useWebAssemblyFastTLS())
-        _pthread_setspecific_direct(WTF_WASM_CONTEXT_KEY, bitwise_cast&lt;void*&gt;(instance));
</del><ins>+    if (useFastTLSForContext())
+        _pthread_setspecific_direct(WTF_WASM_CONTEXT_KEY, bitwise_cast&lt;void*&gt;(context));
</ins><span class="cx"> #endif
</span><span class="cx">     // FIXME: Save this state elsewhere to allow PIC. https://bugs.webkit.org/show_bug.cgi?id=169773
</span><del>-    vm.wasmContext = instance;
</del><ins>+    vm.wasmContext = context;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><del>-} // namespace JSC
</del><ins>+} } // namespace JSC::Wasm
</ins><span class="cx"> 
</span><span class="cx"> #endif // ENABLE(WEBASSEMBLY)
</span></span></pre></div>
<a id="trunkSourceJavaScriptCorewasmWasmContexth"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/wasm/WasmContext.h (214601 => 214602)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/wasm/WasmContext.h        2017-03-30 07:16:27 UTC (rev 214601)
+++ trunk/Source/JavaScriptCore/wasm/WasmContext.h        2017-03-30 07:29:02 UTC (rev 214602)
</span><span class="lines">@@ -27,16 +27,36 @@
</span><span class="cx"> 
</span><span class="cx"> #if ENABLE(WEBASSEMBLY)
</span><span class="cx"> 
</span><ins>+#include &quot;Options.h&quot;
+
</ins><span class="cx"> namespace JSC {
</span><span class="cx"> 
</span><span class="cx"> class JSWebAssemblyInstance;
</span><span class="cx"> class VM;
</span><span class="cx"> 
</span><del>-using WasmContext = JSWebAssemblyInstance;
</del><ins>+namespace Wasm {
</ins><span class="cx"> 
</span><del>-WasmContext* loadWasmContext(VM&amp;);
-void storeWasmContext(VM&amp;, WasmContext*);
</del><ins>+// FIXME: We might want this to be something else at some point:
+// https://bugs.webkit.org/show_bug.cgi?id=170260
+using Context = JSWebAssemblyInstance;
</ins><span class="cx"> 
</span><del>-} // namespace JSC
</del><ins>+inline bool useFastTLS()
+{
+#if ENABLE(FAST_TLS_JIT)
+    return Options::useWebAssemblyFastTLS();
+#else
+    return false;
+#endif
+}
</ins><span class="cx"> 
</span><ins>+inline bool useFastTLSForContext()
+{
+    return useFastTLS();
+}
+
+Context* loadContext(VM&amp;);
+void storeContext(VM&amp;, Context*);
+
+} } // namespace JSC::Wasm
+
</ins><span class="cx"> #endif // ENABLE(WEBASSEMBLY)
</span></span></pre></div>
<a id="trunkSourceJavaScriptCorewasmWasmMemoryInformationcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/wasm/WasmMemoryInformation.cpp (214601 => 214602)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/wasm/WasmMemoryInformation.cpp        2017-03-30 07:16:27 UTC (rev 214601)
+++ trunk/Source/JavaScriptCore/wasm/WasmMemoryInformation.cpp        2017-03-30 07:29:02 UTC (rev 214602)
</span><span class="lines">@@ -29,6 +29,7 @@
</span><span class="cx"> #if ENABLE(WEBASSEMBLY)
</span><span class="cx"> 
</span><span class="cx"> #include &quot;WasmCallingConvention.h&quot;
</span><ins>+#include &quot;WasmContext.h&quot;
</ins><span class="cx"> #include &quot;WasmMemory.h&quot;
</span><span class="cx"> #include &lt;wtf/NeverDestroyed.h&gt;
</span><span class="cx"> 
</span><span class="lines">@@ -64,12 +65,12 @@
</span><span class="cx">         //        see: https://bugs.webkit.org/show_bug.cgi?id=162952
</span><span class="cx">         Vector&lt;unsigned&gt; pinnedSizes = { 0 };
</span><span class="cx">         unsigned numberOfPinnedRegisters = pinnedSizes.size() + 1;
</span><del>-        if (!useFastTLSForWasmContext())
</del><ins>+        if (!useFastTLSForContext())
</ins><span class="cx">             ++numberOfPinnedRegisters;
</span><span class="cx">         Vector&lt;GPRReg&gt; pinnedRegs = getPinnedRegisters(numberOfPinnedRegisters);
</span><span class="cx"> 
</span><span class="cx">         baseMemoryPointer = pinnedRegs.takeLast();
</span><del>-        if (!useFastTLSForWasmContext())
</del><ins>+        if (!useFastTLSForContext())
</ins><span class="cx">             wasmContextPointer = pinnedRegs.takeLast();
</span><span class="cx"> 
</span><span class="cx">         ASSERT(pinnedSizes.size() == pinnedRegs.size());
</span></span></pre></div>
<a id="trunkSourceJavaScriptCorewasmWasmMemoryInformationh"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/wasm/WasmMemoryInformation.h (214601 => 214602)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/wasm/WasmMemoryInformation.h        2017-03-30 07:16:27 UTC (rev 214601)
+++ trunk/Source/JavaScriptCore/wasm/WasmMemoryInformation.h        2017-03-30 07:29:02 UTC (rev 214602)
</span><span class="lines">@@ -81,20 +81,6 @@
</span><span class="cx">     bool m_isImport { false };
</span><span class="cx"> };
</span><span class="cx"> 
</span><del>-inline bool useFastTLS()
-{
-#if ENABLE(FAST_TLS_JIT)
-    return Options::useWebAssemblyFastTLS();
-#else
-    return false;
-#endif
-}
-
-inline bool useFastTLSForWasmContext()
-{
-    return useFastTLS();
-}
-
</del><span class="cx"> } } // namespace JSC::Wasm
</span><span class="cx"> 
</span><span class="cx"> #endif // ENABLE(WASM)
</span></span></pre></div>
<a id="trunkSourceJavaScriptCorewasmjsWebAssemblyFunctioncpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/wasm/js/WebAssemblyFunction.cpp (214601 => 214602)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/wasm/js/WebAssemblyFunction.cpp        2017-03-30 07:16:27 UTC (rev 214601)
+++ trunk/Source/JavaScriptCore/wasm/js/WebAssemblyFunction.cpp        2017-03-30 07:29:02 UTC (rev 214602)
</span><span class="lines">@@ -82,6 +82,12 @@
</span><span class="cx">     TraceScope traceScope(WebAssemblyExecuteStart, WebAssemblyExecuteEnd);
</span><span class="cx"> 
</span><span class="cx">     Vector&lt;JSValue&gt; boxedArgs;
</span><ins>+    Wasm::Context* wasmContext = wasmFunction-&gt;instance();
+    // When we don't use fast TLS to store the context, the js
+    // entry wrapper expects the WasmContext* as the first argument.
+    if (!Wasm::useFastTLSForContext())
+        boxedArgs.append(wasmContext);
+
</ins><span class="cx">     for (unsigned argIndex = 0; argIndex &lt; signature-&gt;argumentCount(); ++argIndex) {
</span><span class="cx">         JSValue arg = exec-&gt;argument(argIndex);
</span><span class="cx">         switch (signature-&gt;argument(argIndex)) {
</span><span class="lines">@@ -122,16 +128,16 @@
</span><span class="cx">     protoCallFrame.init(nullptr, wasmFunction, firstArgument, argCount, remainingArgs);
</span><span class="cx"> 
</span><span class="cx">     // FIXME Do away with this entire function, and only use the entrypoint generated by B3. https://bugs.webkit.org/show_bug.cgi?id=166486
</span><del>-    // FIXME: We would like to make loadWasmContext and storeWasmContext no-op if we use pinned registers.
-    // However, we use VM.wasmContext field to pass instance to wasm function's JS glue code.
-    // We should pass JSWebAssemblyInstance directly to vmEntryToWasm.
-    // https://bugs.webkit.org/show_bug.cgi?id=170182
-    JSWebAssemblyInstance* prevJSWebAssemblyInstance = loadWasmContext(vm);
-    storeWasmContext(vm, wasmFunction-&gt;instance());
</del><ins>+    Wasm::Context* prevWasmContext = Wasm::loadContext(vm);
+    Wasm::storeContext(vm, wasmContext);
</ins><span class="cx">     ASSERT(wasmFunction-&gt;instance());
</span><del>-    ASSERT(wasmFunction-&gt;instance() == loadWasmContext(vm));
</del><ins>+    ASSERT(wasmFunction-&gt;instance() == Wasm::loadContext(vm));
</ins><span class="cx">     EncodedJSValue rawResult = vmEntryToWasm(wasmFunction-&gt;jsEntrypoint(), &amp;vm, &amp;protoCallFrame);
</span><del>-    storeWasmContext(vm, prevJSWebAssemblyInstance);
</del><ins>+    // We need to make sure this is in a register or on the stack since it's stored in Vector&lt;JSValue&gt;.
+    // This probably isn't strictly necessary, since the WebAssemblyFunction* should keep the instance
+    // alive. But it's good hygiene.
+    wasmContext-&gt;use(); 
+    Wasm::storeContext(vm, prevWasmContext);
</ins><span class="cx">     RETURN_IF_EXCEPTION(scope, { });
</span><span class="cx"> 
</span><span class="cx">     switch (signature-&gt;returnType()) {
</span></span></pre>
</div>
</div>

</body>
</html>