Implement the GetLocal and SetLocal instructions in WebAssembly
authorcommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 4 Sep 2015 21:00:56 +0000 (21:00 +0000)
committercommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 4 Sep 2015 21:00:56 +0000 (21:00 +0000)
https://bugs.webkit.org/show_bug.cgi?id=148793

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

This patch implements the GetLocal and SetLocal instructions for locals
of type int32 in WebAssembly. A "local" in this context is either an
argument or a local variable.

* tests/stress/wasm-locals.js: Added.
(shouldBe):
* tests/stress/wasm-locals.wasm: Added.
* wasm/WASMFunctionCompiler.h:
(JSC::WASMFunctionCompiler::buildSetLocal):
(JSC::WASMFunctionCompiler::buildGetLocal):
* wasm/WASMFunctionParser.cpp:
(JSC::WASMFunctionParser::parseSetLocalStatement):
(JSC::WASMFunctionParser::parseExpressionI32):
(JSC::WASMFunctionParser::parseGetLocalExpressionI32):
* wasm/WASMFunctionParser.h:
* wasm/WASMFunctionSyntaxChecker.h:
(JSC::WASMFunctionSyntaxChecker::buildSetLocal):
(JSC::WASMFunctionSyntaxChecker::buildGetLocal):

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

Source/JavaScriptCore/ChangeLog
Source/JavaScriptCore/tests/stress/wasm-locals.js [new file with mode: 0644]
Source/JavaScriptCore/tests/stress/wasm-locals.wasm [new file with mode: 0644]
Source/JavaScriptCore/wasm/WASMFunctionCompiler.h
Source/JavaScriptCore/wasm/WASMFunctionParser.cpp
Source/JavaScriptCore/wasm/WASMFunctionParser.h
Source/JavaScriptCore/wasm/WASMFunctionSyntaxChecker.h

index 5d23109..a5ebebb 100644 (file)
@@ -1,3 +1,29 @@
+2015-09-04  Sukolsak Sakshuwong  <sukolsak@gmail.com>
+
+        Implement the GetLocal and SetLocal instructions in WebAssembly
+        https://bugs.webkit.org/show_bug.cgi?id=148793
+
+        Reviewed by Saam Barati.
+
+        This patch implements the GetLocal and SetLocal instructions for locals
+        of type int32 in WebAssembly. A "local" in this context is either an
+        argument or a local variable.
+
+        * tests/stress/wasm-locals.js: Added.
+        (shouldBe):
+        * tests/stress/wasm-locals.wasm: Added.
+        * wasm/WASMFunctionCompiler.h:
+        (JSC::WASMFunctionCompiler::buildSetLocal):
+        (JSC::WASMFunctionCompiler::buildGetLocal):
+        * wasm/WASMFunctionParser.cpp:
+        (JSC::WASMFunctionParser::parseSetLocalStatement):
+        (JSC::WASMFunctionParser::parseExpressionI32):
+        (JSC::WASMFunctionParser::parseGetLocalExpressionI32):
+        * wasm/WASMFunctionParser.h:
+        * wasm/WASMFunctionSyntaxChecker.h:
+        (JSC::WASMFunctionSyntaxChecker::buildSetLocal):
+        (JSC::WASMFunctionSyntaxChecker::buildGetLocal):
+
 2015-09-04  Basile Clement  <basile_clement@apple.com>
 
         Unreviewed, add missing copyright to file from r189336
diff --git a/Source/JavaScriptCore/tests/stress/wasm-locals.js b/Source/JavaScriptCore/tests/stress/wasm-locals.js
new file mode 100644 (file)
index 0000000..6ec9227
--- /dev/null
@@ -0,0 +1,38 @@
+//@ skip
+
+function shouldBe(actual, expected) {
+    if (actual !== expected)
+        throw new Error('bad value: ' + actual);
+}
+
+/*
+wasm-locals.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 sum(x, y) {
+        x = x | 0;
+        y = y | 0;
+        var result = 0;
+        result = (x + y) | 0;
+        return result;
+    }
+
+    function add1(x) {
+        x = x | 0;
+        return (x + 1) | 0;
+    }
+
+    return {
+        sum: sum,
+        add1: add1,
+    };
+}
+*/
+
+var module = loadWebAssembly("wasm-locals.wasm");
+
+shouldBe(module.sum(12, 30), 42);
+
+shouldBe(module.add1(42), 43);
diff --git a/Source/JavaScriptCore/tests/stress/wasm-locals.wasm b/Source/JavaScriptCore/tests/stress/wasm-locals.wasm
new file mode 100644 (file)
index 0000000..fa761b6
Binary files /dev/null and b/Source/JavaScriptCore/tests/stress/wasm-locals.wasm differ
index 7376fd9..f00f0d1 100644 (file)
@@ -127,6 +127,19 @@ public:
         m_codeBlock->capabilityLevel();
     }
 
+    void buildSetLocal(uint32_t localIndex, int, WASMType type)
+    {
+        switch (type) {
+        case WASMType::I32:
+            load32(temporaryAddress(m_tempStackTop - 1), GPRInfo::regT0);
+            m_tempStackTop--;
+            store32(GPRInfo::regT0, localAddress(localIndex));
+            break;
+        default:
+            ASSERT_NOT_REACHED();
+        }
+    }
+
     void buildReturn(int, WASMExpressionType returnType)
     {
         switch (returnType) {
@@ -151,6 +164,20 @@ public:
         return UNUSED;
     }
 
+    int buildGetLocal(uint32_t localIndex, WASMType type)
+    {
+        switch (type) {
+        case WASMType::I32:
+            load32(localAddress(localIndex), GPRInfo::regT0);
+            m_tempStackTop++;
+            store32(GPRInfo::regT0, temporaryAddress(m_tempStackTop - 1));
+            break;
+        default:
+            ASSERT_NOT_REACHED();
+        }
+        return UNUSED;
+    }
+
     int buildBinaryI32(int, int, WASMOpExpressionI32 op)
     {
         load32(temporaryAddress(m_tempStackTop - 2), GPRInfo::regT0);
index c8b6484..be8c435 100644 (file)
@@ -210,8 +210,9 @@ ContextStatement WASMFunctionParser::parseSetLocalStatement(Context& context, ui
 {
     FAIL_IF_FALSE(localIndex < m_localTypes.size(), "The local variable index is incorrect.");
     WASMType type = m_localTypes[localIndex];
-    parseExpression(context, WASMExpressionType(type));
-    // FIXME: Implement this instruction.
+    ContextExpression expression = parseExpression(context, WASMExpressionType(type));
+    PROPAGATE_ERROR();
+    context.buildSetLocal(localIndex, expression, type);
     return UNUSED;
 }
 
@@ -425,11 +426,12 @@ ContextExpression WASMFunctionParser::parseExpressionI32(Context& context)
         switch (op) {
         case WASMOpExpressionI32::Immediate:
             return parseImmediateExpressionI32(context);
+        case WASMOpExpressionI32::GetLocal:
+            return parseGetLocalExpressionI32(context);
         case WASMOpExpressionI32::Add:
         case WASMOpExpressionI32::Sub:
             return parseBinaryExpressionI32(context, op);
         case WASMOpExpressionI32::ConstantPoolIndex:
-        case WASMOpExpressionI32::GetLocal:
         case WASMOpExpressionI32::GetGlobal:
         case WASMOpExpressionI32::SetLocal:
         case WASMOpExpressionI32::SetGlobal:
@@ -507,8 +509,9 @@ ContextExpression WASMFunctionParser::parseExpressionI32(Context& context)
         switch (opWithImmediate) {
         case WASMOpExpressionI32WithImmediate::Immediate:
             return parseImmediateExpressionI32(context, immediate);
-        case WASMOpExpressionI32WithImmediate::ConstantPoolIndex:
         case WASMOpExpressionI32WithImmediate::GetLocal:
+            return parseGetLocalExpressionI32(context, immediate);
+        case WASMOpExpressionI32WithImmediate::ConstantPoolIndex:
             // FIXME: Implement these instructions.
             FAIL_WITH_MESSAGE("Unsupported instruction.");
         default:
@@ -533,6 +536,22 @@ ContextExpression WASMFunctionParser::parseImmediateExpressionI32(Context& conte
 }
 
 template <class Context>
+ContextExpression WASMFunctionParser::parseGetLocalExpressionI32(Context& context, uint32_t localIndex)
+{
+    FAIL_IF_FALSE(localIndex < m_localTypes.size(), "The local index is incorrect.");
+    FAIL_IF_FALSE(m_localTypes[localIndex] == WASMType::I32, "Expected a local of type int32.");
+    return context.buildGetLocal(localIndex, WASMType::I32);
+}
+
+template <class Context>
+ContextExpression WASMFunctionParser::parseGetLocalExpressionI32(Context& context)
+{
+    uint32_t localIndex;
+    READ_COMPACT_UINT32_OR_FAIL(localIndex, "Cannot read the local index.");
+    return parseGetLocalExpressionI32(context, localIndex);
+}
+
+template <class Context>
 ContextExpression WASMFunctionParser::parseBinaryExpressionI32(Context& context, WASMOpExpressionI32 op)
 {
     ContextExpression left = parseExpressionI32(context);
index 9ecc026..56b5ead 100644 (file)
@@ -80,6 +80,8 @@ private:
     template <class Context> ContextExpression parseExpressionI32(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);
+    template <class Context> ContextExpression parseGetLocalExpressionI32(Context&);
     template <class Context> ContextExpression parseBinaryExpressionI32(Context&, WASMOpExpressionI32);
 
     JSWASMModule* m_module;
index 1851f8d..af69daf 100644 (file)
@@ -47,6 +47,11 @@ public:
         ASSERT(!m_tempStackTop);
     }
 
+    void buildSetLocal(uint32_t, int, WASMType)
+    {
+        m_tempStackTop--;
+    }
+
     void buildReturn(int, WASMExpressionType returnType)
     {
         if (returnType != WASMExpressionType::Void)
@@ -60,6 +65,13 @@ public:
         return UNUSED;
     }
 
+    int buildGetLocal(uint32_t, WASMType)
+    {
+        m_tempStackTop++;
+        updateTempStackHeight();
+        return UNUSED;
+    }
+
     int buildBinaryI32(int, int, WASMOpExpressionI32)
     {
         m_tempStackTop--;