Title: [209083] trunk
Revision
209083
Author
[email protected]
Date
2016-11-29 11:37:00 -0800 (Tue, 29 Nov 2016)

Log Message

Add simple way to implement Wasm ops that require more than one B3 opcode
https://bugs.webkit.org/show_bug.cgi?id=165129

Reviewed by Geoffrey Garen.

JSTests:

* wasm/function-tests/eqz.js: Added.
* wasm/function-tests/max.js: Added.
* wasm/function-tests/min.js: Added.
* wasm/wasm.json:

Source/_javascript_Core:

This patch adds a simple way to show the B3IRGenerator opcode script how
to generate code for Wasm opcodes that do not have a one to one mapping.
The syntax is pretty simple right now. There are only three things one
can use as of this patch (although more things might be added in the future)
1) Wasm opcode arguments: These are referred to as @<argument_number>. For example,
   I32.sub would map to Sub(@0, @1).
2) 32-bit int constants: These are reffered to as i32(<value>). For example, i32.inc
   would map to Add(@0, i32(1))
3) B3 opcodes: These are referred to as the B3 opcode name followed by the B3Value's constructor
   arguments. A value may take the result of another value as an argument. For example, you can do
   Div(Mul(@0, Add(@0, i32(1))), i32(2)) if there was a b3 opcode that computed the sum from 1 to n.

These scripts are used to implement Wasm's eqz and floating point max/min opcodes. This patch
also adds missing support for the Wasm Neg opcodes.

* jsc.cpp:
(box):
(functionTestWasmModuleFunctions):
* wasm/WasmB3IRGenerator.cpp:
(JSC::Wasm::toB3Op): Deleted.
* wasm/WasmFunctionParser.h:
(JSC::Wasm::FunctionParser<Context>::parseBody):
* wasm/WasmModuleParser.cpp:
(JSC::Wasm::ModuleParser::parseType):
* wasm/WasmParser.h:
(JSC::Wasm::Parser::parseUInt8):
(JSC::Wasm::Parser::parseValueType):
* wasm/generateWasmB3IRGeneratorInlinesHeader.py:
(Source):
(Source.__init__):
(read):
(lex):
(CodeGenerator):
(CodeGenerator.__init__):
(CodeGenerator.advance):
(CodeGenerator.token):
(CodeGenerator.parseError):
(CodeGenerator.consume):
(CodeGenerator.generateParameters):
(CodeGenerator.generateOpcode):
(CodeGenerator.generate):
(temp):
(generateB3OpCode):
(generateI32ConstCode):
(generateB3Code):
(generateSimpleCode):
* wasm/wasm.json:

Modified Paths

Added Paths

Diff

Modified: trunk/JSTests/ChangeLog (209082 => 209083)


--- trunk/JSTests/ChangeLog	2016-11-29 19:20:08 UTC (rev 209082)
+++ trunk/JSTests/ChangeLog	2016-11-29 19:37:00 UTC (rev 209083)
@@ -1,3 +1,15 @@
+2016-11-29  Keith Miller  <[email protected]>
+
+        Add simple way to implement Wasm ops that require more than one B3 opcode
+        https://bugs.webkit.org/show_bug.cgi?id=165129
+
+        Reviewed by Geoffrey Garen.
+
+        * wasm/function-tests/eqz.js: Added.
+        * wasm/function-tests/max.js: Added.
+        * wasm/function-tests/min.js: Added.
+        * wasm/wasm.json:
+
 2016-11-21  Yusuke Suzuki  <[email protected]>
 
         Crash in com.apple._javascript_Core: WTF::ThreadSpecific<WTF::WTFThreadData, + 142

Added: trunk/JSTests/wasm/function-tests/eqz.js (0 => 209083)


--- trunk/JSTests/wasm/function-tests/eqz.js	                        (rev 0)
+++ trunk/JSTests/wasm/function-tests/eqz.js	2016-11-29 19:37:00 UTC (rev 209083)
@@ -0,0 +1,21 @@
+import Builder from '../Builder.js'
+
+const b = new Builder();
+b.setChecked(false);
+b.Type().End()
+    .Function().End()
+    .Memory().InitialMaxPages(1, 1).End()
+    .Code()
+    .Function({ params: ["i32"], ret: "i32" }, [])
+    .GetLocal(0)
+    .I32Eqz()
+    .End()
+
+
+const bin = b.WebAssembly()
+bin.trim();
+testWasmModuleFunctions(bin.get(), 1,
+                        [[{type: "i32", value: 0 }, [{ type: "i32", value: 1 }]],
+                         [{type: "i32", value: 1 }, [{ type: "i32", value: 0 }]]
+                        ]
+                       );

Added: trunk/JSTests/wasm/function-tests/max.js (0 => 209083)


--- trunk/JSTests/wasm/function-tests/max.js	                        (rev 0)
+++ trunk/JSTests/wasm/function-tests/max.js	2016-11-29 19:37:00 UTC (rev 209083)
@@ -0,0 +1,34 @@
+import Builder from '../Builder.js'
+
+const b = new Builder();
+b.setChecked(false);
+b.Type().End()
+    .Function().End()
+    .Memory().InitialMaxPages(1, 1).End()
+    .Code()
+    .Function({ params: ["f32", "f32"], ret: "f32" }, [])
+    .GetLocal(0)
+    .GetLocal(1)
+    .F32Max()
+    .End()
+
+    .Function({ params: ["f64", "f64"], ret: "f64" }, [])
+    .GetLocal(0)
+    .GetLocal(1)
+    .F64Max()
+    .End()
+
+
+
+const bin = b.WebAssembly()
+bin.trim();
+testWasmModuleFunctions(bin.get(), 2,
+                        [[{type: "f32", value: 100 }, [{ type: "f32", value: 100 }, { type: "f32", value: 1.5 }]],
+                         [{type: "f32", value: 100 }, [{ type: "f32", value: 100 }, { type: "f32", value: -1.5 }]],
+                         [{type: "f32", value: -1.5 }, [{ type: "f32", value: -100 }, { type: "f32", value: -1.5 }]]
+                        ],
+                        [[{type: "f64", value: 100 }, [{ type: "f64", value: 100 }, { type: "f64", value: 1.5 }]],
+                         [{type: "f64", value: 100 }, [{ type: "f64", value: 100 }, { type: "f64", value: -1.5 }]],
+                         [{type: "f64", value: -1.5 }, [{ type: "f64", value: -100 }, { type: "f64", value: -1.5 }]]
+                        ]
+                       );

Added: trunk/JSTests/wasm/function-tests/min.js (0 => 209083)


--- trunk/JSTests/wasm/function-tests/min.js	                        (rev 0)
+++ trunk/JSTests/wasm/function-tests/min.js	2016-11-29 19:37:00 UTC (rev 209083)
@@ -0,0 +1,34 @@
+import Builder from '../Builder.js'
+
+const b = new Builder();
+b.setChecked(false);
+b.Type().End()
+    .Function().End()
+    .Memory().InitialMaxPages(1, 1).End()
+    .Code()
+    .Function({ params: ["f32", "f32"], ret: "f32" }, [])
+    .GetLocal(0)
+    .GetLocal(1)
+    .F32Min()
+    .End()
+
+    .Function({ params: ["f64", "f64"], ret: "f64" }, [])
+    .GetLocal(0)
+    .GetLocal(1)
+    .F64Min()
+    .End()
+
+
+
+const bin = b.WebAssembly()
+bin.trim();
+testWasmModuleFunctions(bin.get(), 2,
+                        [[{type: "f32", value: 1.5 }, [{ type: "f32", value: 100 }, { type: "f32", value: 1.5 }]],
+                         [{type: "f32", value: -1.5 }, [{ type: "f32", value: 100 }, { type: "f32", value: -1.5 }]],
+                         [{type: "f32", value: -100 }, [{ type: "f32", value: -100 }, { type: "f32", value: 1.5 }]]
+                        ],
+                        [[{type: "f64", value: 1.5 }, [{ type: "f64", value: 100 }, { type: "f64", value: 1.5 }]],
+                         [{type: "f64", value: -1.5 }, [{ type: "f64", value: 100 }, { type: "f64", value: -1.5 }]],
+                         [{type: "f64", value: -100 }, [{ type: "f64", value: -100 }, { type: "f64", value: 1.5 }]]
+                        ]
+                       );

Modified: trunk/JSTests/wasm/wasm.json (209082 => 209083)


--- trunk/JSTests/wasm/wasm.json	2016-11-29 19:20:08 UTC (rev 209082)
+++ trunk/JSTests/wasm/wasm.json	2016-11-29 19:37:00 UTC (rev 209083)
@@ -120,7 +120,7 @@
         "i32.clz":             { "category": "arithmetic", "value":  87, "return": ["i32"],      "parameter": ["i32"],                  "immediate": [], "b3op": "Clz"          },
         "i32.ctz":             { "category": "arithmetic", "value":  88, "return": ["i32"],      "parameter": ["i32"],                  "immediate": []                         },
         "i32.popcnt":          { "category": "arithmetic", "value":  89, "return": ["i32"],      "parameter": ["i32"],                  "immediate": []                         },
-        "i32.eqz":             { "category": "comparison", "value":  90, "return": ["bool"],     "parameter": ["i32"],                  "immediate": []                         },
+        "i32.eqz":             { "category": "comparison", "value":  90, "return": ["bool"],     "parameter": ["i32"],                  "immediate": [], "b3op": "Equal(i32(0), @0)" },
         "i64.add":             { "category": "arithmetic", "value":  91, "return": ["i64"],      "parameter": ["i64", "i64"],           "immediate": [], "b3op": "Add"          },
         "i64.sub":             { "category": "arithmetic", "value":  92, "return": ["i64"],      "parameter": ["i64", "i64"],           "immediate": [], "b3op": "Sub"          },
         "i64.mul":             { "category": "arithmetic", "value":  93, "return": ["i64"],      "parameter": ["i64", "i64"],           "immediate": [], "b3op": "Mul"          },
@@ -149,15 +149,15 @@
         "i64.clz":             { "category": "arithmetic", "value": 114, "return": ["i64"],      "parameter": ["i64"],                  "immediate": [], "b3op": "Clz"          },
         "i64.ctz":             { "category": "arithmetic", "value": 115, "return": ["i64"],      "parameter": ["i64"],                  "immediate": []                         },
         "i64.popcnt":          { "category": "arithmetic", "value": 116, "return": ["i64"],      "parameter": ["i64"],                  "immediate": []                         },
-        "i64.eqz":             { "category": "comparison", "value": 186, "return": ["bool"],     "parameter": ["i64"],                  "immediate": []                         },
+        "i64.eqz":             { "category": "comparison", "value": 186, "return": ["bool"],     "parameter": ["i64"],                  "immediate": [], "b3op": "Equal(i32(0), @0)" },
         "f32.add":             { "category": "arithmetic", "value": 117, "return": ["f32"],      "parameter": ["f32", "f32"],           "immediate": [], "b3op": "Add"          },
         "f32.sub":             { "category": "arithmetic", "value": 118, "return": ["f32"],      "parameter": ["f32", "f32"],           "immediate": [], "b3op": "Sub"          },
         "f32.mul":             { "category": "arithmetic", "value": 119, "return": ["f32"],      "parameter": ["f32", "f32"],           "immediate": [], "b3op": "Mul"          },
         "f32.div":             { "category": "arithmetic", "value": 120, "return": ["f32"],      "parameter": ["f32", "f32"],           "immediate": [], "b3op": "Div"          },
-        "f32.min":             { "category": "arithmetic", "value": 121, "return": ["f32"],      "parameter": ["f32", "f32"],           "immediate": []                         },
-        "f32.max":             { "category": "arithmetic", "value": 122, "return": ["f32"],      "parameter": ["f32", "f32"],           "immediate": []                         },
+        "f32.min":             { "category": "arithmetic", "value": 121, "return": ["f32"],      "parameter": ["f32", "f32"],           "immediate": [], "b3op": "Select(LessThan(@0, @1), @0, @1)" },
+        "f32.max":             { "category": "arithmetic", "value": 122, "return": ["f32"],      "parameter": ["f32", "f32"],           "immediate": [], "b3op": "Select(LessThan(@0, @1), @1, @0)" },
         "f32.abs":             { "category": "arithmetic", "value": 123, "return": ["f32"],      "parameter": ["f32"],                  "immediate": [], "b3op": "Abs"          },
-        "f32.neg":             { "category": "arithmetic", "value": 124, "return": ["f32"],      "parameter": ["f32"],                  "immediate": []                         },
+        "f32.neg":             { "category": "arithmetic", "value": 124, "return": ["f32"],      "parameter": ["f32"],                  "immediate": [], "b3op": "Neg"          },
         "f32.copysign":        { "category": "arithmetic", "value": 125, "return": ["f32"],      "parameter": ["f32"],                  "immediate": []                         },
         "f32.ceil":            { "category": "arithmetic", "value": 126, "return": ["f32"],      "parameter": ["f32"],                  "immediate": [], "b3op": "Ceil"         },
         "f32.floor":           { "category": "arithmetic", "value": 127, "return": ["f32"],      "parameter": ["f32"],                  "immediate": [], "b3op": "Floor"        },
@@ -174,10 +174,10 @@
         "f64.sub":             { "category": "arithmetic", "value": 138, "return": ["f64"],      "parameter": ["f64", "f64"],           "immediate": [], "b3op": "Sub"          },
         "f64.mul":             { "category": "arithmetic", "value": 139, "return": ["f64"],      "parameter": ["f64", "f64"],           "immediate": [], "b3op": "Mul"          },
         "f64.div":             { "category": "arithmetic", "value": 140, "return": ["f64"],      "parameter": ["f64", "f64"],           "immediate": [], "b3op": "Div"          },
-        "f64.min":             { "category": "arithmetic", "value": 141, "return": ["f64"],      "parameter": ["f64", "f64"],           "immediate": []                         },
-        "f64.max":             { "category": "arithmetic", "value": 142, "return": ["f64"],      "parameter": ["f64", "f64"],           "immediate": []                         },
+        "f64.min":             { "category": "arithmetic", "value": 141, "return": ["f64"],      "parameter": ["f64", "f64"],           "immediate": [], "b3op": "Select(LessThan(@0, @1), @0, @1)" },
+        "f64.max":             { "category": "arithmetic", "value": 142, "return": ["f64"],      "parameter": ["f64", "f64"],           "immediate": [], "b3op": "Select(LessThan(@0, @1), @1, @0)" },
         "f64.abs":             { "category": "arithmetic", "value": 143, "return": ["f64"],      "parameter": ["f64"],                  "immediate": [], "b3op": "Abs"          },
-        "f64.neg":             { "category": "arithmetic", "value": 144, "return": ["f64"],      "parameter": ["f64"],                  "immediate": []                         },
+        "f64.neg":             { "category": "arithmetic", "value": 144, "return": ["f64"],      "parameter": ["f64"],                  "immediate": [], "b3op": "Neg"          },
         "f64.copysign":        { "category": "arithmetic", "value": 145, "return": ["f64"],      "parameter": ["f64"],                  "immediate": []                         },
         "f64.ceil":            { "category": "arithmetic", "value": 146, "return": ["f64"],      "parameter": ["f64"],                  "immediate": [], "b3op": "Ceil"         },
         "f64.floor":           { "category": "arithmetic", "value": 147, "return": ["f64"],      "parameter": ["f64"],                  "immediate": [], "b3op": "Floor"        },

Modified: trunk/Source/_javascript_Core/ChangeLog (209082 => 209083)


--- trunk/Source/_javascript_Core/ChangeLog	2016-11-29 19:20:08 UTC (rev 209082)
+++ trunk/Source/_javascript_Core/ChangeLog	2016-11-29 19:37:00 UTC (rev 209083)
@@ -1,3 +1,58 @@
+2016-11-29  Keith Miller  <[email protected]>
+
+        Add simple way to implement Wasm ops that require more than one B3 opcode
+        https://bugs.webkit.org/show_bug.cgi?id=165129
+
+        Reviewed by Geoffrey Garen.
+
+        This patch adds a simple way to show the B3IRGenerator opcode script how
+        to generate code for Wasm opcodes that do not have a one to one mapping.
+        The syntax is pretty simple right now. There are only three things one
+        can use as of this patch (although more things might be added in the future)
+        1) Wasm opcode arguments: These are referred to as @<argument_number>. For example,
+           I32.sub would map to Sub(@0, @1).
+        2) 32-bit int constants: These are reffered to as i32(<value>). For example, i32.inc
+           would map to Add(@0, i32(1))
+        3) B3 opcodes: These are referred to as the B3 opcode name followed by the B3Value's constructor
+           arguments. A value may take the result of another value as an argument. For example, you can do
+           Div(Mul(@0, Add(@0, i32(1))), i32(2)) if there was a b3 opcode that computed the sum from 1 to n.
+
+        These scripts are used to implement Wasm's eqz and floating point max/min opcodes. This patch
+        also adds missing support for the Wasm Neg opcodes.
+
+        * jsc.cpp:
+        (box):
+        (functionTestWasmModuleFunctions):
+        * wasm/WasmB3IRGenerator.cpp:
+        (JSC::Wasm::toB3Op): Deleted.
+        * wasm/WasmFunctionParser.h:
+        (JSC::Wasm::FunctionParser<Context>::parseBody):
+        * wasm/WasmModuleParser.cpp:
+        (JSC::Wasm::ModuleParser::parseType):
+        * wasm/WasmParser.h:
+        (JSC::Wasm::Parser::parseUInt8):
+        (JSC::Wasm::Parser::parseValueType):
+        * wasm/generateWasmB3IRGeneratorInlinesHeader.py:
+        (Source):
+        (Source.__init__):
+        (read):
+        (lex):
+        (CodeGenerator):
+        (CodeGenerator.__init__):
+        (CodeGenerator.advance):
+        (CodeGenerator.token):
+        (CodeGenerator.parseError):
+        (CodeGenerator.consume):
+        (CodeGenerator.generateParameters):
+        (CodeGenerator.generateOpcode):
+        (CodeGenerator.generate):
+        (temp):
+        (generateB3OpCode):
+        (generateI32ConstCode):
+        (generateB3Code):
+        (generateSimpleCode):
+        * wasm/wasm.json:
+
 2016-11-29  Mark Lam  <[email protected]>
 
         Fix exception scope verification failures in ProxyConstructor.cpp and ProxyObject.cpp.

Modified: trunk/Source/_javascript_Core/jsc.cpp (209082 => 209083)


--- trunk/Source/_javascript_Core/jsc.cpp	2016-11-29 19:20:08 UTC (rev 209082)
+++ trunk/Source/_javascript_Core/jsc.cpp	2016-11-29 19:37:00 UTC (rev 209083)
@@ -2509,7 +2509,7 @@
         return JSValue::decode(bitwise_cast<uint32_t>(value.toFloat(exec)));
 
     RELEASE_ASSERT(typeString == "f64");
-    return value;
+    return JSValue::decode(bitwise_cast<uint64_t>(value.asNumber()));
 }
 
 static JSValue callWasmFunction(VM* vm, const B3::Compilation& code, Vector<JSValue>& boxedArgs)
@@ -2548,8 +2548,10 @@
 
     Wasm::Plan plan(&vm, static_cast<uint8_t*>(source->vector()), source->length());
     plan.run();
-    if (plan.failed())
+    if (plan.failed()) {
+        dataLogLn("failed to parse module: ", plan.errorMessage());
         CRASH();
+    }
 
     if (plan.compiledFunctionCount() != functionCount)
         CRASH();

Modified: trunk/Source/_javascript_Core/wasm/WasmB3IRGenerator.cpp (209082 => 209083)


--- trunk/Source/_javascript_Core/wasm/WasmB3IRGenerator.cpp	2016-11-29 19:20:08 UTC (rev 209082)
+++ trunk/Source/_javascript_Core/wasm/WasmB3IRGenerator.cpp	2016-11-29 19:37:00 UTC (rev 209083)
@@ -59,26 +59,6 @@
 const bool verbose = false;
 }
 
-inline B3::Opcode toB3Op(BinaryOpType op)
-{
-    switch (op) {
-#define CREATE_CASE(name, op, b3op) case BinaryOpType::name: return b3op;
-    FOR_EACH_WASM_BINARY_OP(CREATE_CASE)
-#undef CREATE_CASE
-    }
-    RELEASE_ASSERT_NOT_REACHED();
-}
-
-inline B3::Opcode toB3Op(UnaryOpType op)
-{
-    switch (op) {
-#define CREATE_CASE(name, op, b3op) case UnaryOpType::name: return b3op;
-    FOR_EACH_WASM_UNARY_OP(CREATE_CASE)
-#undef CREATE_CASE
-    }
-    RELEASE_ASSERT_NOT_REACHED();
-}
-
 class B3IRGenerator {
 public:
     struct ControlData {

Modified: trunk/Source/_javascript_Core/wasm/WasmFunctionParser.h (209082 => 209083)


--- trunk/Source/_javascript_Core/wasm/WasmFunctionParser.h	2016-11-29 19:20:08 UTC (rev 209082)
+++ trunk/Source/_javascript_Core/wasm/WasmFunctionParser.h	2016-11-29 19:37:00 UTC (rev 209083)
@@ -123,7 +123,7 @@
 {
     while (true) {
         uint8_t op;
-        if (!parseUInt7(op) || !isValidOpType(op)) {
+        if (!parseUInt8(op) || !isValidOpType(op)) {
             if (verbose)
                 WTF::dataLogLn("attempted to decode invalid op: ", RawPointer(reinterpret_cast<void*>(op)), " at offset: ", RawPointer(reinterpret_cast<void*>(m_offset)));
             return false;
@@ -350,7 +350,7 @@
 
     case OpType::Block: {
         Type inlineSignature;
-        if (!parseValueType(inlineSignature))
+        if (!parseResultType(inlineSignature))
             return false;
 
         m_controlStack.append({ WTFMove(m_expressionStack), m_context.addBlock(inlineSignature) });
@@ -360,7 +360,7 @@
 
     case OpType::Loop: {
         Type inlineSignature;
-        if (!parseValueType(inlineSignature))
+        if (!parseResultType(inlineSignature))
             return false;
 
         m_controlStack.append({ WTFMove(m_expressionStack), m_context.addLoop(inlineSignature) });
@@ -370,7 +370,7 @@
 
     case OpType::If: {
         Type inlineSignature;
-        if (!parseValueType(inlineSignature))
+        if (!parseResultType(inlineSignature))
             return false;
 
         ExpressionType condition;

Modified: trunk/Source/_javascript_Core/wasm/WasmModuleParser.cpp (209082 => 209083)


--- trunk/Source/_javascript_Core/wasm/WasmModuleParser.cpp	2016-11-29 19:20:08 UTC (rev 209082)
+++ trunk/Source/_javascript_Core/wasm/WasmModuleParser.cpp	2016-11-29 19:37:00 UTC (rev 209083)
@@ -185,10 +185,10 @@
             return false;
 
         for (unsigned i = 0; i != argumentCount; ++i) {
-            uint8_t argumentType;
-            if (!parseUInt7(argumentType) || !isValueType(static_cast<Type>(argumentType)))
+            Type argumentType;
+            if (!parseResultType(argumentType))
                 return false;
-            argumentTypes.uncheckedAppend(static_cast<Type>(argumentType));
+            argumentTypes.uncheckedAppend(argumentType);
         }
 
         uint8_t returnCount;

Modified: trunk/Source/_javascript_Core/wasm/WasmParser.h (209082 => 209083)


--- trunk/Source/_javascript_Core/wasm/WasmParser.h	2016-11-29 19:20:08 UTC (rev 209082)
+++ trunk/Source/_javascript_Core/wasm/WasmParser.h	2016-11-29 19:37:00 UTC (rev 209083)
@@ -49,10 +49,12 @@
     bool WARN_UNUSED_RETURN parseVarUInt1(uint8_t&);
     bool WARN_UNUSED_RETURN parseInt7(int8_t&);
     bool WARN_UNUSED_RETURN parseUInt7(uint8_t&);
+    bool WARN_UNUSED_RETURN parseUInt8(uint8_t&);
     bool WARN_UNUSED_RETURN parseUInt32(uint32_t&);
     bool WARN_UNUSED_RETURN parseVarUInt32(uint32_t&);
     bool WARN_UNUSED_RETURN parseVarUInt64(uint64_t&);
 
+    bool WARN_UNUSED_RETURN parseResultType(Type&);
     bool WARN_UNUSED_RETURN parseValueType(Type&);
     bool WARN_UNUSED_RETURN parseExternalKind(External::Kind&);
 
@@ -131,6 +133,14 @@
     return true;
 }
 
+ALWAYS_INLINE bool Parser::parseUInt8(uint8_t& result)
+{
+    if (m_offset >= length())
+        return false;
+    result = source()[m_offset++];
+    return true;
+}
+
 ALWAYS_INLINE bool Parser::parseInt7(int8_t& result)
 {
     if (m_offset >= length())
@@ -157,16 +167,21 @@
     return temp <= 1;
 }
 
-ALWAYS_INLINE bool Parser::parseValueType(Type& result)
+ALWAYS_INLINE bool Parser::parseResultType(Type& result)
 {
     uint8_t value;
     if (!parseUInt7(value))
         return false;
-    if (value >= static_cast<uint8_t>(Type::LastValueType))
+    if (value > static_cast<uint8_t>(Type::LastValueType))
         return false;
     result = static_cast<Type>(value);
     return true;
 }
+
+ALWAYS_INLINE bool Parser::parseValueType(Type& result)
+{
+    return parseResultType(result) && isValueType(result);
+}
     
 ALWAYS_INLINE bool Parser::parseExternalKind(External::Kind& result)
 {

Modified: trunk/Source/_javascript_Core/wasm/generateWasmB3IRGeneratorInlinesHeader.py (209082 => 209083)


--- trunk/Source/_javascript_Core/wasm/generateWasmB3IRGeneratorInlinesHeader.py	2016-11-29 19:20:08 UTC (rev 209082)
+++ trunk/Source/_javascript_Core/wasm/generateWasmB3IRGeneratorInlinesHeader.py	2016-11-29 19:37:00 UTC (rev 209083)
@@ -28,6 +28,7 @@
 from generateWasm import *
 import optparse
 import sys
+import re
 
 parser = optparse.OptionParser(usage="usage: %prog <wasm.json> <WasmOps.h>")
 (options, args) = parser.parse_args(sys.argv[0:])
@@ -38,21 +39,161 @@
 opcodes = wasm.opcodes
 wasmB3IRGeneratorHFile = open(args[2], "w")
 
+opcodeRegex = re.compile('([a-zA-Z0-9]+)')
+argumentRegex = re.compile('(\@[0-9]+)')
+decimalRegex = re.compile('([-]?[0-9]+)')
+whitespaceRegex = re.compile('\s+')
+commaRegex = re.compile('(,)')
+oparenRegex = re.compile('(\()')
+cparenRegex = re.compile('(\))')
 
+
+class Source:
+    def __init__(self, contents, offset=0):
+        self.contents = contents
+        self.offset = offset
+
+
+def read(regex, source):
+    match = regex.match(source.contents, source.offset)
+    if not match:
+        return None
+    source.offset = match.end()
+    return match.group()
+
+
+def lex(source):
+    result = []
+    while source.offset != len(source.contents):
+        read(whitespaceRegex, source)
+        opcode = read(opcodeRegex, source)
+        if opcode:
+            result.append(opcode)
+            continue
+
+        argument = read(argumentRegex, source)
+        if argument:
+            result.append(argument)
+            continue
+
+        number = read(decimalRegex, source)
+        if number:
+            result.append(int(number))
+            continue
+
+        oparen = read(oparenRegex, source)
+        if oparen:
+            result.append(oparen)
+            continue
+
+        cparen = read(cparenRegex, source)
+        if cparen:
+            result.append(cparen)
+            continue
+
+        comma = read(commaRegex, source)
+        if comma:
+            # Skip commas
+            continue
+
+        raise Exception("Lexing Error: could not lex token from: " + source.contents + " at offset: " + str(source.offset) + " (" + source.contents[source.offset:] + "). With tokens: [" + ", ".join(result) + "]")
+    return result
+
+
+class CodeGenerator:
+    def __init__(self, tokens):
+        self.tokens = tokens
+        self.index = 0
+        self.code = []
+
+    def advance(self):
+        self.index += 1
+
+    def token(self):
+        return self.tokens[self.index]
+
+    def parseError(self, string):
+        raise Exception("Parse error " + string)
+
+    def consume(self, string):
+        if self.token() != string:
+            self.parseError("Expected " + string + " but got " + self.token())
+        self.advance()
+
+    def generateParameters(self):
+        self.advance()
+        params = []
+        tokens = self.tokens
+        while self.index < len(tokens):
+            if self.token() == ")":
+                self.advance()
+                return params
+            params.append(self.generateOpcode())
+        self.parseError("Parsing arguments fell off end")
+
+    def generateOpcode(self):
+        result = None
+        if self.token() == "i32":
+            self.advance()
+            self.consume("(")
+            self.code.append(generateI32ConstCode(self.index, self.token()))
+            result = temp(self.index)
+            self.advance()
+            self.consume(")")
+        elif argumentRegex.match(self.token()):
+            result = "arg" + self.token()[1:]
+            self.advance()
+        else:
+            op = self.token()
+            index = self.index
+            self.advance()
+            params = self.generateParameters()
+            self.code.append(generateB3OpCode(index, op, params))
+            result = temp(index)
+
+        return result
+
+    def generate(self, wasmOp):
+        if len(self.tokens) == 1:
+            params = ["arg" + str(param) for param in range(len(wasmOp["parameter"]))]
+            return "    result = m_currentBlock->appendNew<Value>(m_proc, B3::" + self.token() + ", Origin(), " + ", ".join(params) + ")"
+        result = self.generateOpcode()
+        self.code.append("result = " + result)
+        return "    " + "    \n".join(self.code)
+
+
+def temp(index):
+    return "temp" + str(index)
+
+
+def generateB3OpCode(index, op, params):
+    return "Value* " + temp(index) + " = m_currentBlock->appendNew<Value>(m_proc, B3::" + op + ", Origin(), " + ", ".join(params) + ");"
+
+
+def generateI32ConstCode(index, value):
+    return "Value* " + temp(index) + " = m_currentBlock->appendIntConstant(m_proc, Origin(), B3::Int32, " + value + ");"
+
+
+def generateB3Code(wasmOp, source):
+    tokens = lex(Source(source))
+    parser = CodeGenerator(tokens)
+    return parser.generate(wasmOp)
+
+
 def generateSimpleCode(op):
     opcode = op["opcode"]
     b3op = opcode["b3op"]
     args = ["ExpressionType arg" + str(param) for param in range(len(opcode["parameter"]))]
     args.append("ExpressionType& result")
-    params = ["arg" + str(param) for param in range(len(opcode["parameter"]))]
     return """
 template<> bool B3IRGenerator::addOp<OpType::""" + wasm.toCpp(op["name"]) + ">(" + ", ".join(args) + """)
 {
-    result = m_currentBlock->appendNew<Value>(m_proc, B3::""" + opcode["b3op"] + ", Origin(), " + ", ".join(params) + """);
+""" + generateB3Code(opcode, b3op) + """;
     return true;
 }
 """
 
+
 definitions = [generateSimpleCode(op) for op in wasm.opcodeIterator(lambda op: isSimple(op) and (isBinary(op) or isUnary(op)))]
 contents = wasm.header + """
 

Modified: trunk/Source/_javascript_Core/wasm/wasm.json (209082 => 209083)


--- trunk/Source/_javascript_Core/wasm/wasm.json	2016-11-29 19:20:08 UTC (rev 209082)
+++ trunk/Source/_javascript_Core/wasm/wasm.json	2016-11-29 19:37:00 UTC (rev 209083)
@@ -120,7 +120,7 @@
         "i32.clz":             { "category": "arithmetic", "value":  87, "return": ["i32"],      "parameter": ["i32"],                  "immediate": [], "b3op": "Clz"          },
         "i32.ctz":             { "category": "arithmetic", "value":  88, "return": ["i32"],      "parameter": ["i32"],                  "immediate": []                         },
         "i32.popcnt":          { "category": "arithmetic", "value":  89, "return": ["i32"],      "parameter": ["i32"],                  "immediate": []                         },
-        "i32.eqz":             { "category": "comparison", "value":  90, "return": ["bool"],     "parameter": ["i32"],                  "immediate": []                         },
+        "i32.eqz":             { "category": "comparison", "value":  90, "return": ["bool"],     "parameter": ["i32"],                  "immediate": [], "b3op": "Equal(i32(0), @0)" },
         "i64.add":             { "category": "arithmetic", "value":  91, "return": ["i64"],      "parameter": ["i64", "i64"],           "immediate": [], "b3op": "Add"          },
         "i64.sub":             { "category": "arithmetic", "value":  92, "return": ["i64"],      "parameter": ["i64", "i64"],           "immediate": [], "b3op": "Sub"          },
         "i64.mul":             { "category": "arithmetic", "value":  93, "return": ["i64"],      "parameter": ["i64", "i64"],           "immediate": [], "b3op": "Mul"          },
@@ -149,15 +149,15 @@
         "i64.clz":             { "category": "arithmetic", "value": 114, "return": ["i64"],      "parameter": ["i64"],                  "immediate": [], "b3op": "Clz"          },
         "i64.ctz":             { "category": "arithmetic", "value": 115, "return": ["i64"],      "parameter": ["i64"],                  "immediate": []                         },
         "i64.popcnt":          { "category": "arithmetic", "value": 116, "return": ["i64"],      "parameter": ["i64"],                  "immediate": []                         },
-        "i64.eqz":             { "category": "comparison", "value": 186, "return": ["bool"],     "parameter": ["i64"],                  "immediate": []                         },
+        "i64.eqz":             { "category": "comparison", "value": 186, "return": ["bool"],     "parameter": ["i64"],                  "immediate": [], "b3op": "Equal(i32(0), @0)" },
         "f32.add":             { "category": "arithmetic", "value": 117, "return": ["f32"],      "parameter": ["f32", "f32"],           "immediate": [], "b3op": "Add"          },
         "f32.sub":             { "category": "arithmetic", "value": 118, "return": ["f32"],      "parameter": ["f32", "f32"],           "immediate": [], "b3op": "Sub"          },
         "f32.mul":             { "category": "arithmetic", "value": 119, "return": ["f32"],      "parameter": ["f32", "f32"],           "immediate": [], "b3op": "Mul"          },
         "f32.div":             { "category": "arithmetic", "value": 120, "return": ["f32"],      "parameter": ["f32", "f32"],           "immediate": [], "b3op": "Div"          },
-        "f32.min":             { "category": "arithmetic", "value": 121, "return": ["f32"],      "parameter": ["f32", "f32"],           "immediate": []                         },
-        "f32.max":             { "category": "arithmetic", "value": 122, "return": ["f32"],      "parameter": ["f32", "f32"],           "immediate": []                         },
+        "f32.min":             { "category": "arithmetic", "value": 121, "return": ["f32"],      "parameter": ["f32", "f32"],           "immediate": [], "b3op": "Select(LessThan(@0, @1), @0, @1)" },
+        "f32.max":             { "category": "arithmetic", "value": 122, "return": ["f32"],      "parameter": ["f32", "f32"],           "immediate": [], "b3op": "Select(LessThan(@0, @1), @1, @0)" },
         "f32.abs":             { "category": "arithmetic", "value": 123, "return": ["f32"],      "parameter": ["f32"],                  "immediate": [], "b3op": "Abs"          },
-        "f32.neg":             { "category": "arithmetic", "value": 124, "return": ["f32"],      "parameter": ["f32"],                  "immediate": []                         },
+        "f32.neg":             { "category": "arithmetic", "value": 124, "return": ["f32"],      "parameter": ["f32"],                  "immediate": [], "b3op": "Neg"          },
         "f32.copysign":        { "category": "arithmetic", "value": 125, "return": ["f32"],      "parameter": ["f32"],                  "immediate": []                         },
         "f32.ceil":            { "category": "arithmetic", "value": 126, "return": ["f32"],      "parameter": ["f32"],                  "immediate": [], "b3op": "Ceil"         },
         "f32.floor":           { "category": "arithmetic", "value": 127, "return": ["f32"],      "parameter": ["f32"],                  "immediate": [], "b3op": "Floor"        },
@@ -174,10 +174,10 @@
         "f64.sub":             { "category": "arithmetic", "value": 138, "return": ["f64"],      "parameter": ["f64", "f64"],           "immediate": [], "b3op": "Sub"          },
         "f64.mul":             { "category": "arithmetic", "value": 139, "return": ["f64"],      "parameter": ["f64", "f64"],           "immediate": [], "b3op": "Mul"          },
         "f64.div":             { "category": "arithmetic", "value": 140, "return": ["f64"],      "parameter": ["f64", "f64"],           "immediate": [], "b3op": "Div"          },
-        "f64.min":             { "category": "arithmetic", "value": 141, "return": ["f64"],      "parameter": ["f64", "f64"],           "immediate": []                         },
-        "f64.max":             { "category": "arithmetic", "value": 142, "return": ["f64"],      "parameter": ["f64", "f64"],           "immediate": []                         },
+        "f64.min":             { "category": "arithmetic", "value": 141, "return": ["f64"],      "parameter": ["f64", "f64"],           "immediate": [], "b3op": "Select(LessThan(@0, @1), @0, @1)" },
+        "f64.max":             { "category": "arithmetic", "value": 142, "return": ["f64"],      "parameter": ["f64", "f64"],           "immediate": [], "b3op": "Select(LessThan(@0, @1), @1, @0)" },
         "f64.abs":             { "category": "arithmetic", "value": 143, "return": ["f64"],      "parameter": ["f64"],                  "immediate": [], "b3op": "Abs"          },
-        "f64.neg":             { "category": "arithmetic", "value": 144, "return": ["f64"],      "parameter": ["f64"],                  "immediate": []                         },
+        "f64.neg":             { "category": "arithmetic", "value": 144, "return": ["f64"],      "parameter": ["f64"],                  "immediate": [], "b3op": "Neg"          },
         "f64.copysign":        { "category": "arithmetic", "value": 145, "return": ["f64"],      "parameter": ["f64"],                  "immediate": []                         },
         "f64.ceil":            { "category": "arithmetic", "value": 146, "return": ["f64"],      "parameter": ["f64"],                  "immediate": [], "b3op": "Ceil"         },
         "f64.floor":           { "category": "arithmetic", "value": 147, "return": ["f64"],      "parameter": ["f64"],                  "immediate": [], "b3op": "Floor"        },
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to