Implement the division and modulo instructions in WebAssembly
authorcommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sat, 5 Sep 2015 00:14:21 +0000 (00:14 +0000)
committercommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sat, 5 Sep 2015 00:14:21 +0000 (00:14 +0000)
https://bugs.webkit.org/show_bug.cgi?id=148791

Patch by Sukolsak Sakshuwong <sukolsak@gmail.com> on 2015-09-04
Reviewed by Geoffrey Garen.

This patch implements the unsigned division, signed modulo, and unsigned
modulo instructions for 32-bit integers in WebAssembly. It also
implements the context pool index instructions, which are needed for
testing. (pack-asmjs puts numbers that are used more than once in the
constant pool.)

* assembler/X86Assembler.h:
(JSC::X86Assembler::divl_r):
* tests/stress/wasm-arithmetic.js:
* tests/stress/wasm-arithmetic.wasm:
* wasm/WASMFunctionCompiler.h:
(JSC::operationMod):
(JSC::operationUnsignedDiv):
(JSC::operationUnsignedMod):
(JSC::WASMFunctionCompiler::buildBinaryI32):
(JSC::WASMFunctionCompiler::callOperation):
* wasm/WASMFunctionParser.cpp:
(JSC::WASMFunctionParser::parseExpressionI32):
(JSC::WASMFunctionParser::parseConstantPoolIndexExpressionI32):
* wasm/WASMFunctionParser.h:

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@189405 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/JavaScriptCore/ChangeLog
Source/JavaScriptCore/assembler/X86Assembler.h
Source/JavaScriptCore/tests/stress/wasm-arithmetic.js
Source/JavaScriptCore/tests/stress/wasm-arithmetic.wasm
Source/JavaScriptCore/wasm/WASMFunctionCompiler.h
Source/JavaScriptCore/wasm/WASMFunctionParser.cpp
Source/JavaScriptCore/wasm/WASMFunctionParser.h

index a606cc9..ca225d1 100644 (file)
@@ -1,3 +1,31 @@
+2015-09-04  Sukolsak Sakshuwong  <sukolsak@gmail.com>
+
+        Implement the division and modulo instructions in WebAssembly
+        https://bugs.webkit.org/show_bug.cgi?id=148791
+
+        Reviewed by Geoffrey Garen.
+
+        This patch implements the unsigned division, signed modulo, and unsigned
+        modulo instructions for 32-bit integers in WebAssembly. It also
+        implements the context pool index instructions, which are needed for
+        testing. (pack-asmjs puts numbers that are used more than once in the
+        constant pool.)
+
+        * assembler/X86Assembler.h:
+        (JSC::X86Assembler::divl_r):
+        * tests/stress/wasm-arithmetic.js:
+        * tests/stress/wasm-arithmetic.wasm:
+        * wasm/WASMFunctionCompiler.h:
+        (JSC::operationMod):
+        (JSC::operationUnsignedDiv):
+        (JSC::operationUnsignedMod):
+        (JSC::WASMFunctionCompiler::buildBinaryI32):
+        (JSC::WASMFunctionCompiler::callOperation):
+        * wasm/WASMFunctionParser.cpp:
+        (JSC::WASMFunctionParser::parseExpressionI32):
+        (JSC::WASMFunctionParser::parseConstantPoolIndexExpressionI32):
+        * wasm/WASMFunctionParser.h:
+
 2015-09-04  Basile Clement  <basile_clement@apple.com>
 
         Fix debug output for an eval call
index c4eb5be..196c675 100644 (file)
@@ -311,6 +311,7 @@ private:
         GROUP3_OP_TEST = 0,
         GROUP3_OP_NOT  = 2,
         GROUP3_OP_NEG  = 3,
+        GROUP3_OP_DIV = 6,
         GROUP3_OP_IDIV = 7,
 
         GROUP5_OP_CALLN = 2,
@@ -932,6 +933,11 @@ public:
         m_formatter.immediate32(value);
     }
 
+    void divl_r(RegisterID dst)
+    {
+        m_formatter.oneByteOp(OP_GROUP3_Ev, GROUP3_OP_DIV, dst);
+    }
+
     void idivl_r(RegisterID dst)
     {
         m_formatter.oneByteOp(OP_GROUP3_Ev, GROUP3_OP_IDIV, dst);
index 4a42996..fc27f4c 100644 (file)
@@ -44,12 +44,52 @@ function asmModule(global, env, buffer) {
         return (-2147483648 / -1) | 0;
     }
 
+    function unsignedDivide() {
+        return ((-1 >>> 0) / 2) | 0;
+    }
+
+    function unsignedDivideByZero() {
+        return ((-1 >>> 0) / 0) | 0;
+    }
+
+    function modulo() {
+        return (42 % 5) | 0;
+    }
+
+    function moduloNegative() {
+        return (-42 % 5) | 0;
+    }
+
+    function moduloZero() {
+        return (1 % 0) | 0;
+    }
+
+    function moduloOverflow() {
+        return (-2147483648 % -1) | 0;
+    }
+
+    function unsignedModulo() {
+        return ((-1 >>> 0) % 100000) | 0;
+    }
+
+    function unsignedModuloZero() {
+        return ((-1 >>> 0) % 0) | 0;
+    }
+
     return {
         addSubtract: addSubtract,
         addOverflow: addOverflow,
         divide: divide,
         divideByZero: divideByZero,
         divideOverflow: divideOverflow,
+        unsignedDivide: unsignedDivide,
+        unsignedDivideByZero: unsignedDivideByZero,
+        modulo: modulo,
+        moduloNegative: moduloNegative,
+        moduloZero: moduloZero,
+        moduloOverflow: moduloOverflow,
+        unsignedModulo: unsignedModulo,
+        unsignedModuloZero: unsignedModuloZero,
     };
 }
 */
@@ -69,3 +109,27 @@ shouldThrow(() => {
 shouldThrow(() => {
     module.divideOverflow();
 }, "Error: Division by zero or division overflow.");
+
+shouldBe(module.unsignedDivide(), 2147483647);
+
+shouldThrow(() => {
+    module.unsignedDivideByZero();
+}, "Error: Division by zero or division overflow.");
+
+shouldBe(module.modulo(), 2);
+
+shouldBe(module.moduloNegative(), -2);
+
+shouldThrow(() => {
+    module.moduloZero();
+}, "Error: Division by zero or division overflow.");
+
+shouldThrow(() => {
+    module.moduloOverflow();
+}, "Error: Division by zero or division overflow.");
+
+shouldBe(module.unsignedModulo(), 67295);
+
+shouldThrow(() => {
+    module.unsignedModuloZero();
+}, "Error: Division by zero or division overflow.");
index 4ccd7b6..b837f9e 100644 (file)
Binary files a/Source/JavaScriptCore/tests/stress/wasm-arithmetic.wasm and b/Source/JavaScriptCore/tests/stress/wasm-arithmetic.wasm differ
index 99d1490..2bfa057 100644 (file)
@@ -42,6 +42,21 @@ static int32_t JIT_OPERATION operationDiv(int32_t left, int32_t right)
 {
     return left / right;
 }
+
+static int32_t JIT_OPERATION operationMod(int32_t left, int32_t right)
+{
+    return left % right;
+}
+
+static uint32_t JIT_OPERATION operationUnsignedDiv(uint32_t left, uint32_t right)
+{
+    return left / right;
+}
+
+static uint32_t JIT_OPERATION operationUnsignedMod(uint32_t left, uint32_t right)
+{
+    return left % right;
+}
 #endif
 
 class WASMFunctionCompiler : private CCallHelpers {
@@ -222,20 +237,49 @@ public:
         case WASMOpExpressionI32::Sub:
             sub32(GPRInfo::regT1, GPRInfo::regT0);
             break;
-        case WASMOpExpressionI32::SDiv: {
+        case WASMOpExpressionI32::SDiv:
+        case WASMOpExpressionI32::UDiv:
+        case WASMOpExpressionI32::SMod:
+        case WASMOpExpressionI32::UMod: {
             m_divideErrorJumpList.append(branchTest32(Zero, GPRInfo::regT1));
-            Jump denominatorNotNeg1 = branch32(NotEqual, GPRInfo::regT1, TrustedImm32(-1));
-            m_divideErrorJumpList.append(branch32(Equal, GPRInfo::regT0, TrustedImm32(-2147483647-1)));
-            denominatorNotNeg1.link(this);
+            if (op == WASMOpExpressionI32::SDiv || op == WASMOpExpressionI32::SMod) {
+                Jump denominatorNotNeg1 = branch32(NotEqual, GPRInfo::regT1, TrustedImm32(-1));
+                m_divideErrorJumpList.append(branch32(Equal, GPRInfo::regT0, TrustedImm32(-2147483647-1)));
+                denominatorNotNeg1.link(this);
+            }
 #if CPU(X86) || CPU(X86_64)
             ASSERT(GPRInfo::regT0 == X86Registers::eax);
             move(GPRInfo::regT1, X86Registers::ecx);
-            m_assembler.cdq();
-            m_assembler.idivl_r(X86Registers::ecx);
+            if (op == WASMOpExpressionI32::SDiv || op == WASMOpExpressionI32::SMod) {
+                m_assembler.cdq();
+                m_assembler.idivl_r(X86Registers::ecx);
+            } else {
+                ASSERT(op == WASMOpExpressionI32::UDiv || op == WASMOpExpressionI32::UMod);
+                xor32(X86Registers::edx, X86Registers::edx);
+                m_assembler.divl_r(X86Registers::ecx);
+            }
+            if (op == WASMOpExpressionI32::SMod || op == WASMOpExpressionI32::UMod)
+                move(X86Registers::edx, GPRInfo::regT0);
 #else
+            // FIXME: We should be able to do an inline div on ARMv7 and ARM64.
             if (maxFrameExtentForSlowPathCall)
                 addPtr(TrustedImm32(-maxFrameExtentForSlowPathCall), stackPointerRegister);
-            callOperation(operationDiv, GPRInfo::regT0, GPRInfo::regT1, GPRInfo::regT0);
+            switch (op) {
+            case WASMOpExpressionI32::SDiv:
+                callOperation(operationDiv, GPRInfo::regT0, GPRInfo::regT1, GPRInfo::regT0);
+                break;
+            case WASMOpExpressionI32::UDiv:
+                callOperation(operationUnsignedDiv, GPRInfo::regT0, GPRInfo::regT1, GPRInfo::regT0);
+                break;
+            case WASMOpExpressionI32::SMod:
+                callOperation(operationMod, GPRInfo::regT0, GPRInfo::regT1, GPRInfo::regT0);
+                break;
+            case WASMOpExpressionI32::UMod:
+                callOperation(operationUnsignedMod, GPRInfo::regT0, GPRInfo::regT1, GPRInfo::regT0);
+                break;
+            default:
+                ASSERT_NOT_REACHED();
+            }
             if (maxFrameExtentForSlowPathCall)
                 addPtr(TrustedImm32(maxFrameExtentForSlowPathCall), stackPointerRegister);
 #endif
@@ -286,6 +330,13 @@ private:
         move(GPRInfo::returnValueGPR, 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);
+    }
+
     unsigned m_stackHeight;
     unsigned m_numberOfLocals;
     unsigned m_tempStackTop { 0 };
index 9a82f48..b85ee25 100644 (file)
@@ -424,6 +424,8 @@ ContextExpression WASMFunctionParser::parseExpressionI32(Context& context)
     READ_OP_EXPRESSION_I32_OR_FAIL(hasImmediate, op, opWithImmediate, immediate, "Cannot read the int32 expression opcode.");
     if (!hasImmediate) {
         switch (op) {
+        case WASMOpExpressionI32::ConstantPoolIndex:
+            return parseConstantPoolIndexExpressionI32(context);
         case WASMOpExpressionI32::Immediate:
             return parseImmediateExpressionI32(context);
         case WASMOpExpressionI32::GetLocal:
@@ -431,8 +433,10 @@ ContextExpression WASMFunctionParser::parseExpressionI32(Context& context)
         case WASMOpExpressionI32::Add:
         case WASMOpExpressionI32::Sub:
         case WASMOpExpressionI32::SDiv:
+        case WASMOpExpressionI32::UDiv:
+        case WASMOpExpressionI32::SMod:
+        case WASMOpExpressionI32::UMod:
             return parseBinaryExpressionI32(context, op);
-        case WASMOpExpressionI32::ConstantPoolIndex:
         case WASMOpExpressionI32::GetGlobal:
         case WASMOpExpressionI32::SetLocal:
         case WASMOpExpressionI32::SetGlobal:
@@ -461,9 +465,6 @@ ContextExpression WASMFunctionParser::parseExpressionI32(Context& context)
         case WASMOpExpressionI32::FromF64:
         case WASMOpExpressionI32::Negate:
         case WASMOpExpressionI32::Mul:
-        case WASMOpExpressionI32::UDiv:
-        case WASMOpExpressionI32::SMod:
-        case WASMOpExpressionI32::UMod:
         case WASMOpExpressionI32::BitNot:
         case WASMOpExpressionI32::BitOr:
         case WASMOpExpressionI32::BitAnd:
@@ -507,13 +508,12 @@ ContextExpression WASMFunctionParser::parseExpressionI32(Context& context)
         }
     } else {
         switch (opWithImmediate) {
+        case WASMOpExpressionI32WithImmediate::ConstantPoolIndex:
+            return parseConstantPoolIndexExpressionI32(context, immediate);
         case WASMOpExpressionI32WithImmediate::Immediate:
             return parseImmediateExpressionI32(context, immediate);
         case WASMOpExpressionI32WithImmediate::GetLocal:
             return parseGetLocalExpressionI32(context, immediate);
-        case WASMOpExpressionI32WithImmediate::ConstantPoolIndex:
-            // FIXME: Implement these instructions.
-            FAIL_WITH_MESSAGE("Unsupported instruction.");
         default:
             ASSERT_NOT_REACHED();
         }
@@ -522,6 +522,21 @@ ContextExpression WASMFunctionParser::parseExpressionI32(Context& context)
 }
 
 template <class Context>
+ContextExpression WASMFunctionParser::parseConstantPoolIndexExpressionI32(Context& context, uint32_t constantPoolIndex)
+{
+    FAIL_IF_FALSE(constantPoolIndex < m_module->i32Constants().size(), "The constant pool index is incorrect.");
+    return context.buildImmediateI32(m_module->i32Constants()[constantPoolIndex]);
+}
+
+template <class Context>
+ContextExpression WASMFunctionParser::parseConstantPoolIndexExpressionI32(Context& context)
+{
+    uint32_t constantPoolIndex;
+    READ_COMPACT_UINT32_OR_FAIL(constantPoolIndex, "Cannot read the constant pool index.");
+    return parseConstantPoolIndexExpressionI32(context, constantPoolIndex);
+}
+
+template <class Context>
 ContextExpression WASMFunctionParser::parseImmediateExpressionI32(Context& context, uint32_t immediate)
 {
     return context.buildImmediateI32(immediate);
index 56b5ead..3e73647 100644 (file)
@@ -78,6 +78,8 @@ private:
     template <class Context> ContextExpression parseExpression(Context&, WASMExpressionType);
 
     template <class Context> ContextExpression parseExpressionI32(Context&);
+    template <class Context> ContextExpression parseConstantPoolIndexExpressionI32(Context&, uint32_t constantPoolIndex);
+    template <class Context> ContextExpression parseConstantPoolIndexExpressionI32(Context&);
     template <class Context> ContextExpression parseImmediateExpressionI32(Context&, uint32_t immediate);
     template <class Context> ContextExpression parseImmediateExpressionI32(Context&);
     template <class Context> ContextExpression parseGetLocalExpressionI32(Context&, uint32_t localIndex);