Title: [188808] trunk/Source/_javascript_Core
Revision
188808
Author
[email protected]
Date
2015-08-21 17:49:20 -0700 (Fri, 21 Aug 2015)

Log Message

Parse control flow statements in WebAssembly
https://bugs.webkit.org/show_bug.cgi?id=148333

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

Parse control flow statements in WebAssembly files generated by pack-asmjs
<https://github.com/WebAssembly/polyfill-prototype-1>.

* wasm/WASMConstants.h:
* wasm/WASMFunctionParser.cpp:
(JSC::WASMFunctionParser::parseStatement):
(JSC::WASMFunctionParser::parseIfStatement):
(JSC::WASMFunctionParser::parseIfElseStatement):
(JSC::WASMFunctionParser::parseWhileStatement):
(JSC::WASMFunctionParser::parseDoStatement):
(JSC::WASMFunctionParser::parseLabelStatement):
(JSC::WASMFunctionParser::parseBreakStatement):
(JSC::WASMFunctionParser::parseBreakLabelStatement):
(JSC::WASMFunctionParser::parseContinueStatement):
(JSC::WASMFunctionParser::parseContinueLabelStatement):
(JSC::WASMFunctionParser::parseSwitchStatement):
* wasm/WASMFunctionParser.h:
(JSC::WASMFunctionParser::WASMFunctionParser):
* wasm/WASMReader.cpp:
(JSC::WASMReader::readCompactInt32):
(JSC::WASMReader::readSwitchCase):
* wasm/WASMReader.h:

Modified Paths

Diff

Modified: trunk/Source/_javascript_Core/ChangeLog (188807 => 188808)


--- trunk/Source/_javascript_Core/ChangeLog	2015-08-22 00:34:31 UTC (rev 188807)
+++ trunk/Source/_javascript_Core/ChangeLog	2015-08-22 00:49:20 UTC (rev 188808)
@@ -1,3 +1,33 @@
+2015-08-21  Sukolsak Sakshuwong  <[email protected]>
+
+        Parse control flow statements in WebAssembly
+        https://bugs.webkit.org/show_bug.cgi?id=148333
+
+        Reviewed by Geoffrey Garen.
+
+        Parse control flow statements in WebAssembly files generated by pack-asmjs
+        <https://github.com/WebAssembly/polyfill-prototype-1>.
+
+        * wasm/WASMConstants.h:
+        * wasm/WASMFunctionParser.cpp:
+        (JSC::WASMFunctionParser::parseStatement):
+        (JSC::WASMFunctionParser::parseIfStatement):
+        (JSC::WASMFunctionParser::parseIfElseStatement):
+        (JSC::WASMFunctionParser::parseWhileStatement):
+        (JSC::WASMFunctionParser::parseDoStatement):
+        (JSC::WASMFunctionParser::parseLabelStatement):
+        (JSC::WASMFunctionParser::parseBreakStatement):
+        (JSC::WASMFunctionParser::parseBreakLabelStatement):
+        (JSC::WASMFunctionParser::parseContinueStatement):
+        (JSC::WASMFunctionParser::parseContinueLabelStatement):
+        (JSC::WASMFunctionParser::parseSwitchStatement):
+        * wasm/WASMFunctionParser.h:
+        (JSC::WASMFunctionParser::WASMFunctionParser):
+        * wasm/WASMReader.cpp:
+        (JSC::WASMReader::readCompactInt32):
+        (JSC::WASMReader::readSwitchCase):
+        * wasm/WASMReader.h:
+
 2015-08-21  Geoffrey Garen  <[email protected]>
 
         Debugger's VM should never be null

Modified: trunk/Source/_javascript_Core/wasm/WASMConstants.h (188807 => 188808)


--- trunk/Source/_javascript_Core/wasm/WASMConstants.h	2015-08-22 00:34:31 UTC (rev 188807)
+++ trunk/Source/_javascript_Core/wasm/WASMConstants.h	2015-08-22 00:49:20 UTC (rev 188808)
@@ -182,6 +182,16 @@
     NumberOfVariableTypesWithImmediates
 };
 
+enum class WASMSwitchCase : uint8_t {
+    CaseWithNoStatements,
+    CaseWithStatement,
+    CaseWithBlockStatement,
+    DefaultWithNoStatements,
+    DefaultWithStatement,
+    DefaultWithBlockStatement,
+    NumberOfSwitchCases
+};
+
 enum class WASMExportFormat : uint8_t {
     Default,
     Record,

Modified: trunk/Source/_javascript_Core/wasm/WASMFunctionParser.cpp (188807 => 188808)


--- trunk/Source/_javascript_Core/wasm/WASMFunctionParser.cpp	2015-08-22 00:34:31 UTC (rev 188807)
+++ trunk/Source/_javascript_Core/wasm/WASMFunctionParser.cpp	2015-08-22 00:49:20 UTC (rev 188808)
@@ -33,10 +33,12 @@
 
 #define PROPAGATE_ERROR() do { if (!m_errorMessage.isNull()) return 0; } while (0)
 #define FAIL_WITH_MESSAGE(errorMessage) do {  m_errorMessage = errorMessage; return 0; } while (0)
+#define READ_COMPACT_INT32_OR_FAIL(result, errorMessage) do { if (!m_reader.readCompactInt32(result)) FAIL_WITH_MESSAGE(errorMessage); } while (0)
 #define READ_COMPACT_UINT32_OR_FAIL(result, errorMessage) do { if (!m_reader.readCompactUInt32(result)) FAIL_WITH_MESSAGE(errorMessage); } while (0)
 #define READ_OP_STATEMENT_OR_FAIL(hasImmediate, op, opWithImmediate, immediate, errorMessage) do { if (!m_reader.readOpStatement(hasImmediate, op, opWithImmediate, immediate)) FAIL_WITH_MESSAGE(errorMessage); } while (0)
 #define READ_OP_EXPRESSION_I32_OR_FAIL(hasImmediate, op, opWithImmediate, immediate, errorMessage) do { if (!m_reader.readOpExpressionI32(hasImmediate, op, opWithImmediate, immediate)) FAIL_WITH_MESSAGE(errorMessage); } while (0)
 #define READ_VARIABLE_TYPES_OR_FAIL(hasImmediate, variableTypes, variableTypesWithImmediate, immediate, errorMessage) do { if (!m_reader.readVariableTypes(hasImmediate, variableTypes, variableTypesWithImmediate, immediate)) FAIL_WITH_MESSAGE(errorMessage); } while (0)
+#define READ_SWITCH_CASE_OR_FAIL(result, errorMessage) do { if (!m_reader.readSwitchCase(result)) FAIL_WITH_MESSAGE(errorMessage); } while (0)
 #define FAIL_IF_FALSE(condition, errorMessage) do { if (!(condition)) FAIL_WITH_MESSAGE(errorMessage); } while (0)
 
 #define UNUSED 0
@@ -123,17 +125,37 @@
         case WASMOpStatement::Block:
             parseBlockStatement(context);
             break;
-        case WASMOpStatement::SetGlobal:
         case WASMOpStatement::If:
+            parseIfStatement(context);
+            break;
         case WASMOpStatement::IfElse:
+            parseIfElseStatement(context);
+            break;
         case WASMOpStatement::While:
+            parseWhileStatement(context);
+            break;
         case WASMOpStatement::Do:
+            parseDoStatement(context);
+            break;
         case WASMOpStatement::Label:
+            parseLabelStatement(context);
+            break;
         case WASMOpStatement::Break:
+            parseBreakStatement(context);
+            break;
         case WASMOpStatement::BreakLabel:
+            parseBreakLabelStatement(context);
+            break;
         case WASMOpStatement::Continue:
+            parseContinueStatement(context);
+            break;
         case WASMOpStatement::ContinueLabel:
+            parseContinueLabelStatement(context);
+            break;
         case WASMOpStatement::Switch:
+            parseSwitchStatement(context);
+            break;
+        case WASMOpStatement::SetGlobal:
         case WASMOpStatement::I32Store8:
         case WASMOpStatement::I32StoreWithOffset8:
         case WASMOpStatement::I32Store16:
@@ -207,6 +229,155 @@
 }
 
 template <class Context>
+ContextStatement WASMFunctionParser::parseIfStatement(Context& context)
+{
+    parseExpressionI32(context);
+    PROPAGATE_ERROR();
+    parseStatement(context);
+    // FIXME: Implement this instruction.
+    return UNUSED;
+}
+
+template <class Context>
+ContextStatement WASMFunctionParser::parseIfElseStatement(Context& context)
+{
+    parseExpressionI32(context);
+    PROPAGATE_ERROR();
+    parseStatement(context);
+    PROPAGATE_ERROR();
+    parseStatement(context);
+    // FIXME: Implement this instruction.
+    return UNUSED;
+}
+
+template <class Context>
+ContextStatement WASMFunctionParser::parseWhileStatement(Context& context)
+{
+    parseExpressionI32(context);
+    PROPAGATE_ERROR();
+
+    m_breakScopeDepth++;
+    m_continueScopeDepth++;
+    parseStatement(context);
+    PROPAGATE_ERROR();
+    m_continueScopeDepth--;
+    m_breakScopeDepth--;
+    // FIXME: Implement this instruction.
+    return UNUSED;
+}
+
+template <class Context>
+ContextStatement WASMFunctionParser::parseDoStatement(Context& context)
+{
+    m_breakScopeDepth++;
+    m_continueScopeDepth++;
+    parseStatement(context);
+    PROPAGATE_ERROR();
+    m_continueScopeDepth--;
+    m_breakScopeDepth--;
+
+    parseExpressionI32(context);
+    // FIXME: Implement this instruction.
+    return UNUSED;
+}
+
+template <class Context>
+ContextStatement WASMFunctionParser::parseLabelStatement(Context& context)
+{
+    m_labelDepth++;
+    parseStatement(context);
+    PROPAGATE_ERROR();
+    m_labelDepth--;
+    // FIXME: Implement this instruction.
+    return UNUSED;
+}
+
+template <class Context>
+ContextStatement WASMFunctionParser::parseBreakStatement(Context&)
+{
+    FAIL_IF_FALSE(m_breakScopeDepth, "'break' is only valid inside a switch or loop statement.");
+    // FIXME: Implement this instruction.
+    return UNUSED;
+}
+
+template <class Context>
+ContextStatement WASMFunctionParser::parseBreakLabelStatement(Context&)
+{
+    uint32_t labelIndex;
+    READ_COMPACT_UINT32_OR_FAIL(labelIndex, "Cannot read the label index.");
+    FAIL_IF_FALSE(labelIndex < m_labelDepth, "The label index is incorrect.");
+    // FIXME: Implement this instruction.
+    return UNUSED;
+}
+
+template <class Context>
+ContextStatement WASMFunctionParser::parseContinueStatement(Context&)
+{
+    FAIL_IF_FALSE(m_continueScopeDepth, "'continue' is only valid inside a loop statement.");
+    // FIXME: Implement this instruction.
+    return UNUSED;
+}
+
+template <class Context>
+ContextStatement WASMFunctionParser::parseContinueLabelStatement(Context&)
+{
+    uint32_t labelIndex;
+    READ_COMPACT_UINT32_OR_FAIL(labelIndex, "Cannot read the label index.");
+    FAIL_IF_FALSE(labelIndex < m_labelDepth, "The label index is incorrect.");
+    // FIXME: Implement this instruction.
+    return UNUSED;
+}
+
+template <class Context>
+ContextStatement WASMFunctionParser::parseSwitchStatement(Context& context)
+{
+    uint32_t numberOfCases;
+    READ_COMPACT_UINT32_OR_FAIL(numberOfCases, "Cannot read the number of cases.");
+    parseExpressionI32(context);
+    PROPAGATE_ERROR();
+
+    m_breakScopeDepth++;
+    for (uint32_t i = 0; i < numberOfCases; ++i) {
+        WASMSwitchCase switchCase;
+        READ_SWITCH_CASE_OR_FAIL(switchCase, "Cannot read the switch case.");
+        switch (switchCase) {
+        case WASMSwitchCase::CaseWithNoStatements:
+        case WASMSwitchCase::CaseWithStatement:
+        case WASMSwitchCase::CaseWithBlockStatement: {
+            uint32_t value;
+            READ_COMPACT_INT32_OR_FAIL(value, "Cannot read the value of the switch case.");
+            if (switchCase == WASMSwitchCase::CaseWithStatement) {
+                parseStatement(context);
+                PROPAGATE_ERROR();
+            } else if (switchCase == WASMSwitchCase::CaseWithBlockStatement) {
+                parseBlockStatement(context);
+                PROPAGATE_ERROR();
+            }
+            break;
+        }
+        case WASMSwitchCase::DefaultWithNoStatements:
+        case WASMSwitchCase::DefaultWithStatement:
+        case WASMSwitchCase::DefaultWithBlockStatement: {
+            FAIL_IF_FALSE(i == numberOfCases - 1, "The default case must be the last case.");
+            if (switchCase == WASMSwitchCase::DefaultWithStatement) {
+                parseStatement(context);
+                PROPAGATE_ERROR();
+            } else if (switchCase == WASMSwitchCase::DefaultWithBlockStatement) {
+                parseBlockStatement(context);
+                PROPAGATE_ERROR();
+            }
+            break;
+        }
+        default:
+            ASSERT_NOT_REACHED();
+        }
+    }
+    m_breakScopeDepth--;
+    // FIXME: Implement this instruction.
+    return UNUSED;
+}
+
+template <class Context>
 ContextExpression WASMFunctionParser::parseExpression(Context& context, WASMExpressionType expressionType)
 {
     switch (expressionType) {

Modified: trunk/Source/_javascript_Core/wasm/WASMFunctionParser.h (188807 => 188808)


--- trunk/Source/_javascript_Core/wasm/WASMFunctionParser.h	2015-08-22 00:34:31 UTC (rev 188807)
+++ trunk/Source/_javascript_Core/wasm/WASMFunctionParser.h	2015-08-22 00:49:20 UTC (rev 188808)
@@ -47,6 +47,9 @@
         : m_module(module)
         , m_reader(static_cast<WebAssemblySourceProvider*>(source.provider())->data())
         , m_functionIndex(functionIndex)
+        , m_breakScopeDepth(0)
+        , m_continueScopeDepth(0)
+        , m_labelDepth(0)
     {
     }
 
@@ -58,6 +61,16 @@
     template <class Context> ContextStatement parseSetLocalStatement(Context&);
     template <class Context> ContextStatement parseReturnStatement(Context&);
     template <class Context> ContextStatement parseBlockStatement(Context&);
+    template <class Context> ContextStatement parseIfStatement(Context&);
+    template <class Context> ContextStatement parseIfElseStatement(Context&);
+    template <class Context> ContextStatement parseWhileStatement(Context&);
+    template <class Context> ContextStatement parseDoStatement(Context&);
+    template <class Context> ContextStatement parseLabelStatement(Context&);
+    template <class Context> ContextStatement parseBreakStatement(Context&);
+    template <class Context> ContextStatement parseBreakLabelStatement(Context&);
+    template <class Context> ContextStatement parseContinueStatement(Context&);
+    template <class Context> ContextStatement parseContinueLabelStatement(Context&);
+    template <class Context> ContextStatement parseSwitchStatement(Context&);
 
     template <class Context> ContextExpression parseExpression(Context&, WASMExpressionType);
 
@@ -75,6 +88,10 @@
     uint32_t m_numberOfI32LocalVariables;
     uint32_t m_numberOfF32LocalVariables;
     uint32_t m_numberOfF64LocalVariables;
+
+    unsigned m_breakScopeDepth;
+    unsigned m_continueScopeDepth;
+    unsigned m_labelDepth;
 };
 
 } // namespace JSC

Modified: trunk/Source/_javascript_Core/wasm/WASMReader.cpp (188807 => 188808)


--- trunk/Source/_javascript_Core/wasm/WASMReader.cpp	2015-08-22 00:34:31 UTC (rev 188807)
+++ trunk/Source/_javascript_Core/wasm/WASMReader.cpp	2015-08-22 00:49:20 UTC (rev 188808)
@@ -95,6 +95,28 @@
     return true;
 }
 
+bool WASMReader::readCompactInt32(uint32_t& result)
+{
+    uint32_t sum = 0;
+    unsigned shift = 0;
+    do {
+        CHECK_READ(1);
+        uint8_t byte = *m_cursor++;
+        if (byte < 0x80) {
+            sum |= byte << shift;
+            int signExtend = (32 - 7) - shift;
+            if (signExtend > 0)
+                result = int32_t(sum) << signExtend >> signExtend;
+            else
+                result = int32_t(sum);
+            return true;
+        }
+        sum |= (byte & firstSevenBitsMask) << shift;
+        shift += 7;
+    } while (shift < 35);
+    return false;
+}
+
 bool WASMReader::readCompactUInt32(uint32_t& result)
 {
     uint32_t sum = 0;
@@ -197,6 +219,11 @@
     return true;
 }
 
+bool WASMReader::readSwitchCase(WASMSwitchCase& result)
+{
+    return readByte<WASMSwitchCase>(result, static_cast<uint8_t>(WASMSwitchCase::NumberOfSwitchCases));
+}
+
 } // namespace JSC
 
 #endif // ENABLE(WEBASSEMBLY)

Modified: trunk/Source/_javascript_Core/wasm/WASMReader.h (188807 => 188808)


--- trunk/Source/_javascript_Core/wasm/WASMReader.h	2015-08-22 00:34:31 UTC (rev 188807)
+++ trunk/Source/_javascript_Core/wasm/WASMReader.h	2015-08-22 00:49:20 UTC (rev 188808)
@@ -48,6 +48,7 @@
     bool readUInt32(uint32_t& result);
     bool readFloat(float& result);
     bool readDouble(double& result);
+    bool readCompactInt32(uint32_t& result);
     bool readCompactUInt32(uint32_t& result);
     bool readString(String& result);
     bool readType(WASMType& result);
@@ -56,6 +57,7 @@
     bool readOpStatement(bool& hasImmediate, WASMOpStatement&, WASMOpStatementWithImmediate&, uint8_t& immediate);
     bool readOpExpressionI32(bool& hasImmediate, WASMOpExpressionI32&, WASMOpExpressionI32WithImmediate&, uint8_t& immediate);
     bool readVariableTypes(bool& hasImmediate, WASMVariableTypes&, WASMVariableTypesWithImmediate&, uint8_t& immediate);
+    bool readSwitchCase(WASMSwitchCase& result);
 
 private:
     static const uint32_t firstSevenBitsMask = 0x7f;
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to