Title: [189582] trunk/Source/_javascript_Core
Revision
189582
Author
[email protected]
Date
2015-09-10 12:01:47 -0700 (Thu, 10 Sep 2015)

Log Message

Convert arguments to WebAssembly functions to the declared types
https://bugs.webkit.org/show_bug.cgi?id=149033

Patch by Sukolsak Sakshuwong <[email protected]> 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):

Modified Paths

Added Paths

Diff

Modified: trunk/Source/_javascript_Core/ChangeLog (189581 => 189582)


--- trunk/Source/_javascript_Core/ChangeLog	2015-09-10 18:57:01 UTC (rev 189581)
+++ trunk/Source/_javascript_Core/ChangeLog	2015-09-10 19:01:47 UTC (rev 189582)
@@ -1,3 +1,32 @@
+2015-09-10  Sukolsak Sakshuwong  <[email protected]>
+
+        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):
+
 2015-09-10  Yusuke Suzuki  <[email protected]>
 
         JSInternalPromiseDeferred should inherit JSPromiseDeferred

Modified: trunk/Source/_javascript_Core/jit/JITOperations.h (189581 => 189582)


--- trunk/Source/_javascript_Core/jit/JITOperations.h	2015-09-10 18:57:01 UTC (rev 189581)
+++ trunk/Source/_javascript_Core/jit/JITOperations.h	2015-09-10 19:01:47 UTC (rev 189582)
@@ -173,6 +173,7 @@
 typedef int32_t JIT_OPERATION (*Z_JITOperation_EC)(ExecState*, JSCell*);
 typedef int32_t JIT_OPERATION (*Z_JITOperation_EGC)(ExecState*, JSGlobalObject*, JSCell*);
 typedef int32_t JIT_OPERATION (*Z_JITOperation_ESJss)(ExecState*, size_t, JSString*);
+typedef int32_t JIT_OPERATION (*Z_JITOperation_EJ)(ExecState*, EncodedJSValue);
 typedef int32_t JIT_OPERATION (*Z_JITOperation_EJZ)(ExecState*, EncodedJSValue, int32_t);
 typedef int32_t JIT_OPERATION (*Z_JITOperation_EJZZ)(ExecState*, EncodedJSValue, int32_t, int32_t);
 typedef size_t JIT_OPERATION (*S_JITOperation_ECC)(ExecState*, JSCell*, JSCell*);

Added: trunk/Source/_javascript_Core/tests/stress/wasm/type-conversion.wasm (0 => 189582)


--- trunk/Source/_javascript_Core/tests/stress/wasm/type-conversion.wasm	                        (rev 0)
+++ trunk/Source/_javascript_Core/tests/stress/wasm/type-conversion.wasm	2015-09-10 19:01:47 UTC (rev 189582)
@@ -0,0 +1 @@
+wasmA\x80\xC0\x80\xA0takeAndReturnInt32takeAndReturnDouble
\ No newline at end of file

Added: trunk/Source/_javascript_Core/tests/stress/wasm-type-conversion.js (0 => 189582)


--- trunk/Source/_javascript_Core/tests/stress/wasm-type-conversion.js	                        (rev 0)
+++ trunk/Source/_javascript_Core/tests/stress/wasm-type-conversion.js	2015-09-10 19:01:47 UTC (rev 189582)
@@ -0,0 +1,55 @@
+//@ skip
+
+function shouldBe(actual, expected) {
+    if (actual !== expected)
+        throw new Error('bad value: ' + actual);
+}
+
+/*
+wasm/type-conversion.wasm is generated by pack-asmjs <https://github.com/WebAssembly/polyfill-prototype-1> from the following script:
+
+function asmModule(global, env, buffer) {
+    "use asm";
+
+    function takeAndReturnInt32(x) {
+        x = x | 0;
+        return x;
+    }
+
+    function takeAndReturnDouble(x) {
+        x = +x;
+        return x;
+    }
+
+    return {
+        takeAndReturnInt32: takeAndReturnInt32,
+        takeAndReturnDouble: takeAndReturnDouble,
+    };
+}
+*/
+
+var module = loadWebAssembly("wasm/type-conversion.wasm");
+
+var two = {
+    valueOf() { return 2; }
+};
+
+shouldBe(module.takeAndReturnInt32(1), 1);
+shouldBe(module.takeAndReturnInt32("1"), 1);
+shouldBe(module.takeAndReturnInt32(1.9), 1);
+shouldBe(module.takeAndReturnInt32(true), 1);
+shouldBe(module.takeAndReturnInt32(false), 0);
+shouldBe(module.takeAndReturnInt32("one"), 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("1.5"), 1.5);
+shouldBe(module.takeAndReturnDouble(1), 1);
+shouldBe(module.takeAndReturnDouble(true), 1);
+shouldBe(module.takeAndReturnDouble(false), 0);
+shouldBe(isNaN(module.takeAndReturnDouble("one")), true);
+shouldBe(isNaN(module.takeAndReturnDouble({ })), true);
+shouldBe(module.takeAndReturnDouble([2.5]), 2.5);
+shouldBe(module.takeAndReturnDouble(two), 2);

Modified: trunk/Source/_javascript_Core/wasm/WASMFunctionCompiler.h (189581 => 189582)


--- trunk/Source/_javascript_Core/wasm/WASMFunctionCompiler.h	2015-09-10 18:57:01 UTC (rev 189581)
+++ trunk/Source/_javascript_Core/wasm/WASMFunctionCompiler.h	2015-09-10 19:01:47 UTC (rev 189582)
@@ -38,6 +38,16 @@
 
 namespace JSC {
 
+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);
+}
+
 #if !CPU(X86) && !CPU(X86_64)
 static int32_t JIT_OPERATION operationDiv(int32_t left, int32_t right)
 {
@@ -98,18 +108,22 @@
             Address address(GPRInfo::callFrameRegister, CallFrame::argumentOffset(i) * sizeof(Register));
             switch (arguments[i]) {
             case WASMType::I32:
-                load32(address, GPRInfo::regT0);
+#if USE(JSVALUE64)
+                loadValueAndConvertToInt32(address, GPRInfo::regT0);
+#else
+                loadValueAndConvertToInt32(address, GPRInfo::regT0, GPRInfo::regT1);
+#endif
                 store32(GPRInfo::regT0, localAddress(localIndex++));
                 break;
             case WASMType::F32:
-                load64(address, GPRInfo::regT0);
-                unboxDoubleWithoutAssertions(GPRInfo::regT0, FPRInfo::fpRegT0);
-                convertDoubleToFloat(FPRInfo::fpRegT0, FPRInfo::fpRegT0);
-                storeDouble(FPRInfo::fpRegT0, localAddress(localIndex++));
-                break;
             case WASMType::F64:
-                load64(address, GPRInfo::regT0);
-                unboxDoubleWithoutAssertions(GPRInfo::regT0, FPRInfo::fpRegT0);
+#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);
                 storeDouble(FPRInfo::fpRegT0, localAddress(localIndex++));
                 break;
             default:
@@ -619,18 +633,69 @@
         return nakedCall;
     }
 
+    void appendCallSetResult(const FunctionPtr& function, GPRReg result)
+    {
+        appendCall(function);
+        move(GPRInfo::returnValueGPR, result);
+    }
+
+#if CPU(X86)
+    void appendCallSetResult(const FunctionPtr& function, FPRReg result)
+    {
+        appendCall(function);
+        m_assembler.fstpl(0, stackPointerRegister);
+        loadDouble(stackPointerRegister, result);
+    }
+#elif CPU(ARM) && !CPU(ARM_HARDFP)
+    void appendCallSetResult(const FunctionPtr& function, FPRReg result)
+    {
+        appendCall(function);
+        m_assembler.vmov(result, GPRInfo::returnValueGPR, GPRInfo::returnValueGPR2);
+    }
+#else // CPU(X86_64) || (CPU(ARM) && CPU(ARM_HARDFP)) || CPU(ARM64) || CPU(MIPS) || CPU(SH4)
+    void appendCallSetResult(const FunctionPtr& 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
+
     void callOperation(int32_t JIT_OPERATION (*operation)(int32_t, int32_t), GPRReg src1, GPRReg src2, GPRReg dst)
     {
         setupArguments(src1, src2);
-        appendCall(operation);
-        move(GPRInfo::returnValueGPR, dst);
+        appendCallSetResult(operation, dst);
     }
 
     void callOperation(uint32_t JIT_OPERATION (*operation)(uint32_t, uint32_t), GPRReg src1, GPRReg src2, GPRReg dst)
     {
         setupArguments(src1, src2);
-        appendCall(operation);
-        move(GPRInfo::returnValueGPR, dst);
+        appendCallSetResult(operation, dst);
     }
 
     void boxArgumentsAndAdjustStackPointer(const Vector<WASMType>& arguments)
@@ -703,6 +768,70 @@
         }
     }
 
+#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
+
     JSWASMModule* m_module;
     unsigned m_stackHeight;
     unsigned m_numberOfLocals;
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to