summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <azakai@google.com>2021-02-26 00:08:10 +0000
committerGitHub <noreply@github.com>2021-02-25 16:08:10 -0800
commit142d5f32ce792327de62b62f09f25528dcd86950 (patch)
treee0459bb2a2ec3a9599f1d1dca94c702800f45eba
parent2572bbcd975c3bb7f99112ced7fe437e7a88eec0 (diff)
downloadbinaryen-142d5f32ce792327de62b62f09f25528dcd86950.tar.gz
binaryen-142d5f32ce792327de62b62f09f25528dcd86950.tar.bz2
binaryen-142d5f32ce792327de62b62f09f25528dcd86950.zip
[Wasm GC] Fix the order of operands in array.new and struct.new (#3617)
Also add a missing source file for a GC test, let.wasm.
-rw-r--r--src/passes/Print.cpp6
-rw-r--r--src/wasm/wasm-s-parser.cpp15
-rw-r--r--test/heap-types.wast8
-rw-r--r--test/heap-types.wast.from-wast8
-rw-r--r--test/heap-types.wast.fromBinary8
-rw-r--r--test/heap-types.wast.fromBinary.noDebugInfo8
-rw-r--r--test/let.txt42
-rw-r--r--test/let.wasm.fromBinary12
-rw-r--r--test/passes/Oz_fuzz-exec_all-features.txt4
-rw-r--r--test/passes/Oz_fuzz-exec_all-features.wast8
10 files changed, 81 insertions, 38 deletions
diff --git a/src/passes/Print.cpp b/src/passes/Print.cpp
index 7a160a8eb..445021bcf 100644
--- a/src/passes/Print.cpp
+++ b/src/passes/Print.cpp
@@ -2752,10 +2752,10 @@ struct PrintSExpression : public OverriddenVisitor<PrintSExpression> {
o << '(';
printExpressionContents(curr);
incIndent();
- printFullLine(curr->rtt);
for (auto& operand : curr->operands) {
printFullLine(operand);
}
+ printFullLine(curr->rtt);
decIndent();
}
void visitStructGet(StructGet* curr) {
@@ -2777,11 +2777,11 @@ struct PrintSExpression : public OverriddenVisitor<PrintSExpression> {
o << '(';
printExpressionContents(curr);
incIndent();
- printFullLine(curr->rtt);
- printFullLine(curr->size);
if (curr->init) {
printFullLine(curr->init);
}
+ printFullLine(curr->size);
+ printFullLine(curr->rtt);
decIndent();
}
void visitArrayGet(ArrayGet* curr) {
diff --git a/src/wasm/wasm-s-parser.cpp b/src/wasm/wasm-s-parser.cpp
index 339d3a04e..7182860ed 100644
--- a/src/wasm/wasm-s-parser.cpp
+++ b/src/wasm/wasm-s-parser.cpp
@@ -2401,8 +2401,6 @@ Expression* SExpressionWasmBuilder::makeRttSub(Element& s) {
Expression* SExpressionWasmBuilder::makeStructNew(Element& s, bool default_) {
auto heapType = parseHeapType(*s[1]);
- auto* rtt = parseExpression(*s[2]);
- validateHeapTypeUsingChild(rtt, heapType, s);
auto numOperands = s.size() - 3;
if (default_ && numOperands > 0) {
throw ParseException(
@@ -2411,8 +2409,10 @@ Expression* SExpressionWasmBuilder::makeStructNew(Element& s, bool default_) {
std::vector<Expression*> operands;
operands.resize(numOperands);
for (Index i = 0; i < numOperands; i++) {
- operands[i] = parseExpression(*s[i + 3]);
+ operands[i] = parseExpression(*s[i + 2]);
}
+ auto* rtt = parseExpression(*s[s.size() - 1]);
+ validateHeapTypeUsingChild(rtt, heapType, s);
return Builder(wasm).makeStructNew(rtt, operands);
}
@@ -2454,13 +2454,14 @@ Expression* SExpressionWasmBuilder::makeStructSet(Element& s) {
Expression* SExpressionWasmBuilder::makeArrayNew(Element& s, bool default_) {
auto heapType = parseHeapType(*s[1]);
- auto* rtt = parseExpression(*s[2]);
- validateHeapTypeUsingChild(rtt, heapType, s);
- auto* size = parseExpression(*s[3]);
Expression* init = nullptr;
+ size_t i = 2;
if (!default_) {
- init = parseExpression(*s[4]);
+ init = parseExpression(*s[i++]);
}
+ auto* size = parseExpression(*s[i++]);
+ auto* rtt = parseExpression(*s[i++]);
+ validateHeapTypeUsingChild(rtt, heapType, s);
return Builder(wasm).makeArrayNew(rtt, size, init);
}
diff --git a/test/heap-types.wast b/test/heap-types.wast
index 87d751691..e645ee74d 100644
--- a/test/heap-types.wast
+++ b/test/heap-types.wast
@@ -95,10 +95,10 @@
)
(drop
(struct.new_with_rtt $struct.A
- (rtt.canon $struct.A)
(i32.const 1)
(f32.const 2.345)
(f64.const 3.14159)
+ (rtt.canon $struct.A)
)
)
(unreachable)
@@ -110,15 +110,15 @@
(local $tw (ref null $words))
(drop
(array.new_with_rtt $vector
- (rtt.canon $vector)
- (i32.const 3)
(f64.const 3.14159)
+ (i32.const 3)
+ (rtt.canon $vector)
)
)
(drop
(array.new_default_with_rtt $matrix
- (rtt.canon $matrix)
(i32.const 10)
+ (rtt.canon $matrix)
)
)
(drop
diff --git a/test/heap-types.wast.from-wast b/test/heap-types.wast.from-wast
index ceef0739a..b4228fd20 100644
--- a/test/heap-types.wast.from-wast
+++ b/test/heap-types.wast.from-wast
@@ -99,10 +99,10 @@
)
(drop
(struct.new_with_rtt $struct.A
- (rtt.canon $struct.A)
(i32.const 1)
(f32.const 2.3450000286102295)
(f64.const 3.14159)
+ (rtt.canon $struct.A)
)
)
(unreachable)
@@ -114,15 +114,15 @@
(local $tw (ref null $words))
(drop
(array.new_with_rtt $vector
- (rtt.canon $vector)
- (i32.const 3)
(f64.const 3.14159)
+ (i32.const 3)
+ (rtt.canon $vector)
)
)
(drop
(array.new_default_with_rtt $matrix
- (rtt.canon $matrix)
(i32.const 10)
+ (rtt.canon $matrix)
)
)
(drop
diff --git a/test/heap-types.wast.fromBinary b/test/heap-types.wast.fromBinary
index 78e01e494..75dcae823 100644
--- a/test/heap-types.wast.fromBinary
+++ b/test/heap-types.wast.fromBinary
@@ -99,10 +99,10 @@
)
(drop
(struct.new_with_rtt $struct.A
- (rtt.canon $struct.A)
(i32.const 1)
(f32.const 2.3450000286102295)
(f64.const 3.14159)
+ (rtt.canon $struct.A)
)
)
(unreachable)
@@ -114,15 +114,15 @@
(local $tw (ref null $vector))
(drop
(array.new_with_rtt $vector
- (rtt.canon $vector)
- (i32.const 3)
(f64.const 3.14159)
+ (i32.const 3)
+ (rtt.canon $vector)
)
)
(drop
(array.new_default_with_rtt $matrix
- (rtt.canon $matrix)
(i32.const 10)
+ (rtt.canon $matrix)
)
)
(drop
diff --git a/test/heap-types.wast.fromBinary.noDebugInfo b/test/heap-types.wast.fromBinary.noDebugInfo
index 1ae5792cb..e0b465bed 100644
--- a/test/heap-types.wast.fromBinary.noDebugInfo
+++ b/test/heap-types.wast.fromBinary.noDebugInfo
@@ -99,10 +99,10 @@
)
(drop
(struct.new_with_rtt ${i32_f32_f64}
- (rtt.canon ${i32_f32_f64})
(i32.const 1)
(f32.const 2.3450000286102295)
(f64.const 3.14159)
+ (rtt.canon ${i32_f32_f64})
)
)
(unreachable)
@@ -114,15 +114,15 @@
(local $4 (ref null $[mut:f64]))
(drop
(array.new_with_rtt $[mut:f64]
- (rtt.canon $[mut:f64])
- (i32.const 3)
(f64.const 3.14159)
+ (i32.const 3)
+ (rtt.canon $[mut:f64])
)
)
(drop
(array.new_default_with_rtt $[ref?|[mut:f64]|]
- (rtt.canon $[ref?|[mut:f64]|])
(i32.const 10)
+ (rtt.canon $[ref?|[mut:f64]|])
)
)
(drop
diff --git a/test/let.txt b/test/let.txt
new file mode 100644
index 000000000..9901aaad8
--- /dev/null
+++ b/test/let.txt
@@ -0,0 +1,42 @@
+;; source code for let.wasm, which was created by wasp
+(module
+ (type $vector (array (mut f64)))
+ (func $main
+ (local $x i32)
+ (local $y i32)
+ (drop (local.get $x)) ;; 0 is the index appearing in the binary
+ ;; first let
+ (array.new_with_rtt $vector
+ (f64.const 3.14159)
+ (i32.const 1)
+ (rtt.canon $vector)
+ )
+ (let (local $v (ref $vector))
+ (drop (local.get $v)) ;; 0
+ (drop (local.get $x)) ;; 1
+ ;; another one, nested
+ (array.new_with_rtt $vector
+ (f64.const 1234)
+ (i32.const 2)
+ (rtt.canon $vector)
+ )
+ (let (local $w (ref $vector))
+ (drop (local.get $v)) ;; 1
+ (drop (local.get $w)) ;; 0
+ (drop (local.get $x)) ;; 2
+ )
+ )
+ ;; another one, later
+ (array.new_with_rtt $vector
+ (f64.const 2.1828)
+ (i32.const 3)
+ (rtt.canon $vector)
+ )
+ (let (local $v (ref $vector))
+ (drop (local.get $v)) ;; 0
+ (drop (local.get $x)) ;; 1
+ )
+ (drop (local.get $x)) ;; 0
+ )
+)
+
diff --git a/test/let.wasm.fromBinary b/test/let.wasm.fromBinary
index 1de54a96d..d3f1760ce 100644
--- a/test/let.wasm.fromBinary
+++ b/test/let.wasm.fromBinary
@@ -13,9 +13,9 @@
(block
(local.set $2
(array.new_with_rtt $[mut:f64]
- (rtt.canon $[mut:f64])
- (i32.const 1)
(f64.const 3.14159)
+ (i32.const 1)
+ (rtt.canon $[mut:f64])
)
)
(block
@@ -28,9 +28,9 @@
(block
(local.set $3
(array.new_with_rtt $[mut:f64]
- (rtt.canon $[mut:f64])
- (i32.const 2)
(f64.const 1234)
+ (i32.const 2)
+ (rtt.canon $[mut:f64])
)
)
(block
@@ -50,9 +50,9 @@
(block
(local.set $4
(array.new_with_rtt $[mut:f64]
- (rtt.canon $[mut:f64])
- (i32.const 3)
(f64.const 2.1828)
+ (i32.const 3)
+ (rtt.canon $[mut:f64])
)
)
(block
diff --git a/test/passes/Oz_fuzz-exec_all-features.txt b/test/passes/Oz_fuzz-exec_all-features.txt
index baa2a24e9..9d79199f5 100644
--- a/test/passes/Oz_fuzz-exec_all-features.txt
+++ b/test/passes/Oz_fuzz-exec_all-features.txt
@@ -89,9 +89,9 @@
(array.len $bytes
(local.tee $0
(array.new_with_rtt $bytes
- (rtt.canon $bytes)
- (i32.const 50)
(i32.const 42)
+ (i32.const 50)
+ (rtt.canon $bytes)
)
)
)
diff --git a/test/passes/Oz_fuzz-exec_all-features.wast b/test/passes/Oz_fuzz-exec_all-features.wast
index 44ea8625b..97e270f0f 100644
--- a/test/passes/Oz_fuzz-exec_all-features.wast
+++ b/test/passes/Oz_fuzz-exec_all-features.wast
@@ -46,9 +46,9 @@
(local $x (ref null $bytes))
(local.set $x
(array.new_with_rtt $bytes
- (rtt.canon $bytes)
- (i32.const 50) ;; size
(i32.const 42) ;; value to splat into the array
+ (i32.const 50) ;; size
+ (rtt.canon $bytes)
)
)
;; The length should be 50
@@ -87,9 +87,9 @@
(call $log
(ref.test $struct
(array.new_with_rtt $bytes
- (rtt.canon $bytes)
- (i32.const 10)
(i32.const 20)
+ (i32.const 10)
+ (rtt.canon $bytes)
)
(rtt.canon $struct)
)