Changeset 188778 in webkit


Ignore:
Timestamp:
Aug 21, 2015 2:46:22 PM (9 years ago)
Author:
commit-queue@webkit.org
Message:

Implement WebAssembly function parser
https://bugs.webkit.org/show_bug.cgi?id=147738

Patch by Sukolsak Sakshuwong <Sukolsak Sakshuwong> on 2015-08-21
Reviewed by Filip Pizlo.

Implement WebAssembly function parser for WebAssembly files produced by pack-asmjs
<https://github.com/WebAssembly/polyfill-prototype-1>. This patch parses only
some instructions on statements and int32 expressions. Parsing of the rest
will be implemented in subsequent patches. The instruction lists in WASMConstants.h
are slightly modified from
<https://github.com/WebAssembly/polyfill-prototype-1/blob/master/src/shared.h>.

  • CMakeLists.txt:
  • JavaScriptCore.vcxproj/JavaScriptCore.vcxproj:
  • JavaScriptCore.vcxproj/JavaScriptCore.vcxproj.filters:
  • JavaScriptCore.xcodeproj/project.pbxproj:
  • wasm/WASMConstants.h: Added.
  • wasm/WASMFormat.h:
  • wasm/WASMFunctionParser.cpp: Added.

(JSC::WASMFunctionParser::checkSyntax):
(JSC::WASMFunctionParser::parseFunction):
(JSC::WASMFunctionParser::parseLocalVariables):
(JSC::WASMFunctionParser::parseStatement):
(JSC::WASMFunctionParser::parseSetLocalStatement):
(JSC::WASMFunctionParser::parseReturnStatement):
(JSC::WASMFunctionParser::parseBlockStatement):
(JSC::WASMFunctionParser::parseExpression):
(JSC::WASMFunctionParser::parseExpressionI32):
(JSC::WASMFunctionParser::parseImmediateExpressionI32):

  • wasm/WASMFunctionParser.h: Added.

(JSC::WASMFunctionParser::WASMFunctionParser):

  • wasm/WASMFunctionSyntaxChecker.h: Renamed from Source/JavaScriptCore/wasm/WASMMagicNumber.h.
  • wasm/WASMModuleParser.cpp:

(JSC::WASMModuleParser::WASMModuleParser):
(JSC::WASMModuleParser::parseFunctionDefinitionSection):
(JSC::WASMModuleParser::parseFunctionDefinition):

  • wasm/WASMModuleParser.h:
  • wasm/WASMReader.cpp:

(JSC::WASMReader::readType):
(JSC::WASMReader::readExpressionType):
(JSC::WASMReader::readExportFormat):
(JSC::WASMReader::readOpStatement):
(JSC::WASMReader::readOpExpressionI32):
(JSC::WASMReader::readVariableTypes):
(JSC::WASMReader::readOp):

  • wasm/WASMReader.h:

(JSC::WASMReader::offset):
(JSC::WASMReader::setOffset):

Location:
trunk/Source/JavaScriptCore
Files:
3 added
10 edited
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/CMakeLists.txt

    r188752 r188778  
    424424
    425425    wasm/JSWASMModule.cpp
     426    wasm/WASMFunctionParser.cpp
    426427    wasm/WASMModuleParser.cpp
    427428    wasm/WASMReader.cpp
  • trunk/Source/JavaScriptCore/ChangeLog

    r188776 r188778  
     12015-08-21  Sukolsak Sakshuwong  <sukolsak@gmail.com>
     2
     3        Implement WebAssembly function parser
     4        https://bugs.webkit.org/show_bug.cgi?id=147738
     5
     6        Reviewed by Filip Pizlo.
     7
     8        Implement WebAssembly function parser for WebAssembly files produced by pack-asmjs
     9        <https://github.com/WebAssembly/polyfill-prototype-1>. This patch parses only
     10        some instructions on statements and int32 expressions. Parsing of the rest
     11        will be implemented in subsequent patches. The instruction lists in WASMConstants.h
     12        are slightly modified from
     13        <https://github.com/WebAssembly/polyfill-prototype-1/blob/master/src/shared.h>.
     14
     15        * CMakeLists.txt:
     16        * JavaScriptCore.vcxproj/JavaScriptCore.vcxproj:
     17        * JavaScriptCore.vcxproj/JavaScriptCore.vcxproj.filters:
     18        * JavaScriptCore.xcodeproj/project.pbxproj:
     19        * wasm/WASMConstants.h: Added.
     20        * wasm/WASMFormat.h:
     21        * wasm/WASMFunctionParser.cpp: Added.
     22        (JSC::WASMFunctionParser::checkSyntax):
     23        (JSC::WASMFunctionParser::parseFunction):
     24        (JSC::WASMFunctionParser::parseLocalVariables):
     25        (JSC::WASMFunctionParser::parseStatement):
     26        (JSC::WASMFunctionParser::parseSetLocalStatement):
     27        (JSC::WASMFunctionParser::parseReturnStatement):
     28        (JSC::WASMFunctionParser::parseBlockStatement):
     29        (JSC::WASMFunctionParser::parseExpression):
     30        (JSC::WASMFunctionParser::parseExpressionI32):
     31        (JSC::WASMFunctionParser::parseImmediateExpressionI32):
     32        * wasm/WASMFunctionParser.h: Added.
     33        (JSC::WASMFunctionParser::WASMFunctionParser):
     34        * wasm/WASMFunctionSyntaxChecker.h: Renamed from Source/JavaScriptCore/wasm/WASMMagicNumber.h.
     35        * wasm/WASMModuleParser.cpp:
     36        (JSC::WASMModuleParser::WASMModuleParser):
     37        (JSC::WASMModuleParser::parseFunctionDefinitionSection):
     38        (JSC::WASMModuleParser::parseFunctionDefinition):
     39        * wasm/WASMModuleParser.h:
     40        * wasm/WASMReader.cpp:
     41        (JSC::WASMReader::readType):
     42        (JSC::WASMReader::readExpressionType):
     43        (JSC::WASMReader::readExportFormat):
     44        (JSC::WASMReader::readOpStatement):
     45        (JSC::WASMReader::readOpExpressionI32):
     46        (JSC::WASMReader::readVariableTypes):
     47        (JSC::WASMReader::readOp):
     48        * wasm/WASMReader.h:
     49        (JSC::WASMReader::offset):
     50        (JSC::WASMReader::setOffset):
     51
    1522015-08-21  Filip Pizlo  <fpizlo@apple.com>
    253
  • trunk/Source/JavaScriptCore/JavaScriptCore.vcxproj/JavaScriptCore.vcxproj

    r188752 r188778  
    893893    <ClCompile Include="..\tools\JSDollarVMPrototype.cpp" />
    894894    <ClCompile Include="..\wasm\JSWASMModule.cpp" />
     895    <ClCompile Include="..\wasm\WASMFunctionParser.cpp" />
    895896    <ClCompile Include="..\wasm\WASMModuleParser.cpp" />
    896897    <ClCompile Include="..\wasm\WASMReader.cpp" />
     
    17871788    <ClInclude Include="..\tools\TieredMMapArray.h" />
    17881789    <ClInclude Include="..\wasm\JSWASMModule.h" />
     1790    <ClInclude Include="..\wasm\WASMConstants.h" />
    17891791    <ClInclude Include="..\wasm\WASMFormat.h" />
    1790     <ClInclude Include="..\wasm\WASMMagicNumber.h" />
     1792    <ClInclude Include="..\wasm\WASMFunctionParser.h" />
     1793    <ClInclude Include="..\wasm\WASMFunctionSyntaxChecker.h" />
    17911794    <ClInclude Include="..\wasm\WASMModuleParser.h" />
    17921795    <ClInclude Include="..\wasm\WASMReader.h" />
  • trunk/Source/JavaScriptCore/JavaScriptCore.vcxproj/JavaScriptCore.vcxproj.filters

    r188752 r188778  
    18421842      <Filter>wasm</Filter>
    18431843    </ClCompile>
     1844    <ClCompile Include="..\wasm\WASMFunctionParser.cpp">
     1845      <Filter>wasm</Filter>
     1846    </ClCompile>
    18441847    <ClCompile Include="..\wasm\WASMModuleParser.cpp">
    18451848      <Filter>wasm</Filter>
     
    44214424      <Filter>wasm</Filter>
    44224425    </ClInclude>
     4426    <ClInclude Include="..\wasm\WASMConstants.h">
     4427      <Filter>wasm</Filter>
     4428    </ClInclude>
    44234429    <ClInclude Include="..\wasm\WASMFormat.h">
    44244430      <Filter>wasm</Filter>
    44254431    </ClInclude>
    4426     <ClInclude Include="..\wasm\WASMMagicNumber.h">
     4432    <ClInclude Include="..\wasm\WASMFunctionParser.h">
     4433      <Filter>wasm</Filter>
     4434    </ClInclude>
     4435    <ClInclude Include="..\wasm\WASMFunctionSyntaxChecker.h">
    44274436      <Filter>wasm</Filter>
    44284437    </ClInclude>
  • trunk/Source/JavaScriptCore/JavaScriptCore.xcodeproj/project.pbxproj

    r188752 r188778  
    10331033                79EE0BFF1B4AFB85000385C9 /* VariableEnvironment.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 79EE0BFD1B4AFB85000385C9 /* VariableEnvironment.cpp */; };
    10341034                79EE0C001B4AFB85000385C9 /* VariableEnvironment.h in Headers */ = {isa = PBXBuildFile; fileRef = 79EE0BFE1B4AFB85000385C9 /* VariableEnvironment.h */; settings = {ATTRIBUTES = (Private, ); }; };
     1035                7B0247551B8682DD00542440 /* WASMConstants.h in Headers */ = {isa = PBXBuildFile; fileRef = 7B0247521B8682D500542440 /* WASMConstants.h */; settings = {ATTRIBUTES = (Private, ); }; };
     1036                7B0247561B8682E100542440 /* WASMFunctionParser.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7B0247531B8682D500542440 /* WASMFunctionParser.cpp */; };
     1037                7B0247571B8682E400542440 /* WASMFunctionParser.h in Headers */ = {isa = PBXBuildFile; fileRef = 7B0247541B8682D500542440 /* WASMFunctionParser.h */; settings = {ATTRIBUTES = (Private, ); }; };
     1038                7B0247591B868EB700542440 /* WASMFunctionSyntaxChecker.h in Headers */ = {isa = PBXBuildFile; fileRef = 7B0247581B868EAE00542440 /* WASMFunctionSyntaxChecker.h */; settings = {ATTRIBUTES = (Private, ); }; };
    10351039                7B39F76D1B62DE2E00360FB4 /* WASMModuleParser.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7B39F7691B62DE2200360FB4 /* WASMModuleParser.cpp */; };
    10361040                7B39F76E1B62DE3200360FB4 /* WASMModuleParser.h in Headers */ = {isa = PBXBuildFile; fileRef = 7B39F76A1B62DE2200360FB4 /* WASMModuleParser.h */; settings = {ATTRIBUTES = (Private, ); }; };
    10371041                7B39F7701B62DE3200360FB4 /* WASMReader.h in Headers */ = {isa = PBXBuildFile; fileRef = 7B39F76C1B62DE2200360FB4 /* WASMReader.h */; settings = {ATTRIBUTES = (Private, ); }; };
    10381042                7B39F7721B63574D00360FB4 /* WASMReader.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7B39F7711B63574B00360FB4 /* WASMReader.cpp */; };
    1039                 7B7A5E271B68288C0027CAD0 /* WASMMagicNumber.h in Headers */ = {isa = PBXBuildFile; fileRef = 7B7A5E261B6828840027CAD0 /* WASMMagicNumber.h */; settings = {ATTRIBUTES = (Private, ); }; };
    10401043                7B98D1361B60CD5F0023B1A4 /* JSWASMModule.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7B98D1341B60CD5A0023B1A4 /* JSWASMModule.cpp */; };
    10411044                7B98D1371B60CD620023B1A4 /* JSWASMModule.h in Headers */ = {isa = PBXBuildFile; fileRef = 7B98D1351B60CD5A0023B1A4 /* JSWASMModule.h */; settings = {ATTRIBUTES = (Private, ); }; };
     
    28432846                79EE0BFD1B4AFB85000385C9 /* VariableEnvironment.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = VariableEnvironment.cpp; sourceTree = "<group>"; };
    28442847                79EE0BFE1B4AFB85000385C9 /* VariableEnvironment.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = VariableEnvironment.h; sourceTree = "<group>"; };
     2848                7B0247521B8682D500542440 /* WASMConstants.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WASMConstants.h; sourceTree = "<group>"; };
     2849                7B0247531B8682D500542440 /* WASMFunctionParser.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WASMFunctionParser.cpp; sourceTree = "<group>"; };
     2850                7B0247541B8682D500542440 /* WASMFunctionParser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WASMFunctionParser.h; sourceTree = "<group>"; };
     2851                7B0247581B868EAE00542440 /* WASMFunctionSyntaxChecker.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WASMFunctionSyntaxChecker.h; sourceTree = "<group>"; };
    28452852                7B39F7691B62DE2200360FB4 /* WASMModuleParser.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WASMModuleParser.cpp; sourceTree = "<group>"; };
    28462853                7B39F76A1B62DE2200360FB4 /* WASMModuleParser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WASMModuleParser.h; sourceTree = "<group>"; };
    28472854                7B39F76C1B62DE2200360FB4 /* WASMReader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WASMReader.h; sourceTree = "<group>"; };
    28482855                7B39F7711B63574B00360FB4 /* WASMReader.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WASMReader.cpp; sourceTree = "<group>"; };
    2849                 7B7A5E261B6828840027CAD0 /* WASMMagicNumber.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WASMMagicNumber.h; sourceTree = "<group>"; };
    28502856                7B98D1341B60CD5A0023B1A4 /* JSWASMModule.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = JSWASMModule.cpp; sourceTree = "<group>"; };
    28512857                7B98D1351B60CD5A0023B1A4 /* JSWASMModule.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSWASMModule.h; sourceTree = "<group>"; };
     
    44454451                                7B98D1341B60CD5A0023B1A4 /* JSWASMModule.cpp */,
    44464452                                7B98D1351B60CD5A0023B1A4 /* JSWASMModule.h */,
     4453                                7B0247521B8682D500542440 /* WASMConstants.h */,
    44474454                                7BC547D21B69599B00959B58 /* WASMFormat.h */,
    4448                                 7B7A5E261B6828840027CAD0 /* WASMMagicNumber.h */,
     4455                                7B0247531B8682D500542440 /* WASMFunctionParser.cpp */,
     4456                                7B0247541B8682D500542440 /* WASMFunctionParser.h */,
     4457                                7B0247581B868EAE00542440 /* WASMFunctionSyntaxChecker.h */,
    44494458                                7B39F7691B62DE2200360FB4 /* WASMModuleParser.cpp */,
    44504459                                7B39F76A1B62DE2200360FB4 /* WASMModuleParser.h */,
     
    59725981                                86E116B10FE75AC800B512BC /* CodeLocation.h in Headers */,
    59735982                                0FBD7E691447999600481315 /* CodeOrigin.h in Headers */,
     5983                                7B0247591B868EB700542440 /* WASMFunctionSyntaxChecker.h in Headers */,
    59745984                                0F21C27D14BE727A00ADC64B /* CodeSpecializationKind.h in Headers */,
    59755985                                0F0B83A714BCF50700885B4F /* CodeType.h in Headers */,
     
    62316241                                0FCEFAE0180738C000472CE4 /* FTLLocation.h in Headers */,
    62326242                                A1587D701B4DC14100D69849 /* IntlDateTimeFormatConstructor.h in Headers */,
     6243                                7B0247571B8682E400542440 /* WASMFunctionParser.h in Headers */,
    62336244                                0FEA0A10170513DB00BB722C /* FTLLowerDFGToLLVM.h in Headers */,
    62346245                                A7D89D0217A0B90400773AD8 /* FTLLoweredNodeValue.h in Headers */,
     
    66196630                                BC18C4510E16F5CD00B34460 /* ProfileNode.h in Headers */,
    66206631                                0FF729A5166AD351000F5BA3 /* ProfilerBytecode.h in Headers */,
     6632                                7B0247551B8682DD00542440 /* WASMConstants.h in Headers */,
    66216633                                0FF729B9166AD360000F5BA3 /* ProfilerBytecodes.h in Headers */,
    66226634                                0F13912A16771C36009CCB07 /* ProfilerBytecodeSequence.h in Headers */,
     
    66426654                                0FF7168C15A3B235008F5DAA /* PropertyOffset.h in Headers */,
    66436655                                A785F6BC18C553FE00F10626 /* SpillRegistersMode.h in Headers */,
    6644                                 7B7A5E271B68288C0027CAD0 /* WASMMagicNumber.h in Headers */,
    66456656                                BC18C4550E16F5CD00B34460 /* PropertySlot.h in Headers */,
    66466657                                0FB7F39C15ED8E4600F167B2 /* PropertyStorage.h in Headers */,
     
    75767587                                E33637A51B63220200EE0840 /* ReflectObject.cpp in Sources */,
    75777588                                147F39CB107EC37600427A48 /* FunctionConstructor.cpp in Sources */,
     7589                                7B0247561B8682E100542440 /* WASMFunctionParser.cpp in Sources */,
    75787590                                0FF0F19F16B72A17005DF95B /* FunctionExecutableDump.cpp in Sources */,
    75797591                                52B310FD1974AE870080857C /* FunctionHasExecutedCache.cpp in Sources */,
  • trunk/Source/JavaScriptCore/wasm/WASMFormat.h

    r188099 r188778  
    6666};
    6767
    68 enum class WASMExportFormat : uint8_t {
    69     Default,
    70     Record,
    71     NumberOfExportFormats
    72 };
    73 
    7468struct WASMSignature {
    7569    WASMExpressionType returnType;
  • trunk/Source/JavaScriptCore/wasm/WASMFunctionSyntaxChecker.h

    r188776 r188778  
    2424 */
    2525
    26 #ifndef WASMMagicNumber_h
    27 #define WASMMagicNumber_h
     26#ifndef WASMFunctionSyntaxChecker_h
     27#define WASMFunctionSyntaxChecker_h
    2828
    2929#if ENABLE(WEBASSEMBLY)
     
    3131namespace JSC {
    3232
    33 static const uint32_t wasmMagicNumber = 0x6d736177;
     33class WASMFunctionSyntaxChecker {
     34public:
     35    typedef int Expression;
     36    typedef int Statement;
     37};
    3438
    3539} // namespace JSC
     
    3741#endif // ENABLE(WEBASSEMBLY)
    3842
    39 #endif // WASMMagicNumber_h
     43#endif // WASMFunctionSyntaxChecker_h
  • trunk/Source/JavaScriptCore/wasm/WASMModuleParser.cpp

    r188099 r188778  
    3232#include "JSWASMModule.h"
    3333#include "StrongInlines.h"
    34 #include "WASMMagicNumber.h"
     34#include "WASMConstants.h"
     35#include "WASMFunctionParser.h"
    3536#include <wtf/MathExtras.h>
    3637
     
    5051
    5152WASMModuleParser::WASMModuleParser(const SourceCode& source)
    52     : m_reader(static_cast<WebAssemblySourceProvider*>(source.provider())->data())
     53    : m_source(source)
     54    , m_reader(static_cast<WebAssemblySourceProvider*>(source.provider())->data())
    5355{
    5456}
     
    248250void WASMModuleParser::parseFunctionDefinitionSection()
    249251{
    250     for (size_t i = 0; i < m_module->functionDeclarations().size(); ++i) {
    251         parseFunctionDefinition();
     252    for (size_t functionIndex = 0; functionIndex < m_module->functionDeclarations().size(); ++functionIndex) {
     253        parseFunctionDefinition(functionIndex);
    252254        PROPAGATE_ERROR();
    253255    }
    254256}
    255257
    256 void WASMModuleParser::parseFunctionDefinition()
    257 {
    258     // FIXME: Support any functions. https://bugs.webkit.org/show_bug.cgi?id=147738
    259     // Currently, we only support functions that have "return 0;" as their only statement.
    260     // These functions consist of exactly 4 bytes, i.e.
    261     // 1. The number of local variables (0) [0x80]
    262     // 2. The number of statements (1) [0x01]
    263     // 3. The return statement [0x0f]
    264     // 4. The immediate expression (0) [0xa0]
    265     uint32_t functionDefinitionBytes;
    266     READ_UINT32_OR_FAIL(functionDefinitionBytes, "Cannot read the function definition.");
    267     FAIL_IF_FALSE(functionDefinitionBytes == 0xa00f0180, "Only functions that have \"return 0;\" "
    268         "as their only statement are supported at the moment.");
     258void WASMModuleParser::parseFunctionDefinition(size_t functionIndex)
     259{
     260    unsigned startOffsetInSource = m_reader.offset();
     261    unsigned endOffsetInSource;
     262    String errorMessage;
     263    if (!WASMFunctionParser::checkSyntax(m_module.get(), m_source, functionIndex, startOffsetInSource, endOffsetInSource, errorMessage)) {
     264        m_errorMessage = errorMessage;
     265        return;
     266    }
     267    m_reader.setOffset(endOffsetInSource);
    269268}
    270269
  • trunk/Source/JavaScriptCore/wasm/WASMModuleParser.h

    r188099 r188778  
    5555    void parseFunctionPointerTableSection();
    5656    void parseFunctionDefinitionSection();
    57     void parseFunctionDefinition();
     57    void parseFunctionDefinition(size_t functionIndex);
    5858    void parseExportSection();
    5959
     60    const SourceCode& m_source;
    6061    WASMReader m_reader;
    6162    Strong<JSWASMModule> m_module;
  • trunk/Source/JavaScriptCore/wasm/WASMReader.cpp

    r188099 r188778  
    2222 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    2323 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     24 *
     25 * =========================================================================
     26 *
     27 * Copyright (c) 2015 by the repository authors of
     28 * WebAssembly/polyfill-prototype-1.
     29 *
     30 * Licensed under the Apache License, Version 2.0 (the "License");
     31 * you may not use this file except in compliance with the License.
     32 * You may obtain a copy of the License at
     33 *
     34 *     http://www.apache.org/licenses/LICENSE-2.0
     35 *
     36 * Unless required by applicable law or agreed to in writing, software
     37 * distributed under the License is distributed on an "AS IS" BASIS,
     38 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     39 * See the License for the specific language governing permissions and
     40 * limitations under the License.
    2441 */
    2542
     
    114131bool WASMReader::readType(WASMType& result)
    115132{
    116     return readByte<WASMType>(result, (uint8_t)WASMType::NumberOfTypes);
     133    return readByte<WASMType>(result, static_cast<uint8_t>(WASMType::NumberOfTypes));
    117134}
    118135
    119136bool WASMReader::readExpressionType(WASMExpressionType& result)
    120137{
    121     return readByte<WASMExpressionType>(result, (uint8_t)WASMExpressionType::NumberOfExpressionTypes);
     138    return readByte<WASMExpressionType>(result, static_cast<uint8_t>(WASMExpressionType::NumberOfExpressionTypes));
    122139}
    123140
    124141bool WASMReader::readExportFormat(WASMExportFormat& result)
    125142{
    126     return readByte<WASMExportFormat>(result, (uint8_t)WASMExportFormat::NumberOfExportFormats);
     143    return readByte<WASMExportFormat>(result, static_cast<uint8_t>(WASMExportFormat::NumberOfExportFormats));
    127144}
    128145
     
    137154}
    138155
     156bool WASMReader::readOpStatement(bool& hasImmediate, WASMOpStatement& op, WASMOpStatementWithImmediate& opWithImmediate, uint8_t& immediate)
     157{
     158    return readOp(hasImmediate, op, opWithImmediate, immediate,
     159        static_cast<uint8_t>(WASMOpStatement::NumberOfWASMOpStatements),
     160        static_cast<uint8_t>(WASMOpStatementWithImmediate::NumberOfWASMOpStatementWithImmediates));
     161}
     162
     163bool WASMReader::readOpExpressionI32(bool& hasImmediate, WASMOpExpressionI32& op, WASMOpExpressionI32WithImmediate& opWithImmediate, uint8_t& immediate)
     164{
     165    return readOp(hasImmediate, op, opWithImmediate, immediate,
     166        static_cast<uint8_t>(WASMOpExpressionI32::NumberOfWASMOpExpressionI32s),
     167        static_cast<uint8_t>(WASMOpExpressionI32WithImmediate::NumberOfWASMOpExpressionI32WithImmediates));
     168}
     169
     170bool WASMReader::readVariableTypes(bool& hasImmediate, WASMVariableTypes& variableTypes, WASMVariableTypesWithImmediate& variableTypesWithImmediate, uint8_t& immediate)
     171{
     172    return readOp(hasImmediate, variableTypes, variableTypesWithImmediate, immediate,
     173        static_cast<uint8_t>(WASMVariableTypes::NumberOfVariableTypes),
     174        static_cast<uint8_t>(WASMVariableTypesWithImmediate::NumberOfVariableTypesWithImmediates));
     175}
     176
     177template <class T, class TWithImmediate>
     178bool WASMReader::readOp(bool& hasImmediate, T& op, TWithImmediate& opWithImmediate, uint8_t& immediate, uint8_t numberOfValues, uint8_t numberOfValuesWithImmediate)
     179{
     180    CHECK_READ(1);
     181    uint8_t byte = *m_cursor++;
     182
     183    if (!(byte & hasImmediateInOpFlag)) {
     184        if (byte >= numberOfValues)
     185            return false;
     186        hasImmediate = false;
     187        op = T(byte);
     188        return true;
     189    }
     190
     191    uint8_t byteWithoutImmediate = (byte >> immediateBits) & (opWithImmediateLimit - 1);
     192    if (byteWithoutImmediate >= numberOfValuesWithImmediate)
     193        return false;
     194    hasImmediate = true;
     195    opWithImmediate = TWithImmediate(byteWithoutImmediate);
     196    immediate = byte & (immediateLimit - 1);
     197    return true;
     198}
     199
    139200} // namespace JSC
    140201
  • trunk/Source/JavaScriptCore/wasm/WASMReader.h

    r188099 r188778  
    2929#if ENABLE(WEBASSEMBLY)
    3030
     31#include "WASMConstants.h"
    3132#include "WASMFormat.h"
    3233#include <wtf/Vector.h>
     
    4243    }
    4344
     45    unsigned offset() const { return m_cursor - m_buffer.data(); }
     46    void setOffset(unsigned offset) { m_cursor = m_buffer.data() + offset; }
     47
    4448    bool readUInt32(uint32_t& result);
    4549    bool readFloat(float& result);
     
    5054    bool readExpressionType(WASMExpressionType& result);
    5155    bool readExportFormat(WASMExportFormat& result);
     56    bool readOpStatement(bool& hasImmediate, WASMOpStatement&, WASMOpStatementWithImmediate&, uint8_t& immediate);
     57    bool readOpExpressionI32(bool& hasImmediate, WASMOpExpressionI32&, WASMOpExpressionI32WithImmediate&, uint8_t& immediate);
     58    bool readVariableTypes(bool& hasImmediate, WASMVariableTypes&, WASMVariableTypesWithImmediate&, uint8_t& immediate);
    5259
    5360private:
     
    5562
    5663    template <class T> bool readByte(T& result, uint8_t numberOfValues);
     64    template <class T, class TWithImmediate> bool readOp(bool& hasImmediate, T&, TWithImmediate&, uint8_t& immediate, uint8_t numberOfValues, uint8_t numberOfValuesWithImmediate);
    5765
    5866    const Vector<uint8_t>& m_buffer;
Note: See TracChangeset for help on using the changeset viewer.