Add more missing trivial wasm ops.
authorkeith_miller@apple.com <keith_miller@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 8 Dec 2016 01:13:40 +0000 (01:13 +0000)
committerkeith_miller@apple.com <keith_miller@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 8 Dec 2016 01:13:40 +0000 (01:13 +0000)
https://bugs.webkit.org/show_bug.cgi?id=165564

JSTests:

Add tests for drop and tee_local.

Reviewed by Geoffrey Garen.

* wasm/function-tests/drop.js: Added.
* wasm/function-tests/nop.js: Added.
* wasm/function-tests/tee-local.js: Added.

Source/JavaScriptCore:

Reviewed by Geoffrey Garen.

This patch adds the nop, drop, and tee_local opcodes.
It also fixes an issue where we were not generating
the proper enums for the grow_memory and current_memory
opcodes.

* wasm/WasmFunctionParser.h:
(JSC::Wasm::FunctionParser<Context>::parseExpression):
* wasm/generateWasmOpsHeader.py:

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@209503 268f45cc-cd09-0410-ab3c-d52691b4dbfc

JSTests/ChangeLog
JSTests/wasm/function-tests/drop.js [new file with mode: 0644]
JSTests/wasm/function-tests/nop.js [new file with mode: 0644]
JSTests/wasm/function-tests/tee-local.js [new file with mode: 0644]
Source/JavaScriptCore/ChangeLog
Source/JavaScriptCore/wasm/WasmFunctionParser.h
Source/JavaScriptCore/wasm/generateWasmOpsHeader.py

index a0610ad..83922ef 100644 (file)
@@ -1,3 +1,16 @@
+2016-12-07  Keith Miller  <keith_miller@apple.com>
+
+        Add more missing trivial wasm ops.
+        https://bugs.webkit.org/show_bug.cgi?id=165564
+
+        Add tests for drop and tee_local.
+
+        Reviewed by Geoffrey Garen.
+
+        * wasm/function-tests/drop.js: Added.
+        * wasm/function-tests/nop.js: Added.
+        * wasm/function-tests/tee-local.js: Added.
+
 2016-12-06  Keith Miller  <keith_miller@apple.com>
 
         Add support for truncation operators
diff --git a/JSTests/wasm/function-tests/drop.js b/JSTests/wasm/function-tests/drop.js
new file mode 100644 (file)
index 0000000..298ef2d
--- /dev/null
@@ -0,0 +1,39 @@
+import Builder from '../Builder.js'
+
+const b = new Builder();
+b.Type().End()
+    .Function().End()
+    .Code()
+
+    // This function adds one to the argument.
+    .Function({ params: ["i32"], ret: "i32" }, [])
+    .GetLocal(0)
+    .I32Const(1)
+    .TeeLocal(0)
+    .GetLocal(0)
+    .Drop()
+    .I32Add()
+    .End()
+
+    // This is the id function.
+    .Function({ params: ["i32"], ret: "i32" }, [])
+    .GetLocal(0)
+    .I32Const(1)
+    .Drop()
+    .End()
+
+const bin = b.WebAssembly()
+bin.trim();
+testWasmModuleFunctions(bin.get(), 2,
+                        [[{ type: "i32", value: 1 }, [{ type: "i32", value: 0 }]],
+                         [{ type: "i32", value: 2 }, [{ type: "i32", value: 1 }]],
+                         [{ type: "i32", value: 101 }, [{ type: "i32", value: 100 }]],
+                         [{ type: "i32", value: -99 }, [{ type: "i32", value: -100 }]],
+                        ],
+
+                        [[{ type: "i32", value: 0 }, [{ type: "i32", value: 0 }]],
+                         [{ type: "i32", value: 1 }, [{ type: "i32", value: 1 }]],
+                         [{ type: "i32", value: 100 }, [{ type: "i32", value: 100 }]],
+                         [{ type: "i32", value: -100 }, [{ type: "i32", value: -100 }]],
+                        ],
+                       );
diff --git a/JSTests/wasm/function-tests/nop.js b/JSTests/wasm/function-tests/nop.js
new file mode 100644 (file)
index 0000000..8cba43d
--- /dev/null
@@ -0,0 +1,34 @@
+import Builder from '../Builder.js'
+
+const b = new Builder();
+b.Type().End()
+    .Function().End()
+    .Code()
+
+    // This function adds one to the argument.
+    .Function({ params: ["i32"], ret: "i32" }, [])
+    .GetLocal(0)
+    .Nop()
+    .End()
+
+    // This is the id function.
+    .Function({ params: ["i32"], ret: "i32" }, [])
+    .Nop()
+    .GetLocal(0)
+    .End()
+
+const bin = b.WebAssembly()
+bin.trim();
+testWasmModuleFunctions(bin.get(), 2,
+                        [[{ type: "i32", value: 0 }, [{ type: "i32", value: 0 }]],
+                         [{ type: "i32", value: 1 }, [{ type: "i32", value: 1 }]],
+                         [{ type: "i32", value: 100 }, [{ type: "i32", value: 100 }]],
+                         [{ type: "i32", value: -100 }, [{ type: "i32", value: -100 }]],
+                        ],
+
+                        [[{ type: "i32", value: 0 }, [{ type: "i32", value: 0 }]],
+                         [{ type: "i32", value: 1 }, [{ type: "i32", value: 1 }]],
+                         [{ type: "i32", value: 100 }, [{ type: "i32", value: 100 }]],
+                         [{ type: "i32", value: -100 }, [{ type: "i32", value: -100 }]],
+                        ],
+                       );
diff --git a/JSTests/wasm/function-tests/tee-local.js b/JSTests/wasm/function-tests/tee-local.js
new file mode 100644 (file)
index 0000000..860f6c4
--- /dev/null
@@ -0,0 +1,26 @@
+import Builder from '../Builder.js'
+
+const b = new Builder();
+b.Type().End()
+    .Function().End()
+    .Code()
+
+    // This function multiplies the argument by 2
+    .Function({ params: ["i32"], ret: "i32" }, [])
+    .GetLocal(0)
+    .I32Const(1)
+    .TeeLocal(0)
+    .GetLocal(0)
+    .I32Add()
+    .I32Mul()
+    .End()
+
+const bin = b.WebAssembly()
+bin.trim();
+testWasmModuleFunctions(bin.get(), 1,
+                        [[{ type: "i32", value: 0 }, [{ type: "i32", value: 0 }]],
+                         [{ type: "i32", value: 2 }, [{ type: "i32", value: 1 }]],
+                         [{ type: "i32", value: 200 }, [{ type: "i32", value: 100 }]],
+                         [{ type: "i32", value: -200 }, [{ type: "i32", value: -100 }]],
+                        ],
+                       );
index e1ea4a1..b798aee 100644 (file)
@@ -1,3 +1,19 @@
+2016-12-07  Keith Miller  <keith_miller@apple.com>
+
+        Add more missing trivial wasm ops.
+        https://bugs.webkit.org/show_bug.cgi?id=165564
+
+        Reviewed by Geoffrey Garen.
+
+        This patch adds the nop, drop, and tee_local opcodes.
+        It also fixes an issue where we were not generating
+        the proper enums for the grow_memory and current_memory
+        opcodes.
+
+        * wasm/WasmFunctionParser.h:
+        (JSC::Wasm::FunctionParser<Context>::parseExpression):
+        * wasm/generateWasmOpsHeader.py:
+
 2016-12-07  Geoffrey Garen  <ggaren@apple.com>
 
         Renamed source => parentSource
index 89b0ab3..e621125 100644 (file)
@@ -336,6 +336,15 @@ bool FunctionParser<Context>::parseExpression(OpType op)
         return m_context.setLocal(index, value);
     }
 
+    case OpType::TeeLocal: {
+        uint32_t index;
+        if (!parseVarUInt32(index))
+            return false;
+        if (!m_expressionStack.size())
+            return false;
+        return m_context.setLocal(index, m_expressionStack.last());
+    }
+
     case OpType::Call: {
         uint32_t functionIndex;
         if (!parseVarUInt32(functionIndex))
@@ -492,7 +501,24 @@ bool FunctionParser<Context>::parseExpression(OpType op)
         return true;
     }
 
-    default: {
+    case OpType::Drop: {
+        if (!m_expressionStack.size()) {
+            setErrorMessage("Attempted to drop an expression from an empty stack.");
+            return false;
+        }
+        m_expressionStack.takeLast();
+        return true;
+    }
+
+    case OpType::Nop: {
+        return true;
+    }
+
+    case OpType::GrowMemory:
+    case OpType::CurrentMemory:
+    case OpType::GetGlobal:
+    case OpType::SetGlobal:
+    case OpType::CallIndirect: {
         // FIXME: Not yet implemented.
         return false;
     }
index ce89484..a553fb9 100755 (executable)
@@ -65,7 +65,7 @@ def opcodeMacroizer(filter):
         inc += 1
 
 defines = ["#define FOR_EACH_WASM_SPECIAL_OP(macro)"]
-defines.extend([op for op in opcodeMacroizer(lambda op: op["category"] == "special" or op["category"] == "call")])
+defines.extend([op for op in opcodeMacroizer(lambda op: not (isUnary(op) or isBinary(op) or op["category"] == "control" or op["category"] == "memory"))])
 defines.append("\n\n#define FOR_EACH_WASM_CONTROL_FLOW_OP(macro)")
 defines.extend([op for op in opcodeMacroizer(lambda op: op["category"] == "control")])
 defines.append("\n\n#define FOR_EACH_WASM_SIMPLE_UNARY_OP(macro)")