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

Log Message

Implement global variables in WebAssembly
https://bugs.webkit.org/show_bug.cgi?id=149031

Patch by Sukolsak Sakshuwong <[email protected]> on 2015-09-10
Reviewed by Geoffrey Garen.

This patch implements global variables in WebAssembly. There are two
types of global variables in the current format that we use (the format
used by <https://github.com/WebAssembly/polyfill-prototype-1>): internal
global variables and imported global variables. This patch does not yet
import values for imported global variables. It will be done in a
subsequent patch.

* tests/stress/wasm-globals.js: Added.
(shouldBe):
* tests/stress/wasm/globals.wasm: Added.
* wasm/JSWASMModule.h:
(JSC::JSWASMModule::globalVariables):
* wasm/WASMFunctionCompiler.h:
(JSC::WASMFunctionCompiler::buildSetGlobal):
(JSC::WASMFunctionCompiler::buildGetGlobal):
* wasm/WASMFunctionParser.cpp:
(JSC::WASMFunctionParser::parseStatement):
(JSC::WASMFunctionParser::parseSetGlobalStatement):
(JSC::WASMFunctionParser::parseExpressionI32):
(JSC::WASMFunctionParser::parseGetGlobalExpressionI32):
(JSC::WASMFunctionParser::parseExpressionF64):
(JSC::WASMFunctionParser::parseGetGlobalExpressionF64):
* wasm/WASMFunctionParser.h:
* wasm/WASMFunctionSyntaxChecker.h:
(JSC::WASMFunctionSyntaxChecker::buildSetGlobal):
(JSC::WASMFunctionSyntaxChecker::buildGetGlobal):
* wasm/WASMModuleParser.cpp:
(JSC::WASMModuleParser::parseGlobalSection):

Modified Paths

Added Paths

Diff

Modified: trunk/Source/_javascript_Core/ChangeLog (189583 => 189584)


--- trunk/Source/_javascript_Core/ChangeLog	2015-09-10 19:19:15 UTC (rev 189583)
+++ trunk/Source/_javascript_Core/ChangeLog	2015-09-10 19:34:40 UTC (rev 189584)
@@ -1,3 +1,39 @@
+2015-09-10  Sukolsak Sakshuwong  <[email protected]>
+
+        Implement global variables in WebAssembly
+        https://bugs.webkit.org/show_bug.cgi?id=149031
+
+        Reviewed by Geoffrey Garen.
+
+        This patch implements global variables in WebAssembly. There are two
+        types of global variables in the current format that we use (the format
+        used by <https://github.com/WebAssembly/polyfill-prototype-1>): internal
+        global variables and imported global variables. This patch does not yet
+        import values for imported global variables. It will be done in a
+        subsequent patch.
+
+        * tests/stress/wasm-globals.js: Added.
+        (shouldBe):
+        * tests/stress/wasm/globals.wasm: Added.
+        * wasm/JSWASMModule.h:
+        (JSC::JSWASMModule::globalVariables):
+        * wasm/WASMFunctionCompiler.h:
+        (JSC::WASMFunctionCompiler::buildSetGlobal):
+        (JSC::WASMFunctionCompiler::buildGetGlobal):
+        * wasm/WASMFunctionParser.cpp:
+        (JSC::WASMFunctionParser::parseStatement):
+        (JSC::WASMFunctionParser::parseSetGlobalStatement):
+        (JSC::WASMFunctionParser::parseExpressionI32):
+        (JSC::WASMFunctionParser::parseGetGlobalExpressionI32):
+        (JSC::WASMFunctionParser::parseExpressionF64):
+        (JSC::WASMFunctionParser::parseGetGlobalExpressionF64):
+        * wasm/WASMFunctionParser.h:
+        * wasm/WASMFunctionSyntaxChecker.h:
+        (JSC::WASMFunctionSyntaxChecker::buildSetGlobal):
+        (JSC::WASMFunctionSyntaxChecker::buildGetGlobal):
+        * wasm/WASMModuleParser.cpp:
+        (JSC::WASMModuleParser::parseGlobalSection):
+
 2015-09-10  Yusuke Suzuki  <[email protected]>
 
         Consider long module path name case in Windows

Added: trunk/Source/_javascript_Core/tests/stress/wasm/globals.wasm (0 => 189584)


--- trunk/Source/_javascript_Core/tests/stress/wasm/globals.wasm	                        (rev 0)
+++ trunk/Source/_javascript_Core/tests/stress/wasm/globals.wasm	2015-09-10 19:34:40 UTC (rev 189584)
@@ -0,0 +1 @@
+wasmy\x80\x80\x80\xA0\xC0\x80\xA1\xA0getXgetYsetXsetY
\ No newline at end of file

Added: trunk/Source/_javascript_Core/tests/stress/wasm-globals.js (0 => 189584)


--- trunk/Source/_javascript_Core/tests/stress/wasm-globals.js	                        (rev 0)
+++ trunk/Source/_javascript_Core/tests/stress/wasm-globals.js	2015-09-10 19:34:40 UTC (rev 189584)
@@ -0,0 +1,57 @@
+//@ skip
+
+function shouldBe(actual, expected) {
+    if (actual !== expected)
+        throw new Error('bad value: ' + actual);
+}
+
+/*
+wasm/globals.wasm is generated by pack-asmjs <https://github.com/WebAssembly/polyfill-prototype-1> from the following script:
+
+function asmModule(global, env, buffer) {
+    "use asm";
+
+    var x = 0;
+    var y = 0.0;
+
+    function getX() {
+        return x;
+    }
+
+    function getY() {
+        return y;
+    }
+
+    function setX(newX) {
+        newX = newX | 0;
+        x = newX;
+    }
+
+    function setY(newY) {
+        newY = +newY;
+        y = newY;
+    }
+
+    return {
+        getX: getX,
+        getY: getY,
+        setX: setX,
+        setY: setY,
+    };
+}
+*/
+
+var module = loadWebAssembly("wasm/globals.wasm");
+
+shouldBe(module.getX(), 0);
+shouldBe(module.getY(), 0);
+
+module.setX(1);
+
+shouldBe(module.getX(), 1);
+shouldBe(module.getY(), 0);
+
+module.setY(0.5);
+
+shouldBe(module.getX(), 1);
+shouldBe(module.getY(), 0.5);

Modified: trunk/Source/_javascript_Core/wasm/JSWASMModule.h (189583 => 189584)


--- trunk/Source/_javascript_Core/wasm/JSWASMModule.h	2015-09-10 19:19:15 UTC (rev 189583)
+++ trunk/Source/_javascript_Core/wasm/JSWASMModule.h	2015-09-10 19:34:40 UTC (rev 189584)
@@ -37,6 +37,25 @@
 public:
     typedef JSDestructibleObject Base;
 
+    union GlobalVariable {
+        GlobalVariable(int32_t value)
+            : intValue(value)
+        {
+        }
+        GlobalVariable(float value)
+            : floatValue(value)
+        {
+        }
+        GlobalVariable(double value)
+            : doubleValue(value)
+        {
+        }
+
+        int32_t intValue;
+        float floatValue;
+        double doubleValue;
+    };
+
     static JSWASMModule* create(VM& vm, Structure* structure)
     {
         JSWASMModule* module = new (NotNull, allocateCell<JSWASMModule>(vm.heap)) JSWASMModule(vm, structure);
@@ -67,6 +86,7 @@
     Vector<WriteBarrier<JSFunction>>& functions() { return m_functions; }
     Vector<unsigned>& functionStartOffsetsInSource() { return m_functionStartOffsetsInSource; }
     Vector<unsigned>& functionStackHeights() { return m_functionStackHeights; }
+    Vector<GlobalVariable>& globalVariables() { return m_globalVariables; }
 
 private:
     JSWASMModule(VM& vm, Structure* structure)
@@ -87,6 +107,7 @@
     Vector<WriteBarrier<JSFunction>> m_functions;
     Vector<unsigned> m_functionStartOffsetsInSource;
     Vector<unsigned> m_functionStackHeights;
+    Vector<GlobalVariable> m_globalVariables;
 };
 
 } // namespace JSC

Modified: trunk/Source/_javascript_Core/wasm/WASMFunctionCompiler.h (189583 => 189584)


--- trunk/Source/_javascript_Core/wasm/WASMFunctionCompiler.h	2015-09-10 19:19:15 UTC (rev 189583)
+++ trunk/Source/_javascript_Core/wasm/WASMFunctionCompiler.h	2015-09-10 19:34:40 UTC (rev 189584)
@@ -223,6 +223,25 @@
         }
     }
 
+    void buildSetGlobal(uint32_t globalIndex, int, WASMType type)
+    {
+        move(TrustedImmPtr(&m_module->globalVariables()[globalIndex]), GPRInfo::regT0);
+        switch (type) {
+        case WASMType::I32:
+        case WASMType::F32:
+            load32(temporaryAddress(m_tempStackTop - 1), GPRInfo::regT1);
+            store32(GPRInfo::regT1, GPRInfo::regT0);
+            break;
+        case WASMType::F64:
+            loadDouble(temporaryAddress(m_tempStackTop - 1), FPRInfo::fpRegT0);
+            storeDouble(FPRInfo::fpRegT0, GPRInfo::regT0);
+            break;
+        default:
+            ASSERT_NOT_REACHED();
+        }
+        m_tempStackTop--;
+    }
+
     void buildReturn(int, WASMExpressionType returnType)
     {
         switch (returnType) {
@@ -293,6 +312,25 @@
         return UNUSED;
     }
 
+    int buildGetGlobal(uint32_t globalIndex, WASMType type)
+    {
+        move(TrustedImmPtr(&m_module->globalVariables()[globalIndex]), GPRInfo::regT0);
+        switch (type) {
+        case WASMType::I32:
+        case WASMType::F32:
+            load32(GPRInfo::regT0, GPRInfo::regT0);
+            store32(GPRInfo::regT0, temporaryAddress(m_tempStackTop++));
+            break;
+        case WASMType::F64:
+            loadDouble(GPRInfo::regT0, FPRInfo::fpRegT0);
+            storeDouble(FPRInfo::fpRegT0, temporaryAddress(m_tempStackTop++));
+            break;
+        default:
+            ASSERT_NOT_REACHED();
+        }
+        return UNUSED;
+    }
+
     int buildUnaryI32(int, WASMOpExpressionI32 op)
     {
         load32(temporaryAddress(m_tempStackTop - 1), GPRInfo::regT0);

Modified: trunk/Source/_javascript_Core/wasm/WASMFunctionParser.cpp (189583 => 189584)


--- trunk/Source/_javascript_Core/wasm/WASMFunctionParser.cpp	2015-09-10 19:19:15 UTC (rev 189583)
+++ trunk/Source/_javascript_Core/wasm/WASMFunctionParser.cpp	2015-09-10 19:34:40 UTC (rev 189584)
@@ -137,6 +137,9 @@
         case WASMOpStatement::SetLocal:
             parseSetLocalStatement(context);
             break;
+        case WASMOpStatement::SetGlobal:
+            parseSetGlobalStatement(context);
+            break;
         case WASMOpStatement::Return:
             parseReturnStatement(context);
             break;
@@ -173,7 +176,6 @@
         case WASMOpStatement::Switch:
             parseSwitchStatement(context);
             break;
-        case WASMOpStatement::SetGlobal:
         case WASMOpStatement::I32Store8:
         case WASMOpStatement::I32StoreWithOffset8:
         case WASMOpStatement::I32Store16:
@@ -198,8 +200,8 @@
             parseSetLocalStatement(context, immediate);
             break;
         case WASMOpStatementWithImmediate::SetGlobal:
-            // FIXME: Implement this instruction.
-            FAIL_WITH_MESSAGE("Unsupported instruction.");
+            parseSetGlobalStatement(context, immediate);
+            break;
         default:
             ASSERT_NOT_REACHED();
         }
@@ -227,6 +229,25 @@
 }
 
 template <class Context>
+ContextStatement WASMFunctionParser::parseSetGlobalStatement(Context& context, uint32_t globalIndex)
+{
+    FAIL_IF_FALSE(globalIndex < m_module->globalVariableTypes().size(), "The global index is incorrect.");
+    WASMType type = m_module->globalVariableTypes()[globalIndex];
+    ContextExpression _expression_ = parseExpression(context, WASMExpressionType(type));
+    PROPAGATE_ERROR();
+    context.buildSetGlobal(globalIndex, _expression_, type);
+    return UNUSED;
+}
+
+template <class Context>
+ContextStatement WASMFunctionParser::parseSetGlobalStatement(Context& context)
+{
+    uint32_t globalIndex;
+    READ_COMPACT_UINT32_OR_FAIL(globalIndex, "Cannot read the global index.");
+    return parseSetGlobalStatement(context, globalIndex);
+}
+
+template <class Context>
 ContextStatement WASMFunctionParser::parseReturnStatement(Context& context)
 {
     ContextExpression _expression_ = 0;
@@ -474,6 +495,8 @@
             return parseImmediateExpressionI32(context);
         case WASMOpExpressionI32::GetLocal:
             return parseGetLocalExpressionI32(context);
+        case WASMOpExpressionI32::GetGlobal:
+            return parseGetGlobalExpressionI32(context);
         case WASMOpExpressionI32::CallInternal:
             return parseCallInternalExpressionI32(context);
         case WASMOpExpressionI32::Negate:
@@ -514,7 +537,6 @@
         case WASMOpExpressionI32::GreaterThanF64:
         case WASMOpExpressionI32::GreaterThanOrEqualF64:
             return parseRelationalF64ExpressionI32(context, op);
-        case WASMOpExpressionI32::GetGlobal:
         case WASMOpExpressionI32::SetLocal:
         case WASMOpExpressionI32::SetGlobal:
         case WASMOpExpressionI32::SLoad8:
@@ -615,6 +637,16 @@
 }
 
 template <class Context>
+ContextExpression WASMFunctionParser::parseGetGlobalExpressionI32(Context& context)
+{
+    uint32_t globalIndex;
+    READ_COMPACT_UINT32_OR_FAIL(globalIndex, "Cannot read the global index.");
+    FAIL_IF_FALSE(globalIndex < m_module->globalVariableTypes().size(), "The global index is incorrect.");
+    FAIL_IF_FALSE(m_module->globalVariableTypes()[globalIndex] == WASMType::I32, "Expected a global variable of type int32.");
+    return context.buildGetGlobal(globalIndex, WASMType::I32);
+}
+
+template <class Context>
 ContextExpression WASMFunctionParser::parseCallInternalExpressionI32(Context& context)
 {
     return parseCallInternal(context, WASMExpressionType::I32);
@@ -675,6 +707,7 @@
         case WASMOpExpressionF64::GetLocal:
             return parseGetLocalExpressionF64(context);
         case WASMOpExpressionF64::GetGlobal:
+            return parseGetGlobalExpressionF64(context);
         case WASMOpExpressionF64::SetLocal:
         case WASMOpExpressionF64::SetGlobal:
         case WASMOpExpressionF64::Load:
@@ -769,6 +802,16 @@
 }
 
 template <class Context>
+ContextExpression WASMFunctionParser::parseGetGlobalExpressionF64(Context& context)
+{
+    uint32_t globalIndex;
+    READ_COMPACT_UINT32_OR_FAIL(globalIndex, "Cannot read the global index.");
+    FAIL_IF_FALSE(globalIndex < m_module->globalVariableTypes().size(), "The global index is incorrect.");
+    FAIL_IF_FALSE(m_module->globalVariableTypes()[globalIndex] == WASMType::F64, "Expected a global variable of type float64.");
+    return context.buildGetGlobal(globalIndex, WASMType::F64);
+}
+
+template <class Context>
 ContextExpressionList WASMFunctionParser::parseCallArguments(Context& context, const Vector<WASMType>& arguments)
 {
     ContextExpressionList argumentList;

Modified: trunk/Source/_javascript_Core/wasm/WASMFunctionParser.h (189583 => 189584)


--- trunk/Source/_javascript_Core/wasm/WASMFunctionParser.h	2015-09-10 19:19:15 UTC (rev 189583)
+++ trunk/Source/_javascript_Core/wasm/WASMFunctionParser.h	2015-09-10 19:34:40 UTC (rev 189584)
@@ -64,6 +64,8 @@
     template <class Context> ContextStatement parseStatement(Context&);
     template <class Context> ContextStatement parseSetLocalStatement(Context&, uint32_t localIndex);
     template <class Context> ContextStatement parseSetLocalStatement(Context&);
+    template <class Context> ContextStatement parseSetGlobalStatement(Context&, uint32_t globalIndex);
+    template <class Context> ContextStatement parseSetGlobalStatement(Context&);
     template <class Context> ContextStatement parseReturnStatement(Context&);
     template <class Context> ContextStatement parseBlockStatement(Context&);
     template <class Context> ContextStatement parseIfStatement(Context&);
@@ -86,6 +88,7 @@
     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 parseGetGlobalExpressionI32(Context&);
     template <class Context> ContextExpression parseCallInternalExpressionI32(Context&);
     template <class Context> ContextExpression parseUnaryExpressionI32(Context&, WASMOpExpressionI32);
     template <class Context> ContextExpression parseBinaryExpressionI32(Context&, WASMOpExpressionI32);
@@ -98,6 +101,7 @@
     template <class Context> ContextExpression parseImmediateExpressionF64(Context&);
     template <class Context> ContextExpression parseGetLocalExpressionF64(Context&, uint32_t localIndex);
     template <class Context> ContextExpression parseGetLocalExpressionF64(Context&);
+    template <class Context> ContextExpression parseGetGlobalExpressionF64(Context&);
 
     template <class Context> ContextExpressionList parseCallArguments(Context&, const Vector<WASMType>& arguments);
     template <class Context> ContextExpression parseCallInternal(Context&, WASMExpressionType returnType);

Modified: trunk/Source/_javascript_Core/wasm/WASMFunctionSyntaxChecker.h (189583 => 189584)


--- trunk/Source/_javascript_Core/wasm/WASMFunctionSyntaxChecker.h	2015-09-10 19:19:15 UTC (rev 189583)
+++ trunk/Source/_javascript_Core/wasm/WASMFunctionSyntaxChecker.h	2015-09-10 19:34:40 UTC (rev 189584)
@@ -55,6 +55,11 @@
         m_tempStackTop--;
     }
 
+    void buildSetGlobal(uint32_t, int, WASMType)
+    {
+        m_tempStackTop--;
+    }
+
     void buildReturn(int, WASMExpressionType returnType)
     {
         if (returnType != WASMExpressionType::Void)
@@ -82,6 +87,13 @@
         return UNUSED;
     }
 
+    int buildGetGlobal(uint32_t, WASMType)
+    {
+        m_tempStackTop++;
+        updateTempStackHeight();
+        return UNUSED;
+    }
+
     int buildUnaryI32(int, WASMOpExpressionI32)
     {
         return UNUSED;

Modified: trunk/Source/_javascript_Core/wasm/WASMModuleParser.cpp (189583 => 189584)


--- trunk/Source/_javascript_Core/wasm/WASMModuleParser.cpp	2015-09-10 19:19:15 UTC (rev 189583)
+++ trunk/Source/_javascript_Core/wasm/WASMModuleParser.cpp	2015-09-10 19:34:40 UTC (rev 189584)
@@ -191,26 +191,37 @@
 
     Vector<WASMType>& globalVariableTypes = m_module->globalVariableTypes();
     globalVariableTypes.reserveInitialCapacity(numberOfGlobalVariables);
-    for (uint32_t i = 0; i < numberOfInternalI32GlobalVariables; ++i)
+    Vector<JSWASMModule::GlobalVariable>& globalVariables = m_module->globalVariables();
+    globalVariables.reserveInitialCapacity(numberOfGlobalVariables);
+    for (uint32_t i = 0; i < numberOfInternalI32GlobalVariables; ++i) {
         globalVariableTypes.uncheckedAppend(WASMType::I32);
-    for (uint32_t i = 0; i < numberOfInternalF32GlobalVariables; ++i)
+        globalVariables.uncheckedAppend(JSWASMModule::GlobalVariable(0));
+    }
+    for (uint32_t i = 0; i < numberOfInternalF32GlobalVariables; ++i) {
         globalVariableTypes.uncheckedAppend(WASMType::F32);
-    for (uint32_t i = 0; i < numberOfInternalF64GlobalVariables; ++i)
+        globalVariables.uncheckedAppend(JSWASMModule::GlobalVariable(0.0f));
+    }
+    for (uint32_t i = 0; i < numberOfInternalF64GlobalVariables; ++i) {
         globalVariableTypes.uncheckedAppend(WASMType::F64);
+        globalVariables.uncheckedAppend(JSWASMModule::GlobalVariable(0.0));
+    }
     for (uint32_t i = 0; i < numberOfImportedI32GlobalVariables; ++i) {
         String importName;
         READ_STRING_OR_FAIL(importName, "Cannot read the import name of an int32 global variable.");
         globalVariableTypes.uncheckedAppend(WASMType::I32);
+        globalVariables.uncheckedAppend(JSWASMModule::GlobalVariable(0)); // FIXME: Import the value.
     }
     for (uint32_t i = 0; i < numberOfImportedF32GlobalVariables; ++i) {
         String importName;
         READ_STRING_OR_FAIL(importName, "Cannot read the import name of a float32 global variable.");
         globalVariableTypes.uncheckedAppend(WASMType::F32);
+        globalVariables.uncheckedAppend(JSWASMModule::GlobalVariable(0.0f)); // FIXME: Import the value.
     }
     for (uint32_t i = 0; i < numberOfImportedF64GlobalVariables; ++i) {
         String importName;
         READ_STRING_OR_FAIL(importName, "Cannot read the import name of a float64 global variable.");
         globalVariableTypes.uncheckedAppend(WASMType::F64);
+        globalVariables.uncheckedAppend(JSWASMModule::GlobalVariable(0.0)); // FIXME: Import the value.
     }
 }
 
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to