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

<h3>Log Message</h3>
<pre>Convert arguments to WebAssembly functions to the declared types
https://bugs.webkit.org/show_bug.cgi?id=149033

Patch by Sukolsak Sakshuwong &lt;sukolsak@gmail.com&gt; on 2015-09-10
Reviewed by Geoffrey Garen.

This patch checks the types of arguments to WebAssembly functions and
converts them to the declared types. This is necessary because:
- For example, if a function expects an argument of type double and we
  pass 1.0 to it, it will get a JSValue of an integer, not a double.
- We should follow asm.js's behavior for now, because we want to be able
  to test WebAssembly apps against asm.js apps. asm.js does type
  coercion on arguments by using int|0, Math.fround(float), and +double.

* jit/JITOperations.h:
* tests/stress/wasm-type-conversion.js: Added.
(shouldBe):
(two.valueOf):
* tests/stress/wasm/type-conversion.wasm: Added.
* wasm/WASMFunctionCompiler.h:
(JSC::operationConvertJSValueToInt32):
(JSC::operationConvertJSValueToDouble):
(JSC::WASMFunctionCompiler::startFunction):
(JSC::WASMFunctionCompiler::appendCallSetResult):
(JSC::WASMFunctionCompiler::callOperation):
(JSC::WASMFunctionCompiler::loadValueAndConvertToInt32):
(JSC::WASMFunctionCompiler::loadValueAndConvertToDouble):</pre>

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

<h3>Added Paths</h3>
<ul>
<li><a href="#trunkSourceJavaScriptCoretestsstresswasmtypeconversionwasm">trunk/Source/JavaScriptCore/tests/stress/wasm/type-conversion.wasm</a></li>
<li><a href="#trunkSourceJavaScriptCoretestsstresswasmtypeconversionjs">trunk/Source/JavaScriptCore/tests/stress/wasm-type-conversion.js</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceJavaScriptCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/ChangeLog (189581 => 189582)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/ChangeLog        2015-09-10 18:57:01 UTC (rev 189581)
+++ trunk/Source/JavaScriptCore/ChangeLog        2015-09-10 19:01:47 UTC (rev 189582)
</span><span class="lines">@@ -1,3 +1,32 @@
</span><ins>+2015-09-10  Sukolsak Sakshuwong  &lt;sukolsak@gmail.com&gt;
+
+        Convert arguments to WebAssembly functions to the declared types
+        https://bugs.webkit.org/show_bug.cgi?id=149033
+
+        Reviewed by Geoffrey Garen.
+
+        This patch checks the types of arguments to WebAssembly functions and
+        converts them to the declared types. This is necessary because:
+        - For example, if a function expects an argument of type double and we
+          pass 1.0 to it, it will get a JSValue of an integer, not a double.
+        - We should follow asm.js's behavior for now, because we want to be able
+          to test WebAssembly apps against asm.js apps. asm.js does type
+          coercion on arguments by using int|0, Math.fround(float), and +double.
+
+        * jit/JITOperations.h:
+        * tests/stress/wasm-type-conversion.js: Added.
+        (shouldBe):
+        (two.valueOf):
+        * tests/stress/wasm/type-conversion.wasm: Added.
+        * wasm/WASMFunctionCompiler.h:
+        (JSC::operationConvertJSValueToInt32):
+        (JSC::operationConvertJSValueToDouble):
+        (JSC::WASMFunctionCompiler::startFunction):
+        (JSC::WASMFunctionCompiler::appendCallSetResult):
+        (JSC::WASMFunctionCompiler::callOperation):
+        (JSC::WASMFunctionCompiler::loadValueAndConvertToInt32):
+        (JSC::WASMFunctionCompiler::loadValueAndConvertToDouble):
+
</ins><span class="cx"> 2015-09-10  Yusuke Suzuki  &lt;utatane.tea@gmail.com&gt;
</span><span class="cx"> 
</span><span class="cx">         JSInternalPromiseDeferred should inherit JSPromiseDeferred
</span></span></pre></div>
<a id="trunkSourceJavaScriptCorejitJITOperationsh"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/jit/JITOperations.h (189581 => 189582)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/jit/JITOperations.h        2015-09-10 18:57:01 UTC (rev 189581)
+++ trunk/Source/JavaScriptCore/jit/JITOperations.h        2015-09-10 19:01:47 UTC (rev 189582)
</span><span class="lines">@@ -173,6 +173,7 @@
</span><span class="cx"> typedef int32_t JIT_OPERATION (*Z_JITOperation_EC)(ExecState*, JSCell*);
</span><span class="cx"> typedef int32_t JIT_OPERATION (*Z_JITOperation_EGC)(ExecState*, JSGlobalObject*, JSCell*);
</span><span class="cx"> typedef int32_t JIT_OPERATION (*Z_JITOperation_ESJss)(ExecState*, size_t, JSString*);
</span><ins>+typedef int32_t JIT_OPERATION (*Z_JITOperation_EJ)(ExecState*, EncodedJSValue);
</ins><span class="cx"> typedef int32_t JIT_OPERATION (*Z_JITOperation_EJZ)(ExecState*, EncodedJSValue, int32_t);
</span><span class="cx"> typedef int32_t JIT_OPERATION (*Z_JITOperation_EJZZ)(ExecState*, EncodedJSValue, int32_t, int32_t);
</span><span class="cx"> typedef size_t JIT_OPERATION (*S_JITOperation_ECC)(ExecState*, JSCell*, JSCell*);
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoretestsstresswasmtypeconversionwasm"></a>
<div class="addfile"><h4>Added: trunk/Source/JavaScriptCore/tests/stress/wasm/type-conversion.wasm (0 => 189582)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/tests/stress/wasm/type-conversion.wasm                                (rev 0)
+++ trunk/Source/JavaScriptCore/tests/stress/wasm/type-conversion.wasm        2015-09-10 19:01:47 UTC (rev 189582)
</span><span class="lines">@@ -0,0 +1 @@
</span><ins>+wasmA\x80\xC0\x80\xA0takeAndReturnInt32takeAndReturnDouble
</ins><span class="cx">\ No newline at end of file
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoretestsstresswasmtypeconversionjs"></a>
<div class="addfile"><h4>Added: trunk/Source/JavaScriptCore/tests/stress/wasm-type-conversion.js (0 => 189582)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/tests/stress/wasm-type-conversion.js                                (rev 0)
+++ trunk/Source/JavaScriptCore/tests/stress/wasm-type-conversion.js        2015-09-10 19:01:47 UTC (rev 189582)
</span><span class="lines">@@ -0,0 +1,55 @@
</span><ins>+//@ skip
+
+function shouldBe(actual, expected) {
+    if (actual !== expected)
+        throw new Error('bad value: ' + actual);
+}
+
+/*
+wasm/type-conversion.wasm is generated by pack-asmjs &lt;https://github.com/WebAssembly/polyfill-prototype-1&gt; from the following script:
+
+function asmModule(global, env, buffer) {
+    &quot;use asm&quot;;
+
+    function takeAndReturnInt32(x) {
+        x = x | 0;
+        return x;
+    }
+
+    function takeAndReturnDouble(x) {
+        x = +x;
+        return x;
+    }
+
+    return {
+        takeAndReturnInt32: takeAndReturnInt32,
+        takeAndReturnDouble: takeAndReturnDouble,
+    };
+}
+*/
+
+var module = loadWebAssembly(&quot;wasm/type-conversion.wasm&quot;);
+
+var two = {
+    valueOf() { return 2; }
+};
+
+shouldBe(module.takeAndReturnInt32(1), 1);
+shouldBe(module.takeAndReturnInt32(&quot;1&quot;), 1);
+shouldBe(module.takeAndReturnInt32(1.9), 1);
+shouldBe(module.takeAndReturnInt32(true), 1);
+shouldBe(module.takeAndReturnInt32(false), 0);
+shouldBe(module.takeAndReturnInt32(&quot;one&quot;), 0);
+shouldBe(module.takeAndReturnInt32({ }), 0);
+shouldBe(module.takeAndReturnInt32([2.5]), 2);
+shouldBe(module.takeAndReturnInt32(two), 2);
+
+shouldBe(module.takeAndReturnDouble(1.5), 1.5);
+shouldBe(module.takeAndReturnDouble(&quot;1.5&quot;), 1.5);
+shouldBe(module.takeAndReturnDouble(1), 1);
+shouldBe(module.takeAndReturnDouble(true), 1);
+shouldBe(module.takeAndReturnDouble(false), 0);
+shouldBe(isNaN(module.takeAndReturnDouble(&quot;one&quot;)), true);
+shouldBe(isNaN(module.takeAndReturnDouble({ })), true);
+shouldBe(module.takeAndReturnDouble([2.5]), 2.5);
+shouldBe(module.takeAndReturnDouble(two), 2);
</ins></span></pre></div>
<a id="trunkSourceJavaScriptCorewasmWASMFunctionCompilerh"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/wasm/WASMFunctionCompiler.h (189581 => 189582)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/wasm/WASMFunctionCompiler.h        2015-09-10 18:57:01 UTC (rev 189581)
+++ trunk/Source/JavaScriptCore/wasm/WASMFunctionCompiler.h        2015-09-10 19:01:47 UTC (rev 189582)
</span><span class="lines">@@ -38,6 +38,16 @@
</span><span class="cx"> 
</span><span class="cx"> namespace JSC {
</span><span class="cx"> 
</span><ins>+static int32_t JIT_OPERATION operationConvertJSValueToInt32(ExecState* exec, EncodedJSValue value)
+{
+    return JSValue::decode(value).toInt32(exec);
+}
+
+static double JIT_OPERATION operationConvertJSValueToDouble(ExecState* exec, EncodedJSValue value)
+{
+    return JSValue::decode(value).toNumber(exec);
+}
+
</ins><span class="cx"> #if !CPU(X86) &amp;&amp; !CPU(X86_64)
</span><span class="cx"> static int32_t JIT_OPERATION operationDiv(int32_t left, int32_t right)
</span><span class="cx"> {
</span><span class="lines">@@ -98,18 +108,22 @@
</span><span class="cx">             Address address(GPRInfo::callFrameRegister, CallFrame::argumentOffset(i) * sizeof(Register));
</span><span class="cx">             switch (arguments[i]) {
</span><span class="cx">             case WASMType::I32:
</span><del>-                load32(address, GPRInfo::regT0);
</del><ins>+#if USE(JSVALUE64)
+                loadValueAndConvertToInt32(address, GPRInfo::regT0);
+#else
+                loadValueAndConvertToInt32(address, GPRInfo::regT0, GPRInfo::regT1);
+#endif
</ins><span class="cx">                 store32(GPRInfo::regT0, localAddress(localIndex++));
</span><span class="cx">                 break;
</span><span class="cx">             case WASMType::F32:
</span><del>-                load64(address, GPRInfo::regT0);
-                unboxDoubleWithoutAssertions(GPRInfo::regT0, FPRInfo::fpRegT0);
-                convertDoubleToFloat(FPRInfo::fpRegT0, FPRInfo::fpRegT0);
-                storeDouble(FPRInfo::fpRegT0, localAddress(localIndex++));
-                break;
</del><span class="cx">             case WASMType::F64:
</span><del>-                load64(address, GPRInfo::regT0);
-                unboxDoubleWithoutAssertions(GPRInfo::regT0, FPRInfo::fpRegT0);
</del><ins>+#if USE(JSVALUE64)
+                loadValueAndConvertToDouble(address, FPRInfo::fpRegT0, GPRInfo::regT0, GPRInfo::regT1);
+#else
+                loadValueAndConvertToDouble(address, FPRInfo::fpRegT0, GPRInfo::regT0, GPRInfo::regT1, GPRInfo::regT2, FPRInfo::fpRegT1);
+#endif
+                if (arguments[i] == WASMType::F32)
+                    convertDoubleToFloat(FPRInfo::fpRegT0, FPRInfo::fpRegT0);
</ins><span class="cx">                 storeDouble(FPRInfo::fpRegT0, localAddress(localIndex++));
</span><span class="cx">                 break;
</span><span class="cx">             default:
</span><span class="lines">@@ -619,18 +633,69 @@
</span><span class="cx">         return nakedCall;
</span><span class="cx">     }
</span><span class="cx"> 
</span><ins>+    void appendCallSetResult(const FunctionPtr&amp; function, GPRReg result)
+    {
+        appendCall(function);
+        move(GPRInfo::returnValueGPR, result);
+    }
+
+#if CPU(X86)
+    void appendCallSetResult(const FunctionPtr&amp; function, FPRReg result)
+    {
+        appendCall(function);
+        m_assembler.fstpl(0, stackPointerRegister);
+        loadDouble(stackPointerRegister, result);
+    }
+#elif CPU(ARM) &amp;&amp; !CPU(ARM_HARDFP)
+    void appendCallSetResult(const FunctionPtr&amp; function, FPRReg result)
+    {
+        appendCall(function);
+        m_assembler.vmov(result, GPRInfo::returnValueGPR, GPRInfo::returnValueGPR2);
+    }
+#else // CPU(X86_64) || (CPU(ARM) &amp;&amp; CPU(ARM_HARDFP)) || CPU(ARM64) || CPU(MIPS) || CPU(SH4)
+    void appendCallSetResult(const FunctionPtr&amp; function, FPRReg result)
+    {
+        appendCall(function);
+        moveDouble(FPRInfo::returnValueFPR, result);
+    }
+#endif
+
+#if USE(JSVALUE64)
+    void callOperation(Z_JITOperation_EJ operation, GPRReg src, GPRReg dst)
+    {
+        setupArgumentsWithExecState(src);
+        appendCallSetResult(operation, dst);
+    }
+
+    void callOperation(D_JITOperation_EJ operation, GPRReg src, FPRReg dst)
+    {
+        setupArgumentsWithExecState(src);
+        appendCallSetResult(operation, dst);
+    }
+#else
+    void callOperation(Z_JITOperation_EJ operation, GPRReg srcTag, GPRReg srcPayload, GPRReg dst)
+    {
+        setupArgumentsWithExecState(EABI_32BIT_DUMMY_ARG srcPayload, srcTag);
+        appendCallSetResult(operation, dst);
+    }
+
+    void callOperation(D_JITOperation_EJ operation, GPRReg srcTag, GPRReg srcPayload, FPRReg dst)
+    {
+        setupArgumentsWithExecState(EABI_32BIT_DUMMY_ARG srcPayload, srcTag);
+        appendCallSetResult(operation, dst);
+    }
+#endif
+
</ins><span class="cx">     void callOperation(int32_t JIT_OPERATION (*operation)(int32_t, int32_t), GPRReg src1, GPRReg src2, GPRReg dst)
</span><span class="cx">     {
</span><span class="cx">         setupArguments(src1, src2);
</span><del>-        appendCall(operation);
-        move(GPRInfo::returnValueGPR, dst);
</del><ins>+        appendCallSetResult(operation, dst);
</ins><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     void callOperation(uint32_t JIT_OPERATION (*operation)(uint32_t, uint32_t), GPRReg src1, GPRReg src2, GPRReg dst)
</span><span class="cx">     {
</span><span class="cx">         setupArguments(src1, src2);
</span><del>-        appendCall(operation);
-        move(GPRInfo::returnValueGPR, dst);
</del><ins>+        appendCallSetResult(operation, dst);
</ins><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     void boxArgumentsAndAdjustStackPointer(const Vector&lt;WASMType&gt;&amp; arguments)
</span><span class="lines">@@ -703,6 +768,70 @@
</span><span class="cx">         }
</span><span class="cx">     }
</span><span class="cx"> 
</span><ins>+#if USE(JSVALUE64)
+    void loadValueAndConvertToInt32(Address address, GPRReg dst)
+    {
+        JSValueRegs tempRegs(dst);
+        loadValue(address, tempRegs);
+        Jump checkJSInt32 = branchIfInt32(tempRegs);
+
+        callOperation(operationConvertJSValueToInt32, dst, dst);
+
+        checkJSInt32.link(this);
+    }
+
+    void loadValueAndConvertToDouble(Address address, FPRReg dst, GPRReg scratch1, GPRReg scratch2)
+    {
+        JSValueRegs tempRegs(scratch1);
+        loadValue(address, tempRegs);
+        Jump checkJSInt32 = branchIfInt32(tempRegs);
+        Jump checkJSNumber = branchIfNumber(tempRegs, scratch2);
+        JumpList end;
+
+        callOperation(operationConvertJSValueToDouble, tempRegs.gpr(), dst);
+        end.append(jump());
+
+        checkJSInt32.link(this);
+        convertInt32ToDouble(tempRegs.gpr(), dst);
+        end.append(jump());
+
+        checkJSNumber.link(this);
+        unboxDoubleWithoutAssertions(tempRegs.gpr(), dst);
+        end.link(this);
+    }
+#else
+    void loadValueAndConvertToInt32(Address address, GPRReg dst, GPRReg scratch)
+    {
+        JSValueRegs tempRegs(scratch, dst);
+        loadValue(address, tempRegs);
+        Jump checkJSInt32 = branchIfInt32(tempRegs);
+
+        callOperation(operationConvertJSValueToInt32, tempRegs.tagGPR(), tempRegs.payloadGPR(), dst);
+
+        checkJSInt32.link(this);
+    }
+
+    void loadValueAndConvertToDouble(Address address, FPRReg dst, GPRReg scratch1, GPRReg scratch2, GPRReg scratch3, FPRReg fpScratch)
+    {
+        JSValueRegs tempRegs(scratch2, scratch1);
+        loadValue(address, tempRegs);
+        Jump checkJSInt32 = branchIfInt32(tempRegs);
+        Jump checkJSNumber = branchIfNumber(tempRegs, scratch3);
+        JumpList end;
+
+        callOperation(operationConvertJSValueToDouble, tempRegs.tagGPR(), tempRegs.payloadGPR(), dst);
+        end.append(jump());
+
+        checkJSInt32.link(this);
+        convertInt32ToDouble(tempRegs.payloadGPR(), dst);
+        end.append(jump());
+
+        checkJSNumber.link(this);
+        unboxDouble(tempRegs.tagGPR(), tempRegs.payloadGPR(), dst, fpScratch)
+        end.link(this);
+    }
+#endif
+
</ins><span class="cx">     JSWASMModule* m_module;
</span><span class="cx">     unsigned m_stackHeight;
</span><span class="cx">     unsigned m_numberOfLocals;
</span></span></pre>
</div>
</div>

</body>
</html>