Title: [188778] trunk/Source/_javascript_Core
Revision
188778
Author
[email protected]
Date
2015-08-21 14:46:22 -0700 (Fri, 21 Aug 2015)

Log Message

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

Patch by Sukolsak Sakshuwong <[email protected]> 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:
* _javascript_Core.vcxproj/_javascript_Core.vcxproj:
* _javascript_Core.vcxproj/_javascript_Core.vcxproj.filters:
* _javascript_Core.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/_javascript_Core/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):

Modified Paths

Added Paths

Removed Paths

Diff

Modified: trunk/Source/_javascript_Core/CMakeLists.txt (188777 => 188778)


--- trunk/Source/_javascript_Core/CMakeLists.txt	2015-08-21 21:44:34 UTC (rev 188777)
+++ trunk/Source/_javascript_Core/CMakeLists.txt	2015-08-21 21:46:22 UTC (rev 188778)
@@ -423,6 +423,7 @@
     tools/JSDollarVMPrototype.cpp
 
     wasm/JSWASMModule.cpp
+    wasm/WASMFunctionParser.cpp
     wasm/WASMModuleParser.cpp
     wasm/WASMReader.cpp
 

Modified: trunk/Source/_javascript_Core/ChangeLog (188777 => 188778)


--- trunk/Source/_javascript_Core/ChangeLog	2015-08-21 21:44:34 UTC (rev 188777)
+++ trunk/Source/_javascript_Core/ChangeLog	2015-08-21 21:46:22 UTC (rev 188778)
@@ -1,3 +1,54 @@
+2015-08-21  Sukolsak Sakshuwong  <[email protected]>
+
+        Implement WebAssembly function parser
+        https://bugs.webkit.org/show_bug.cgi?id=147738
+
+        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:
+        * _javascript_Core.vcxproj/_javascript_Core.vcxproj:
+        * _javascript_Core.vcxproj/_javascript_Core.vcxproj.filters:
+        * _javascript_Core.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/_javascript_Core/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):
+
 2015-08-21  Filip Pizlo  <[email protected]>
 
         DFG::PutStackSinkingPhase doesn't need to emit KillStack nodes

Modified: trunk/Source/_javascript_Core/_javascript_Core.vcxproj/_javascript_Core.vcxproj (188777 => 188778)


--- trunk/Source/_javascript_Core/_javascript_Core.vcxproj/_javascript_Core.vcxproj	2015-08-21 21:44:34 UTC (rev 188777)
+++ trunk/Source/_javascript_Core/_javascript_Core.vcxproj/_javascript_Core.vcxproj	2015-08-21 21:46:22 UTC (rev 188778)
@@ -892,6 +892,7 @@
     <ClCompile Include="..\tools\JSDollarVM.cpp" />
     <ClCompile Include="..\tools\JSDollarVMPrototype.cpp" />
     <ClCompile Include="..\wasm\JSWASMModule.cpp" />
+    <ClCompile Include="..\wasm\WASMFunctionParser.cpp" />
     <ClCompile Include="..\wasm\WASMModuleParser.cpp" />
     <ClCompile Include="..\wasm\WASMReader.cpp" />
     <ClCompile Include="..\yarr\RegularExpression.cpp" />
@@ -1786,8 +1787,10 @@
     <ClInclude Include="..\tools\ProfileTreeNode.h" />
     <ClInclude Include="..\tools\TieredMMapArray.h" />
     <ClInclude Include="..\wasm\JSWASMModule.h" />
+    <ClInclude Include="..\wasm\WASMConstants.h" />
     <ClInclude Include="..\wasm\WASMFormat.h" />
-    <ClInclude Include="..\wasm\WASMMagicNumber.h" />
+    <ClInclude Include="..\wasm\WASMFunctionParser.h" />
+    <ClInclude Include="..\wasm\WASMFunctionSyntaxChecker.h" />
     <ClInclude Include="..\wasm\WASMModuleParser.h" />
     <ClInclude Include="..\wasm\WASMReader.h" />
     <ClInclude Include="..\yarr\RegularExpression.h" />

Modified: trunk/Source/_javascript_Core/_javascript_Core.vcxproj/_javascript_Core.vcxproj.filters (188777 => 188778)


--- trunk/Source/_javascript_Core/_javascript_Core.vcxproj/_javascript_Core.vcxproj.filters	2015-08-21 21:44:34 UTC (rev 188777)
+++ trunk/Source/_javascript_Core/_javascript_Core.vcxproj/_javascript_Core.vcxproj.filters	2015-08-21 21:46:22 UTC (rev 188778)
@@ -1841,6 +1841,9 @@
     <ClCompile Include="..\wasm\JSWASMModule.cpp">
       <Filter>wasm</Filter>
     </ClCompile>
+    <ClCompile Include="..\wasm\WASMFunctionParser.cpp">
+      <Filter>wasm</Filter>
+    </ClCompile>
     <ClCompile Include="..\wasm\WASMModuleParser.cpp">
       <Filter>wasm</Filter>
     </ClCompile>
@@ -4420,12 +4423,18 @@
     <ClInclude Include="..\wasm\JSWASMModule.h">
       <Filter>wasm</Filter>
     </ClInclude>
+    <ClInclude Include="..\wasm\WASMConstants.h">
+      <Filter>wasm</Filter>
+    </ClInclude>
     <ClInclude Include="..\wasm\WASMFormat.h">
       <Filter>wasm</Filter>
     </ClInclude>
-    <ClInclude Include="..\wasm\WASMMagicNumber.h">
+    <ClInclude Include="..\wasm\WASMFunctionParser.h">
       <Filter>wasm</Filter>
     </ClInclude>
+    <ClInclude Include="..\wasm\WASMFunctionSyntaxChecker.h">
+      <Filter>wasm</Filter>
+    </ClInclude>
     <ClInclude Include="..\wasm\WASMModuleParser.h">
       <Filter>wasm</Filter>
     </ClInclude>

Modified: trunk/Source/_javascript_Core/_javascript_Core.xcodeproj/project.pbxproj (188777 => 188778)


--- trunk/Source/_javascript_Core/_javascript_Core.xcodeproj/project.pbxproj	2015-08-21 21:44:34 UTC (rev 188777)
+++ trunk/Source/_javascript_Core/_javascript_Core.xcodeproj/project.pbxproj	2015-08-21 21:46:22 UTC (rev 188778)
@@ -1032,11 +1032,14 @@
 		70ECA6091AFDBEA200449739 /* TemplateRegistryKey.h in Headers */ = {isa = PBXBuildFile; fileRef = 70ECA6041AFDBEA200449739 /* TemplateRegistryKey.h */; settings = {ATTRIBUTES = (Private, ); }; };
 		79EE0BFF1B4AFB85000385C9 /* VariableEnvironment.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 79EE0BFD1B4AFB85000385C9 /* VariableEnvironment.cpp */; };
 		79EE0C001B4AFB85000385C9 /* VariableEnvironment.h in Headers */ = {isa = PBXBuildFile; fileRef = 79EE0BFE1B4AFB85000385C9 /* VariableEnvironment.h */; settings = {ATTRIBUTES = (Private, ); }; };
+		7B0247551B8682DD00542440 /* WASMConstants.h in Headers */ = {isa = PBXBuildFile; fileRef = 7B0247521B8682D500542440 /* WASMConstants.h */; settings = {ATTRIBUTES = (Private, ); }; };
+		7B0247561B8682E100542440 /* WASMFunctionParser.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7B0247531B8682D500542440 /* WASMFunctionParser.cpp */; };
+		7B0247571B8682E400542440 /* WASMFunctionParser.h in Headers */ = {isa = PBXBuildFile; fileRef = 7B0247541B8682D500542440 /* WASMFunctionParser.h */; settings = {ATTRIBUTES = (Private, ); }; };
+		7B0247591B868EB700542440 /* WASMFunctionSyntaxChecker.h in Headers */ = {isa = PBXBuildFile; fileRef = 7B0247581B868EAE00542440 /* WASMFunctionSyntaxChecker.h */; settings = {ATTRIBUTES = (Private, ); }; };
 		7B39F76D1B62DE2E00360FB4 /* WASMModuleParser.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7B39F7691B62DE2200360FB4 /* WASMModuleParser.cpp */; };
 		7B39F76E1B62DE3200360FB4 /* WASMModuleParser.h in Headers */ = {isa = PBXBuildFile; fileRef = 7B39F76A1B62DE2200360FB4 /* WASMModuleParser.h */; settings = {ATTRIBUTES = (Private, ); }; };
 		7B39F7701B62DE3200360FB4 /* WASMReader.h in Headers */ = {isa = PBXBuildFile; fileRef = 7B39F76C1B62DE2200360FB4 /* WASMReader.h */; settings = {ATTRIBUTES = (Private, ); }; };
 		7B39F7721B63574D00360FB4 /* WASMReader.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7B39F7711B63574B00360FB4 /* WASMReader.cpp */; };
-		7B7A5E271B68288C0027CAD0 /* WASMMagicNumber.h in Headers */ = {isa = PBXBuildFile; fileRef = 7B7A5E261B6828840027CAD0 /* WASMMagicNumber.h */; settings = {ATTRIBUTES = (Private, ); }; };
 		7B98D1361B60CD5F0023B1A4 /* JSWASMModule.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7B98D1341B60CD5A0023B1A4 /* JSWASMModule.cpp */; };
 		7B98D1371B60CD620023B1A4 /* JSWASMModule.h in Headers */ = {isa = PBXBuildFile; fileRef = 7B98D1351B60CD5A0023B1A4 /* JSWASMModule.h */; settings = {ATTRIBUTES = (Private, ); }; };
 		7BC547D31B6959A100959B58 /* WASMFormat.h in Headers */ = {isa = PBXBuildFile; fileRef = 7BC547D21B69599B00959B58 /* WASMFormat.h */; settings = {ATTRIBUTES = (Private, ); }; };
@@ -2842,11 +2845,14 @@
 		70ECA6041AFDBEA200449739 /* TemplateRegistryKey.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TemplateRegistryKey.h; sourceTree = "<group>"; };
 		79EE0BFD1B4AFB85000385C9 /* VariableEnvironment.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = VariableEnvironment.cpp; sourceTree = "<group>"; };
 		79EE0BFE1B4AFB85000385C9 /* VariableEnvironment.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = VariableEnvironment.h; sourceTree = "<group>"; };
+		7B0247521B8682D500542440 /* WASMConstants.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WASMConstants.h; sourceTree = "<group>"; };
+		7B0247531B8682D500542440 /* WASMFunctionParser.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WASMFunctionParser.cpp; sourceTree = "<group>"; };
+		7B0247541B8682D500542440 /* WASMFunctionParser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WASMFunctionParser.h; sourceTree = "<group>"; };
+		7B0247581B868EAE00542440 /* WASMFunctionSyntaxChecker.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WASMFunctionSyntaxChecker.h; sourceTree = "<group>"; };
 		7B39F7691B62DE2200360FB4 /* WASMModuleParser.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WASMModuleParser.cpp; sourceTree = "<group>"; };
 		7B39F76A1B62DE2200360FB4 /* WASMModuleParser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WASMModuleParser.h; sourceTree = "<group>"; };
 		7B39F76C1B62DE2200360FB4 /* WASMReader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WASMReader.h; sourceTree = "<group>"; };
 		7B39F7711B63574B00360FB4 /* WASMReader.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WASMReader.cpp; sourceTree = "<group>"; };
-		7B7A5E261B6828840027CAD0 /* WASMMagicNumber.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WASMMagicNumber.h; sourceTree = "<group>"; };
 		7B98D1341B60CD5A0023B1A4 /* JSWASMModule.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = JSWASMModule.cpp; sourceTree = "<group>"; };
 		7B98D1351B60CD5A0023B1A4 /* JSWASMModule.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSWASMModule.h; sourceTree = "<group>"; };
 		7BC547D21B69599B00959B58 /* WASMFormat.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WASMFormat.h; sourceTree = "<group>"; };
@@ -4444,8 +4450,11 @@
 			children = (
 				7B98D1341B60CD5A0023B1A4 /* JSWASMModule.cpp */,
 				7B98D1351B60CD5A0023B1A4 /* JSWASMModule.h */,
+				7B0247521B8682D500542440 /* WASMConstants.h */,
 				7BC547D21B69599B00959B58 /* WASMFormat.h */,
-				7B7A5E261B6828840027CAD0 /* WASMMagicNumber.h */,
+				7B0247531B8682D500542440 /* WASMFunctionParser.cpp */,
+				7B0247541B8682D500542440 /* WASMFunctionParser.h */,
+				7B0247581B868EAE00542440 /* WASMFunctionSyntaxChecker.h */,
 				7B39F7691B62DE2200360FB4 /* WASMModuleParser.cpp */,
 				7B39F76A1B62DE2200360FB4 /* WASMModuleParser.h */,
 				7B39F7711B63574B00360FB4 /* WASMReader.cpp */,
@@ -5971,6 +5980,7 @@
 				A77F1822164088B200640A47 /* CodeCache.h in Headers */,
 				86E116B10FE75AC800B512BC /* CodeLocation.h in Headers */,
 				0FBD7E691447999600481315 /* CodeOrigin.h in Headers */,
+				7B0247591B868EB700542440 /* WASMFunctionSyntaxChecker.h in Headers */,
 				0F21C27D14BE727A00ADC64B /* CodeSpecializationKind.h in Headers */,
 				0F0B83A714BCF50700885B4F /* CodeType.h in Headers */,
 				BC18C3F30E16F5CD00B34460 /* CommonIdentifiers.h in Headers */,
@@ -6230,6 +6240,7 @@
 				0F8F2B96172E04A3007DBDA5 /* FTLLink.h in Headers */,
 				0FCEFAE0180738C000472CE4 /* FTLLocation.h in Headers */,
 				A1587D701B4DC14100D69849 /* IntlDateTimeFormatConstructor.h in Headers */,
+				7B0247571B8682E400542440 /* WASMFunctionParser.h in Headers */,
 				0FEA0A10170513DB00BB722C /* FTLLowerDFGToLLVM.h in Headers */,
 				A7D89D0217A0B90400773AD8 /* FTLLoweredNodeValue.h in Headers */,
 				0FD8A31C17D51F2200CA2C40 /* FTLOSREntry.h in Headers */,
@@ -6618,6 +6629,7 @@
 				95CD45770E1C4FDD0085358E /* ProfileGenerator.h in Headers */,
 				BC18C4510E16F5CD00B34460 /* ProfileNode.h in Headers */,
 				0FF729A5166AD351000F5BA3 /* ProfilerBytecode.h in Headers */,
+				7B0247551B8682DD00542440 /* WASMConstants.h in Headers */,
 				0FF729B9166AD360000F5BA3 /* ProfilerBytecodes.h in Headers */,
 				0F13912A16771C36009CCB07 /* ProfilerBytecodeSequence.h in Headers */,
 				0FF729BA166AD360000F5BA3 /* ProfilerCompilation.h in Headers */,
@@ -6641,7 +6653,6 @@
 				BC18C4540E16F5CD00B34460 /* PropertyNameArray.h in Headers */,
 				0FF7168C15A3B235008F5DAA /* PropertyOffset.h in Headers */,
 				A785F6BC18C553FE00F10626 /* SpillRegistersMode.h in Headers */,
-				7B7A5E271B68288C0027CAD0 /* WASMMagicNumber.h in Headers */,
 				BC18C4550E16F5CD00B34460 /* PropertySlot.h in Headers */,
 				0FB7F39C15ED8E4600F167B2 /* PropertyStorage.h in Headers */,
 				0F6FC751196110A800E1D02D /* ComplexGetStatus.h in Headers */,
@@ -7575,6 +7586,7 @@
 				0F5A6283188C98D40072C9DF /* FTLValueRange.cpp in Sources */,
 				E33637A51B63220200EE0840 /* ReflectObject.cpp in Sources */,
 				147F39CB107EC37600427A48 /* FunctionConstructor.cpp in Sources */,
+				7B0247561B8682E100542440 /* WASMFunctionParser.cpp in Sources */,
 				0FF0F19F16B72A17005DF95B /* FunctionExecutableDump.cpp in Sources */,
 				52B310FD1974AE870080857C /* FunctionHasExecutedCache.cpp in Sources */,
 				147F39CC107EC37600427A48 /* FunctionPrototype.cpp in Sources */,

Added: trunk/Source/_javascript_Core/wasm/WASMConstants.h (0 => 188778)


--- trunk/Source/_javascript_Core/wasm/WASMConstants.h	                        (rev 0)
+++ trunk/Source/_javascript_Core/wasm/WASMConstants.h	2015-08-21 21:46:22 UTC (rev 188778)
@@ -0,0 +1,203 @@
+/*
+ * Copyright (C) 2015 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * =========================================================================
+ *
+ * Copyright (c) 2015 by the repository authors of
+ * WebAssembly/polyfill-prototype-1.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef WASMConstants_h
+#define WASMConstants_h
+
+#if ENABLE(WEBASSEMBLY)
+
+namespace JSC {
+
+static const uint32_t wasmMagicNumber = 0x6d736177;
+
+enum class WASMOpStatement : uint8_t {
+    SetLocal,
+    SetGlobal,
+    I32Store8,
+    I32StoreWithOffset8,
+    I32Store16,
+    I32StoreWithOffset16,
+    I32Store32,
+    I32StoreWithOffset32,
+    F32Store,
+    F32StoreWithOffset,
+    F64Store,
+    F64StoreWithOffset,
+    CallInternal,
+    CallIndirect,
+    CallImport,
+    Return,
+    Block,
+    If,
+    IfElse,
+    While,
+    Do,
+    Label,
+    Break,
+    BreakLabel,
+    Continue,
+    ContinueLabel,
+    Switch,
+    NumberOfWASMOpStatements
+};
+
+enum class WASMOpStatementWithImmediate : uint8_t {
+    SetLocal,
+    SetGlobal,
+    NumberOfWASMOpStatementWithImmediates
+};
+
+enum class WASMOpExpressionI32 : uint8_t {
+    ConstantPoolIndex,
+    Immediate,
+    GetLocal,
+    GetGlobal,
+    SetLocal,
+    SetGlobal,
+    SLoad8,
+    SLoadWithOffset8,
+    ULoad8,
+    ULoadWithOffset8,
+    SLoad16,
+    SLoadWithOffset16,
+    ULoad16,
+    ULoadWithOffset16,
+    Load32,
+    LoadWithOffset32,
+    Store8,
+    StoreWithOffset8,
+    Store16,
+    StoreWithOffset16,
+    Store32,
+    StoreWithOffset32,
+    CallInternal,
+    CallIndirect,
+    CallImport,
+    Conditional,
+    Comma,
+    FromF32,
+    FromF64,
+    Negate,
+    Add,
+    Sub,
+    Mul,
+    SDiv,
+    UDiv,
+    SMod,
+    UMod,
+    BitNot,
+    BitOr,
+    BitAnd,
+    BitXor,
+    LeftShift,
+    ArithmeticRightShift,
+    LogicalRightShift,
+    CountLeadingZeros,
+    LogicalNot,
+    EqualI32,
+    EqualF32,
+    EqualF64,
+    NotEqualI32,
+    NotEqualF32,
+    NotEqualF64,
+    SLessThanI32,
+    ULessThanI32,
+    LessThanF32,
+    LessThanF64,
+    SLessThanOrEqualI32,
+    ULessThanOrEqualI32,
+    LessThanOrEqualF32,
+    LessThanOrEqualF64,
+    SGreaterThanI32,
+    UGreaterThanI32,
+    GreaterThanF32,
+    GreaterThanF64,
+    SGreaterThanOrEqualI32,
+    UGreaterThanOrEqualI32,
+    GreaterThanOrEqualF32,
+    GreaterThanOrEqualF64,
+    SMin,
+    UMin,
+    SMax,
+    UMax,
+    Abs,
+    NumberOfWASMOpExpressionI32s
+};
+
+enum class WASMOpExpressionI32WithImmediate : uint8_t {
+    ConstantPoolIndex,
+    Immediate,
+    GetLocal,
+    NumberOfWASMOpExpressionI32WithImmediates
+};
+
+enum class WASMVariableTypes : uint8_t {
+    I32 = 1 << 0,
+    F32 = 1 << 1,
+    F64 = 1 << 2,
+    NumberOfVariableTypes = 8
+};
+
+enum class WASMVariableTypesWithImmediate : uint8_t {
+    I32,
+    NumberOfVariableTypesWithImmediates
+};
+
+enum class WASMExportFormat : uint8_t {
+    Default,
+    Record,
+    NumberOfExportFormats
+};
+
+static const uint8_t hasImmediateInOpFlag = 0x80;
+
+static const unsigned opWithImmediateBits = 2;
+static const uint32_t opWithImmediateLimit = 1 << opWithImmediateBits;
+
+static const unsigned immediateBits = 5;
+static const uint32_t immediateLimit = 1 << immediateBits;
+
+} // namespace JSC
+
+#endif // ENABLE(WEBASSEMBLY)
+
+#endif // WASMConstants_h

Modified: trunk/Source/_javascript_Core/wasm/WASMFormat.h (188777 => 188778)


--- trunk/Source/_javascript_Core/wasm/WASMFormat.h	2015-08-21 21:44:34 UTC (rev 188777)
+++ trunk/Source/_javascript_Core/wasm/WASMFormat.h	2015-08-21 21:46:22 UTC (rev 188778)
@@ -65,12 +65,6 @@
     NumberOfExpressionTypes
 };
 
-enum class WASMExportFormat : uint8_t {
-    Default,
-    Record,
-    NumberOfExportFormats
-};
-
 struct WASMSignature {
     WASMExpressionType returnType;
     Vector<WASMType> arguments;

Added: trunk/Source/_javascript_Core/wasm/WASMFunctionParser.cpp (0 => 188778)


--- trunk/Source/_javascript_Core/wasm/WASMFunctionParser.cpp	                        (rev 0)
+++ trunk/Source/_javascript_Core/wasm/WASMFunctionParser.cpp	2015-08-21 21:46:22 UTC (rev 188778)
@@ -0,0 +1,347 @@
+/*
+ * Copyright (C) 2015 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "WASMFunctionParser.h"
+
+#if ENABLE(WEBASSEMBLY)
+
+#include "JSWASMModule.h"
+#include "WASMFunctionSyntaxChecker.h"
+
+#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_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 FAIL_IF_FALSE(condition, errorMessage) do { if (!(condition)) FAIL_WITH_MESSAGE(errorMessage); } while (0)
+
+#define UNUSED 0
+
+namespace JSC {
+
+bool WASMFunctionParser::checkSyntax(JSWASMModule* module, const SourceCode& source, size_t functionIndex, unsigned startOffsetInSource, unsigned& endOffsetInSource, String& errorMessage)
+{
+    WASMFunctionParser parser(module, source, functionIndex);
+    WASMFunctionSyntaxChecker syntaxChecker;
+    parser.m_reader.setOffset(startOffsetInSource);
+    parser.parseFunction(syntaxChecker);
+    if (!parser.m_errorMessage.isNull()) {
+        errorMessage = parser.m_errorMessage;
+        return false;
+    }
+    endOffsetInSource = parser.m_reader.offset();
+    return true;
+}
+
+template <class Context>
+bool WASMFunctionParser::parseFunction(Context& context)
+{
+    const WASMSignature& signature = m_module->signatures()[m_module->functionDeclarations()[m_functionIndex].signatureIndex];
+
+    m_returnType = signature.returnType;
+
+    parseLocalVariables();
+    PROPAGATE_ERROR();
+
+    const Vector<WASMType>& arguments = signature.arguments;
+    for (size_t i = 0; i < arguments.size(); ++i)
+        m_localTypes.append(arguments[i]);
+    for (uint32_t i = 0; i < m_numberOfI32LocalVariables; ++i)
+        m_localTypes.append(WASMType::I32);
+    for (uint32_t i = 0; i < m_numberOfF32LocalVariables; ++i)
+        m_localTypes.append(WASMType::F32);
+    for (uint32_t i = 0; i < m_numberOfF64LocalVariables; ++i)
+        m_localTypes.append(WASMType::F64);
+
+    parseBlockStatement(context);
+    return true;
+}
+
+bool WASMFunctionParser::parseLocalVariables()
+{
+    m_numberOfI32LocalVariables = 0;
+    m_numberOfF32LocalVariables = 0;
+    m_numberOfF64LocalVariables = 0;
+
+    bool hasImmediate;
+    WASMVariableTypes variableTypes;
+    WASMVariableTypesWithImmediate variableTypesWithImmediate;
+    uint8_t immediate;
+    READ_VARIABLE_TYPES_OR_FAIL(hasImmediate, variableTypes, variableTypesWithImmediate, immediate, "Cannot read the types of local variables.");
+    if (!hasImmediate) {
+        if (static_cast<uint8_t>(variableTypes) & static_cast<uint8_t>(WASMVariableTypes::I32))
+            READ_COMPACT_UINT32_OR_FAIL(m_numberOfI32LocalVariables, "Cannot read the number of int32 local variables.");
+        if (static_cast<uint8_t>(variableTypes) & static_cast<uint8_t>(WASMVariableTypes::F32))
+            READ_COMPACT_UINT32_OR_FAIL(m_numberOfF32LocalVariables, "Cannot read the number of float32 local variables.");
+        if (static_cast<uint8_t>(variableTypes) & static_cast<uint8_t>(WASMVariableTypes::F64))
+            READ_COMPACT_UINT32_OR_FAIL(m_numberOfF64LocalVariables, "Cannot read the number of float64 local variables.");
+    } else
+        m_numberOfI32LocalVariables = immediate;
+    return true;
+}
+
+template <class Context>
+ContextStatement WASMFunctionParser::parseStatement(Context& context)
+{
+    bool hasImmediate;
+    WASMOpStatement op;
+    WASMOpStatementWithImmediate opWithImmediate;
+    uint8_t immediate;
+    READ_OP_STATEMENT_OR_FAIL(hasImmediate, op, opWithImmediate, immediate, "Cannot read the statement opcode.");
+    if (!hasImmediate) {
+        switch (op) {
+        case WASMOpStatement::SetLocal:
+            parseSetLocalStatement(context);
+            break;
+        case WASMOpStatement::Return:
+            parseReturnStatement(context);
+            break;
+        case WASMOpStatement::Block:
+            parseBlockStatement(context);
+            break;
+        case WASMOpStatement::SetGlobal:
+        case WASMOpStatement::If:
+        case WASMOpStatement::IfElse:
+        case WASMOpStatement::While:
+        case WASMOpStatement::Do:
+        case WASMOpStatement::Label:
+        case WASMOpStatement::Break:
+        case WASMOpStatement::BreakLabel:
+        case WASMOpStatement::Continue:
+        case WASMOpStatement::ContinueLabel:
+        case WASMOpStatement::Switch:
+        case WASMOpStatement::I32Store8:
+        case WASMOpStatement::I32StoreWithOffset8:
+        case WASMOpStatement::I32Store16:
+        case WASMOpStatement::I32StoreWithOffset16:
+        case WASMOpStatement::I32Store32:
+        case WASMOpStatement::I32StoreWithOffset32:
+        case WASMOpStatement::F32Store:
+        case WASMOpStatement::F32StoreWithOffset:
+        case WASMOpStatement::F64Store:
+        case WASMOpStatement::F64StoreWithOffset:
+        case WASMOpStatement::CallInternal:
+        case WASMOpStatement::CallIndirect:
+        case WASMOpStatement::CallImport:
+            // FIXME: Implement these instructions.
+            FAIL_WITH_MESSAGE("Unsupported instruction.");
+        default:
+            ASSERT_NOT_REACHED();
+        }
+    } else {
+        switch (opWithImmediate) {
+        case WASMOpStatementWithImmediate::SetLocal:
+            parseSetLocalStatement(context, immediate);
+            break;
+        case WASMOpStatementWithImmediate::SetGlobal:
+            // FIXME: Implement this instruction.
+            FAIL_WITH_MESSAGE("Unsupported instruction.");
+        default:
+            ASSERT_NOT_REACHED();
+        }
+    }
+    return UNUSED;
+}
+
+template <class Context>
+ContextStatement WASMFunctionParser::parseSetLocalStatement(Context& context, uint32_t localIndex)
+{
+    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.
+    return UNUSED;
+}
+
+template <class Context>
+ContextStatement WASMFunctionParser::parseSetLocalStatement(Context& context)
+{
+    uint32_t localIndex;
+    READ_COMPACT_UINT32_OR_FAIL(localIndex, "Cannot read the local index.");
+    return parseSetLocalStatement(context, localIndex);
+}
+
+template <class Context>
+ContextStatement WASMFunctionParser::parseReturnStatement(Context& context)
+{
+    if (m_returnType != WASMExpressionType::Void)
+        parseExpression(context, m_returnType);
+    // FIXME: Implement this instruction.
+    return UNUSED;
+}
+
+template <class Context>
+ContextStatement WASMFunctionParser::parseBlockStatement(Context& context)
+{
+    uint32_t numberOfStatements;
+    READ_COMPACT_UINT32_OR_FAIL(numberOfStatements, "Cannot read the number of statements.");
+    for (uint32_t i = 0; i < numberOfStatements; ++i) {
+        parseStatement(context);
+        PROPAGATE_ERROR();
+    }
+    return UNUSED;
+}
+
+template <class Context>
+ContextExpression WASMFunctionParser::parseExpression(Context& context, WASMExpressionType expressionType)
+{
+    switch (expressionType) {
+    case WASMExpressionType::I32:
+        return parseExpressionI32(context);
+    case WASMExpressionType::F32:
+    case WASMExpressionType::F64:
+    case WASMExpressionType::Void:
+        // FIXME: Implement these instructions.
+        FAIL_WITH_MESSAGE("Unsupported instruction.");
+    default:
+        ASSERT_NOT_REACHED();
+    }
+    return 0;
+}
+
+template <class Context>
+ContextExpression WASMFunctionParser::parseExpressionI32(Context& context)
+{
+    bool hasImmediate;
+    WASMOpExpressionI32 op;
+    WASMOpExpressionI32WithImmediate opWithImmediate;
+    uint8_t immediate;
+    READ_OP_EXPRESSION_I32_OR_FAIL(hasImmediate, op, opWithImmediate, immediate, "Cannot read the int32 _expression_ opcode.");
+    if (!hasImmediate) {
+        switch (op) {
+        case WASMOpExpressionI32::Immediate:
+            return parseImmediateExpressionI32(context);
+        case WASMOpExpressionI32::ConstantPoolIndex:
+        case WASMOpExpressionI32::GetLocal:
+        case WASMOpExpressionI32::GetGlobal:
+        case WASMOpExpressionI32::SetLocal:
+        case WASMOpExpressionI32::SetGlobal:
+        case WASMOpExpressionI32::SLoad8:
+        case WASMOpExpressionI32::SLoadWithOffset8:
+        case WASMOpExpressionI32::ULoad8:
+        case WASMOpExpressionI32::ULoadWithOffset8:
+        case WASMOpExpressionI32::SLoad16:
+        case WASMOpExpressionI32::SLoadWithOffset16:
+        case WASMOpExpressionI32::ULoad16:
+        case WASMOpExpressionI32::ULoadWithOffset16:
+        case WASMOpExpressionI32::Load32:
+        case WASMOpExpressionI32::LoadWithOffset32:
+        case WASMOpExpressionI32::Store8:
+        case WASMOpExpressionI32::StoreWithOffset8:
+        case WASMOpExpressionI32::Store16:
+        case WASMOpExpressionI32::StoreWithOffset16:
+        case WASMOpExpressionI32::Store32:
+        case WASMOpExpressionI32::StoreWithOffset32:
+        case WASMOpExpressionI32::CallInternal:
+        case WASMOpExpressionI32::CallIndirect:
+        case WASMOpExpressionI32::CallImport:
+        case WASMOpExpressionI32::Conditional:
+        case WASMOpExpressionI32::Comma:
+        case WASMOpExpressionI32::FromF32:
+        case WASMOpExpressionI32::FromF64:
+        case WASMOpExpressionI32::Negate:
+        case WASMOpExpressionI32::Add:
+        case WASMOpExpressionI32::Sub:
+        case WASMOpExpressionI32::Mul:
+        case WASMOpExpressionI32::SDiv:
+        case WASMOpExpressionI32::UDiv:
+        case WASMOpExpressionI32::SMod:
+        case WASMOpExpressionI32::UMod:
+        case WASMOpExpressionI32::BitNot:
+        case WASMOpExpressionI32::BitOr:
+        case WASMOpExpressionI32::BitAnd:
+        case WASMOpExpressionI32::BitXor:
+        case WASMOpExpressionI32::LeftShift:
+        case WASMOpExpressionI32::ArithmeticRightShift:
+        case WASMOpExpressionI32::LogicalRightShift:
+        case WASMOpExpressionI32::CountLeadingZeros:
+        case WASMOpExpressionI32::LogicalNot:
+        case WASMOpExpressionI32::EqualI32:
+        case WASMOpExpressionI32::EqualF32:
+        case WASMOpExpressionI32::EqualF64:
+        case WASMOpExpressionI32::NotEqualI32:
+        case WASMOpExpressionI32::NotEqualF32:
+        case WASMOpExpressionI32::NotEqualF64:
+        case WASMOpExpressionI32::SLessThanI32:
+        case WASMOpExpressionI32::ULessThanI32:
+        case WASMOpExpressionI32::LessThanF32:
+        case WASMOpExpressionI32::LessThanF64:
+        case WASMOpExpressionI32::SLessThanOrEqualI32:
+        case WASMOpExpressionI32::ULessThanOrEqualI32:
+        case WASMOpExpressionI32::LessThanOrEqualF32:
+        case WASMOpExpressionI32::LessThanOrEqualF64:
+        case WASMOpExpressionI32::SGreaterThanI32:
+        case WASMOpExpressionI32::UGreaterThanI32:
+        case WASMOpExpressionI32::GreaterThanF32:
+        case WASMOpExpressionI32::GreaterThanF64:
+        case WASMOpExpressionI32::SGreaterThanOrEqualI32:
+        case WASMOpExpressionI32::UGreaterThanOrEqualI32:
+        case WASMOpExpressionI32::GreaterThanOrEqualF32:
+        case WASMOpExpressionI32::GreaterThanOrEqualF64:
+        case WASMOpExpressionI32::SMin:
+        case WASMOpExpressionI32::UMin:
+        case WASMOpExpressionI32::SMax:
+        case WASMOpExpressionI32::UMax:
+        case WASMOpExpressionI32::Abs:
+            // FIXME: Implement these instructions.
+            FAIL_WITH_MESSAGE("Unsupported instruction.");
+        default:
+            ASSERT_NOT_REACHED();
+        }
+    } else {
+        switch (opWithImmediate) {
+        case WASMOpExpressionI32WithImmediate::Immediate:
+            return parseImmediateExpressionI32(context, immediate);
+        case WASMOpExpressionI32WithImmediate::ConstantPoolIndex:
+        case WASMOpExpressionI32WithImmediate::GetLocal:
+            // FIXME: Implement these instructions.
+            FAIL_WITH_MESSAGE("Unsupported instruction.");
+        default:
+            ASSERT_NOT_REACHED();
+        }
+    }
+    return 0;
+}
+
+template <class Context>
+ContextExpression WASMFunctionParser::parseImmediateExpressionI32(Context&, uint32_t)
+{
+    // FIXME: Implement this instruction.
+    return UNUSED;
+}
+
+template <class Context>
+ContextExpression WASMFunctionParser::parseImmediateExpressionI32(Context& context)
+{
+    uint32_t immediate;
+    READ_COMPACT_UINT32_OR_FAIL(immediate, "Cannot read the immediate.");
+    return parseImmediateExpressionI32(context, immediate);
+}
+
+} // namespace JSC
+
+#endif // ENABLE(WEBASSEMBLY)

Added: trunk/Source/_javascript_Core/wasm/WASMFunctionParser.h (0 => 188778)


--- trunk/Source/_javascript_Core/wasm/WASMFunctionParser.h	                        (rev 0)
+++ trunk/Source/_javascript_Core/wasm/WASMFunctionParser.h	2015-08-21 21:46:22 UTC (rev 188778)
@@ -0,0 +1,84 @@
+/*
+ * Copyright (C) 2015 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef WASMFunctionParser_h
+#define WASMFunctionParser_h
+
+#if ENABLE(WEBASSEMBLY)
+
+#include "SourceCode.h"
+#include "WASMReader.h"
+
+#define ContextExpression typename Context::_expression_
+#define ContextStatement typename Context::Statement
+
+namespace JSC {
+
+class JSWASMModule;
+
+class WASMFunctionParser {
+public:
+    static bool checkSyntax(JSWASMModule*, const SourceCode&, size_t functionIndex, unsigned startOffsetInSource, unsigned& endOffsetInSource, String& errorMessage);
+
+private:
+    WASMFunctionParser(JSWASMModule* module, const SourceCode& source, size_t functionIndex)
+        : m_module(module)
+        , m_reader(static_cast<WebAssemblySourceProvider*>(source.provider())->data())
+        , m_functionIndex(functionIndex)
+    {
+    }
+
+    template <class Context> bool parseFunction(Context&);
+    bool parseLocalVariables();
+
+    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 parseReturnStatement(Context&);
+    template <class Context> ContextStatement parseBlockStatement(Context&);
+
+    template <class Context> ContextExpression parseExpression(Context&, WASMExpressionType);
+
+    template <class Context> ContextExpression parseExpressionI32(Context&);
+    template <class Context> ContextExpression parseImmediateExpressionI32(Context&, uint32_t immediate);
+    template <class Context> ContextExpression parseImmediateExpressionI32(Context&);
+
+    JSWASMModule* m_module;
+    WASMReader m_reader;
+    size_t m_functionIndex;
+    String m_errorMessage;
+
+    WASMExpressionType m_returnType;
+    Vector<WASMType> m_localTypes;
+    uint32_t m_numberOfI32LocalVariables;
+    uint32_t m_numberOfF32LocalVariables;
+    uint32_t m_numberOfF64LocalVariables;
+};
+
+} // namespace JSC
+
+#endif // ENABLE(WEBASSEMBLY)
+
+#endif // WASMFunctionParser_h

Copied: trunk/Source/_javascript_Core/wasm/WASMFunctionSyntaxChecker.h (from rev 188776, trunk/Source/_javascript_Core/wasm/WASMMagicNumber.h) (0 => 188778)


--- trunk/Source/_javascript_Core/wasm/WASMFunctionSyntaxChecker.h	                        (rev 0)
+++ trunk/Source/_javascript_Core/wasm/WASMFunctionSyntaxChecker.h	2015-08-21 21:46:22 UTC (rev 188778)
@@ -0,0 +1,43 @@
+/*
+ * Copyright (C) 2015 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef WASMFunctionSyntaxChecker_h
+#define WASMFunctionSyntaxChecker_h
+
+#if ENABLE(WEBASSEMBLY)
+
+namespace JSC {
+
+class WASMFunctionSyntaxChecker {
+public:
+    typedef int _expression_;
+    typedef int Statement;
+};
+
+} // namespace JSC
+
+#endif // ENABLE(WEBASSEMBLY)
+
+#endif // WASMFunctionSyntaxChecker_h

Deleted: trunk/Source/_javascript_Core/wasm/WASMMagicNumber.h (188777 => 188778)


--- trunk/Source/_javascript_Core/wasm/WASMMagicNumber.h	2015-08-21 21:44:34 UTC (rev 188777)
+++ trunk/Source/_javascript_Core/wasm/WASMMagicNumber.h	2015-08-21 21:46:22 UTC (rev 188778)
@@ -1,39 +0,0 @@
-/*
- * Copyright (C) 2015 Apple Inc. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
- * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
- * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
- * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
- * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#ifndef WASMMagicNumber_h
-#define WASMMagicNumber_h
-
-#if ENABLE(WEBASSEMBLY)
-
-namespace JSC {
-
-static const uint32_t wasmMagicNumber = 0x6d736177;
-
-} // namespace JSC
-
-#endif // ENABLE(WEBASSEMBLY)
-
-#endif // WASMMagicNumber_h

Modified: trunk/Source/_javascript_Core/wasm/WASMModuleParser.cpp (188777 => 188778)


--- trunk/Source/_javascript_Core/wasm/WASMModuleParser.cpp	2015-08-21 21:44:34 UTC (rev 188777)
+++ trunk/Source/_javascript_Core/wasm/WASMModuleParser.cpp	2015-08-21 21:46:22 UTC (rev 188778)
@@ -31,7 +31,8 @@
 #include "JSCInlines.h"
 #include "JSWASMModule.h"
 #include "StrongInlines.h"
-#include "WASMMagicNumber.h"
+#include "WASMConstants.h"
+#include "WASMFunctionParser.h"
 #include <wtf/MathExtras.h>
 
 #define FAIL_WITH_MESSAGE(errorMessage) do { m_errorMessage = errorMessage; return; } while (0)
@@ -49,7 +50,8 @@
 namespace JSC {
 
 WASMModuleParser::WASMModuleParser(const SourceCode& source)
-    : m_reader(static_cast<WebAssemblySourceProvider*>(source.provider())->data())
+    : m_source(source)
+    , m_reader(static_cast<WebAssemblySourceProvider*>(source.provider())->data())
 {
 }
 
@@ -247,25 +249,22 @@
 
 void WASMModuleParser::parseFunctionDefinitionSection()
 {
-    for (size_t i = 0; i < m_module->functionDeclarations().size(); ++i) {
-        parseFunctionDefinition();
+    for (size_t functionIndex = 0; functionIndex < m_module->functionDeclarations().size(); ++functionIndex) {
+        parseFunctionDefinition(functionIndex);
         PROPAGATE_ERROR();
     }
 }
 
-void WASMModuleParser::parseFunctionDefinition()
+void WASMModuleParser::parseFunctionDefinition(size_t functionIndex)
 {
-    // FIXME: Support any functions. https://bugs.webkit.org/show_bug.cgi?id=147738
-    // Currently, we only support functions that have "return 0;" as their only statement.
-    // These functions consist of exactly 4 bytes, i.e.
-    // 1. The number of local variables (0) [0x80]
-    // 2. The number of statements (1) [0x01]
-    // 3. The return statement [0x0f]
-    // 4. The immediate _expression_ (0) [0xa0]
-    uint32_t functionDefinitionBytes;
-    READ_UINT32_OR_FAIL(functionDefinitionBytes, "Cannot read the function definition.");
-    FAIL_IF_FALSE(functionDefinitionBytes == 0xa00f0180, "Only functions that have \"return 0;\" "
-        "as their only statement are supported at the moment.");
+    unsigned startOffsetInSource = m_reader.offset();
+    unsigned endOffsetInSource;
+    String errorMessage;
+    if (!WASMFunctionParser::checkSyntax(m_module.get(), m_source, functionIndex, startOffsetInSource, endOffsetInSource, errorMessage)) {
+        m_errorMessage = errorMessage;
+        return;
+    }
+    m_reader.setOffset(endOffsetInSource);
 }
 
 void WASMModuleParser::parseExportSection()

Modified: trunk/Source/_javascript_Core/wasm/WASMModuleParser.h (188777 => 188778)


--- trunk/Source/_javascript_Core/wasm/WASMModuleParser.h	2015-08-21 21:44:34 UTC (rev 188777)
+++ trunk/Source/_javascript_Core/wasm/WASMModuleParser.h	2015-08-21 21:46:22 UTC (rev 188778)
@@ -54,9 +54,10 @@
     void parseFunctionDeclarationSection();
     void parseFunctionPointerTableSection();
     void parseFunctionDefinitionSection();
-    void parseFunctionDefinition();
+    void parseFunctionDefinition(size_t functionIndex);
     void parseExportSection();
 
+    const SourceCode& m_source;
     WASMReader m_reader;
     Strong<JSWASMModule> m_module;
     String m_errorMessage;

Modified: trunk/Source/_javascript_Core/wasm/WASMReader.cpp (188777 => 188778)


--- trunk/Source/_javascript_Core/wasm/WASMReader.cpp	2015-08-21 21:44:34 UTC (rev 188777)
+++ trunk/Source/_javascript_Core/wasm/WASMReader.cpp	2015-08-21 21:46:22 UTC (rev 188778)
@@ -21,6 +21,23 @@
  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * =========================================================================
+ *
+ * Copyright (c) 2015 by the repository authors of
+ * WebAssembly/polyfill-prototype-1.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
  */
 
 #include "config.h"
@@ -113,17 +130,17 @@
 
 bool WASMReader::readType(WASMType& result)
 {
-    return readByte<WASMType>(result, (uint8_t)WASMType::NumberOfTypes);
+    return readByte<WASMType>(result, static_cast<uint8_t>(WASMType::NumberOfTypes));
 }
 
 bool WASMReader::readExpressionType(WASMExpressionType& result)
 {
-    return readByte<WASMExpressionType>(result, (uint8_t)WASMExpressionType::NumberOfExpressionTypes);
+    return readByte<WASMExpressionType>(result, static_cast<uint8_t>(WASMExpressionType::NumberOfExpressionTypes));
 }
 
 bool WASMReader::readExportFormat(WASMExportFormat& result)
 {
-    return readByte<WASMExportFormat>(result, (uint8_t)WASMExportFormat::NumberOfExportFormats);
+    return readByte<WASMExportFormat>(result, static_cast<uint8_t>(WASMExportFormat::NumberOfExportFormats));
 }
 
 template <class T> bool WASMReader::readByte(T& result, uint8_t numberOfValues)
@@ -136,6 +153,50 @@
     return true;
 }
 
+bool WASMReader::readOpStatement(bool& hasImmediate, WASMOpStatement& op, WASMOpStatementWithImmediate& opWithImmediate, uint8_t& immediate)
+{
+    return readOp(hasImmediate, op, opWithImmediate, immediate,
+        static_cast<uint8_t>(WASMOpStatement::NumberOfWASMOpStatements),
+        static_cast<uint8_t>(WASMOpStatementWithImmediate::NumberOfWASMOpStatementWithImmediates));
+}
+
+bool WASMReader::readOpExpressionI32(bool& hasImmediate, WASMOpExpressionI32& op, WASMOpExpressionI32WithImmediate& opWithImmediate, uint8_t& immediate)
+{
+    return readOp(hasImmediate, op, opWithImmediate, immediate,
+        static_cast<uint8_t>(WASMOpExpressionI32::NumberOfWASMOpExpressionI32s),
+        static_cast<uint8_t>(WASMOpExpressionI32WithImmediate::NumberOfWASMOpExpressionI32WithImmediates));
+}
+
+bool WASMReader::readVariableTypes(bool& hasImmediate, WASMVariableTypes& variableTypes, WASMVariableTypesWithImmediate& variableTypesWithImmediate, uint8_t& immediate)
+{
+    return readOp(hasImmediate, variableTypes, variableTypesWithImmediate, immediate,
+        static_cast<uint8_t>(WASMVariableTypes::NumberOfVariableTypes),
+        static_cast<uint8_t>(WASMVariableTypesWithImmediate::NumberOfVariableTypesWithImmediates));
+}
+
+template <class T, class TWithImmediate>
+bool WASMReader::readOp(bool& hasImmediate, T& op, TWithImmediate& opWithImmediate, uint8_t& immediate, uint8_t numberOfValues, uint8_t numberOfValuesWithImmediate)
+{
+    CHECK_READ(1);
+    uint8_t byte = *m_cursor++;
+
+    if (!(byte & hasImmediateInOpFlag)) {
+        if (byte >= numberOfValues)
+            return false;
+        hasImmediate = false;
+        op = T(byte);
+        return true;
+    }
+
+    uint8_t byteWithoutImmediate = (byte >> immediateBits) & (opWithImmediateLimit - 1);
+    if (byteWithoutImmediate >= numberOfValuesWithImmediate)
+        return false;
+    hasImmediate = true;
+    opWithImmediate = TWithImmediate(byteWithoutImmediate);
+    immediate = byte & (immediateLimit - 1);
+    return true;
+}
+
 } // namespace JSC
 
 #endif // ENABLE(WEBASSEMBLY)

Modified: trunk/Source/_javascript_Core/wasm/WASMReader.h (188777 => 188778)


--- trunk/Source/_javascript_Core/wasm/WASMReader.h	2015-08-21 21:44:34 UTC (rev 188777)
+++ trunk/Source/_javascript_Core/wasm/WASMReader.h	2015-08-21 21:46:22 UTC (rev 188778)
@@ -28,6 +28,7 @@
 
 #if ENABLE(WEBASSEMBLY)
 
+#include "WASMConstants.h"
 #include "WASMFormat.h"
 #include <wtf/Vector.h>
 
@@ -41,6 +42,9 @@
     {
     }
 
+    unsigned offset() const { return m_cursor - m_buffer.data(); }
+    void setOffset(unsigned offset) { m_cursor = m_buffer.data() + offset; }
+
     bool readUInt32(uint32_t& result);
     bool readFloat(float& result);
     bool readDouble(double& result);
@@ -49,11 +53,15 @@
     bool readType(WASMType& result);
     bool readExpressionType(WASMExpressionType& result);
     bool readExportFormat(WASMExportFormat& result);
+    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);
 
 private:
     static const uint32_t firstSevenBitsMask = 0x7f;
 
     template <class T> bool readByte(T& result, uint8_t numberOfValues);
+    template <class T, class TWithImmediate> bool readOp(bool& hasImmediate, T&, TWithImmediate&, uint8_t& immediate, uint8_t numberOfValues, uint8_t numberOfValuesWithImmediate);
 
     const Vector<uint8_t>& m_buffer;
     const uint8_t* m_cursor;
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to