Skip to content

Commit

Permalink
Update the text syntax for tuple types (WebAssembly#6246)
Browse files Browse the repository at this point in the history
Instead of e.g. `(i32 i32)`, use `(tuple i32 i32)`. Having a keyword to
introduce the s-expression is more consistent with the rest of the language.
  • Loading branch information
tlively authored and radekdoulik committed Jul 12, 2024
1 parent 5d2acd7 commit 289c262
Show file tree
Hide file tree
Showing 47 changed files with 241 additions and 238 deletions.
1 change: 1 addition & 0 deletions src/shared-constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ extern Name PRINT;
extern Name EXIT;
extern Name SHARED;
extern Name TAG;
extern Name TUPLE;

} // namespace wasm

Expand Down
13 changes: 8 additions & 5 deletions src/wasm/wasm-s-parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1393,12 +1393,15 @@ Type SExpressionWasmBuilder::elementToType(Element& s) {
}
return Type(parseHeapType(*s[i]), nullable);
}
// It's a tuple.
std::vector<Type> types;
for (size_t i = 0; i < s.size(); ++i) {
types.push_back(elementToType(*list[i]));
if (elementStartsWith(s, TUPLE)) {
// It's a tuple.
std::vector<Type> types;
for (size_t i = 1; i < s.size(); ++i) {
types.push_back(elementToType(*list[i]));
}
return Type(types);
}
return Type(types);
throw SParseException(std::string("expected type, got list"), s);
}

Type SExpressionWasmBuilder::stringToLaneType(const char* str) {
Expand Down
6 changes: 2 additions & 4 deletions src/wasm/wasm-type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1952,11 +1952,9 @@ std::ostream& TypePrinter::print(HeapType type) {
}

std::ostream& TypePrinter::print(const Tuple& tuple) {
os << '(';
auto sep = "";
os << "(tuple";
for (Type type : tuple) {
os << sep;
sep = " ";
os << ' ';
print(type);
}
return os << ')';
Expand Down
1 change: 1 addition & 0 deletions src/wasm/wasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ Name PRINT("print");
Name EXIT("exit");
Name SHARED("shared");
Name TAG("tag");
Name TUPLE("tuple");

// Expressions

Expand Down
10 changes: 5 additions & 5 deletions test/example/typeinfo.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ i31ref
(ref null $array.0)

;; Tuple
()
(tuple)
none
(i32 f64)
(i32 f64)
(tuple i32 f64)
(tuple i32 f64)

;; Signature of references (param/result)
(func (param (ref null $struct.0)) (result (ref $array.0)))
Expand Down Expand Up @@ -77,8 +77,8 @@ none
(ref null $array.0)

;; Tuple of references
((ref $func.0) (ref null $func.0) (ref $struct.0) (ref null $struct.0) (ref $array.0) (ref null $array.0))
((ref $func.0) (ref null $func.0) (ref $struct.0) (ref null $struct.0) (ref $array.0) (ref null $array.0))
(tuple (ref $func.0) (ref null $func.0) (ref $struct.0) (ref null $struct.0) (ref $array.0) (ref null $array.0))
(tuple (ref $func.0) (ref null $func.0) (ref $struct.0) (ref null $struct.0) (ref $array.0) (ref null $array.0))

;; Recursive (not really)
(func (param (ref $func.0)))
Expand Down
12 changes: 6 additions & 6 deletions test/gtest/cfg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,9 @@ TEST_F(CFGTest, LinearReachingDefinitions) {
auto moduleText = R"wasm(
(module
(func $bar
(local $a (i32))
(local $b (i32))
(local $c (i32))
(local $a i32)
(local $b i32)
(local $c i32)
(local.set $a
(i32.const 1)
)
Expand Down Expand Up @@ -335,8 +335,8 @@ TEST_F(CFGTest, ReachingDefinitionsIf) {
auto moduleText = R"wasm(
(module
(func $bar
(local $a (i32))
(local $b (i32))
(local $a i32)
(local $b i32)
(local.set $a
(i32.const 1)
)
Expand Down Expand Up @@ -406,7 +406,7 @@ TEST_F(CFGTest, ReachingDefinitionsIf) {
TEST_F(CFGTest, ReachingDefinitionsLoop) {
auto moduleText = R"wasm(
(module
(func $bar (param $a (i32)) (param $b (i32))
(func $bar (param $a i32) (param $b i32)
(loop $loop
(drop
(local.get $a)
Expand Down
8 changes: 4 additions & 4 deletions test/lit/basic/exception-handling-old.wast
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
)

;; CHECK-TEXT: (func $try-catch-multivalue-tag (type $0)
;; CHECK-TEXT-NEXT: (local $x (i32 i64))
;; CHECK-TEXT-NEXT: (local $x (tuple i32 i64))
;; CHECK-TEXT-NEXT: (try $try
;; CHECK-TEXT-NEXT: (do
;; CHECK-TEXT-NEXT: (throw $e-i32-i64
Expand All @@ -128,7 +128,7 @@
;; CHECK-BIN: (func $try-catch-multivalue-tag (type $0)
;; CHECK-BIN-NEXT: (local $x i32)
;; CHECK-BIN-NEXT: (local $1 i64)
;; CHECK-BIN-NEXT: (local $2 (i32 i64))
;; CHECK-BIN-NEXT: (local $2 (tuple i32 i64))
;; CHECK-BIN-NEXT: (local $3 i32)
;; CHECK-BIN-NEXT: (try $label$3
;; CHECK-BIN-NEXT: (do
Expand Down Expand Up @@ -162,7 +162,7 @@
;; CHECK-BIN-NEXT: )
;; CHECK-BIN-NEXT: )
;; CHECK-BIN-NEXT: )
(func $try-catch-multivalue-tag (local $x (i32 i64))
(func $try-catch-multivalue-tag (local $x (tuple i32 i64))
(try
(do
(throw $e-i32-i64 (i32.const 0) (i64.const 0))
Expand Down Expand Up @@ -1376,7 +1376,7 @@
;; CHECK-BIN-NODEBUG: (func $3 (type $0)
;; CHECK-BIN-NODEBUG-NEXT: (local $0 i32)
;; CHECK-BIN-NODEBUG-NEXT: (local $1 i64)
;; CHECK-BIN-NODEBUG-NEXT: (local $2 (i32 i64))
;; CHECK-BIN-NODEBUG-NEXT: (local $2 (tuple i32 i64))
;; CHECK-BIN-NODEBUG-NEXT: (local $3 i32)
;; CHECK-BIN-NODEBUG-NEXT: (try $label$3
;; CHECK-BIN-NODEBUG-NEXT: (do
Expand Down
20 changes: 10 additions & 10 deletions test/lit/basic/exception-handling.wast
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,9 @@
;; CHECK-TEXT-NEXT: )
;; CHECK-TEXT-NEXT: )
;; CHECK-BIN: (func $try-table-multivalue-tag (type $0)
;; CHECK-BIN-NEXT: (local $0 (i32 i64))
;; CHECK-BIN-NEXT: (local $0 (tuple i32 i64))
;; CHECK-BIN-NEXT: (local $1 i32)
;; CHECK-BIN-NEXT: (local $2 (i32 i64 exnref))
;; CHECK-BIN-NEXT: (local $2 (tuple i32 i64 exnref))
;; CHECK-BIN-NEXT: (local $3 i64)
;; CHECK-BIN-NEXT: (local $4 i32)
;; CHECK-BIN-NEXT: (block $label$1
Expand Down Expand Up @@ -419,7 +419,7 @@
;; CHECK-TEXT-NEXT: )
;; CHECK-TEXT-NEXT: )
;; CHECK-BIN: (func $try-table-all-catch-clauses-i32-tag (type $0)
;; CHECK-BIN-NEXT: (local $0 (i32 exnref))
;; CHECK-BIN-NEXT: (local $0 (tuple i32 exnref))
;; CHECK-BIN-NEXT: (local $1 i32)
;; CHECK-BIN-NEXT: (block $label$1
;; CHECK-BIN-NEXT: (drop
Expand Down Expand Up @@ -517,10 +517,10 @@
;; CHECK-TEXT-NEXT: )
;; CHECK-TEXT-NEXT: )
;; CHECK-BIN: (func $try-table-all-catch-clauses-multivalue-tag (type $0)
;; CHECK-BIN-NEXT: (local $0 (i32 i64 exnref))
;; CHECK-BIN-NEXT: (local $0 (tuple i32 i64 exnref))
;; CHECK-BIN-NEXT: (local $1 i64)
;; CHECK-BIN-NEXT: (local $2 i32)
;; CHECK-BIN-NEXT: (local $3 (i32 i64))
;; CHECK-BIN-NEXT: (local $3 (tuple i32 i64))
;; CHECK-BIN-NEXT: (local $4 i32)
;; CHECK-BIN-NEXT: (block $label$1
;; CHECK-BIN-NEXT: (local.set $3
Expand Down Expand Up @@ -810,9 +810,9 @@
;; CHECK-BIN-NODEBUG-NEXT: )

;; CHECK-BIN-NODEBUG: (func $5 (type $0)
;; CHECK-BIN-NODEBUG-NEXT: (local $0 (i32 i64))
;; CHECK-BIN-NODEBUG-NEXT: (local $0 (tuple i32 i64))
;; CHECK-BIN-NODEBUG-NEXT: (local $1 i32)
;; CHECK-BIN-NODEBUG-NEXT: (local $2 (i32 i64 exnref))
;; CHECK-BIN-NODEBUG-NEXT: (local $2 (tuple i32 i64 exnref))
;; CHECK-BIN-NODEBUG-NEXT: (local $3 i64)
;; CHECK-BIN-NODEBUG-NEXT: (local $4 i32)
;; CHECK-BIN-NODEBUG-NEXT: (block $label$1
Expand Down Expand Up @@ -899,7 +899,7 @@
;; CHECK-BIN-NODEBUG-NEXT: )

;; CHECK-BIN-NODEBUG: (func $7 (type $0)
;; CHECK-BIN-NODEBUG-NEXT: (local $0 (i32 exnref))
;; CHECK-BIN-NODEBUG-NEXT: (local $0 (tuple i32 exnref))
;; CHECK-BIN-NODEBUG-NEXT: (local $1 i32)
;; CHECK-BIN-NODEBUG-NEXT: (block $label$1
;; CHECK-BIN-NODEBUG-NEXT: (drop
Expand Down Expand Up @@ -942,10 +942,10 @@
;; CHECK-BIN-NODEBUG-NEXT: )

;; CHECK-BIN-NODEBUG: (func $8 (type $0)
;; CHECK-BIN-NODEBUG-NEXT: (local $0 (i32 i64 exnref))
;; CHECK-BIN-NODEBUG-NEXT: (local $0 (tuple i32 i64 exnref))
;; CHECK-BIN-NODEBUG-NEXT: (local $1 i64)
;; CHECK-BIN-NODEBUG-NEXT: (local $2 i32)
;; CHECK-BIN-NODEBUG-NEXT: (local $3 (i32 i64))
;; CHECK-BIN-NODEBUG-NEXT: (local $3 (tuple i32 i64))
;; CHECK-BIN-NODEBUG-NEXT: (local $4 i32)
;; CHECK-BIN-NODEBUG-NEXT: (block $label$1
;; CHECK-BIN-NODEBUG-NEXT: (local.set $3
Expand Down
4 changes: 2 additions & 2 deletions test/lit/basic/typed_continuations_resume.wast
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
;; CHECK-TEXT-NEXT: )
;; CHECK-TEXT-NEXT: )
;; CHECK-BIN: (func $go (type $3) (param $x (ref $ct)) (result i32)
;; CHECK-BIN-NEXT: (local $1 (i32 (ref $ct)))
;; CHECK-BIN-NEXT: (local $1 (tuple i32 (ref $ct)))
;; CHECK-BIN-NEXT: (local $2 i32)
;; CHECK-BIN-NEXT: (local.set $1
;; CHECK-BIN-NEXT: (block $label$1 (type $2) (result i32 (ref $ct))
Expand Down Expand Up @@ -135,7 +135,7 @@
;; CHECK-BIN-NODEBUG: (tag $tag$0 (param i32) (result i32))

;; CHECK-BIN-NODEBUG: (func $0 (type $3) (param $0 (ref $1)) (result i32)
;; CHECK-BIN-NODEBUG-NEXT: (local $1 (i32 (ref $1)))
;; CHECK-BIN-NODEBUG-NEXT: (local $1 (tuple i32 (ref $1)))
;; CHECK-BIN-NODEBUG-NEXT: (local $2 i32)
;; CHECK-BIN-NODEBUG-NEXT: (local.set $1
;; CHECK-BIN-NODEBUG-NEXT: (block $label$1 (type $2) (result i32 (ref $1))
Expand Down
8 changes: 4 additions & 4 deletions test/lit/basic/types-function-references.wast
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@
)

;; CHECK-TEXT: (func $type-only-in-tuple-local (type $void)
;; CHECK-TEXT-NEXT: (local $x (i32 (ref null $=>anyref) f64))
;; CHECK-TEXT-NEXT: (local $x (tuple i32 (ref null $=>anyref) f64))
;; CHECK-TEXT-NEXT: (nop)
;; CHECK-TEXT-NEXT: )
;; CHECK-BIN: (func $type-only-in-tuple-local (type $void)
Expand All @@ -184,7 +184,7 @@
;; CHECK-BIN-NEXT: (nop)
;; CHECK-BIN-NEXT: )
(func $type-only-in-tuple-local
(local $x (i32 (ref null $=>anyref) f64))
(local $x (tuple i32 (ref null $=>anyref) f64))
)

;; CHECK-TEXT: (func $type-only-in-tuple-block (type $void)
Expand All @@ -195,7 +195,7 @@
;; CHECK-TEXT-NEXT: )
;; CHECK-TEXT-NEXT: )
;; CHECK-BIN: (func $type-only-in-tuple-block (type $void)
;; CHECK-BIN-NEXT: (local $0 (i32 (ref null $mixed_results) f64))
;; CHECK-BIN-NEXT: (local $0 (tuple i32 (ref null $mixed_results) f64))
;; CHECK-BIN-NEXT: (local $1 (ref null $mixed_results))
;; CHECK-BIN-NEXT: (local $2 i32)
;; CHECK-BIN-NEXT: (local.set $0
Expand Down Expand Up @@ -433,7 +433,7 @@
;; CHECK-BIN-NODEBUG-NEXT: )

;; CHECK-BIN-NODEBUG: (func $8 (type $1)
;; CHECK-BIN-NODEBUG-NEXT: (local $0 (i32 (ref null $0) f64))
;; CHECK-BIN-NODEBUG-NEXT: (local $0 (tuple i32 (ref null $0) f64))
;; CHECK-BIN-NODEBUG-NEXT: (local $1 (ref null $0))
;; CHECK-BIN-NODEBUG-NEXT: (local $2 i32)
;; CHECK-BIN-NODEBUG-NEXT: (local.set $0
Expand Down
8 changes: 4 additions & 4 deletions test/lit/blocktype.wast
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; RTRIP: (func $f1 (type $f1) (result (ref $f1) (ref $f2))
;; RTRIP-NEXT: (local $0 ((ref $f1) (ref $f2)))
;; RTRIP-NEXT: (local $1 ((ref $f1) (ref $f2)))
;; RTRIP-NEXT: (local $0 (tuple (ref $f1) (ref $f2)))
;; RTRIP-NEXT: (local $1 (tuple (ref $f1) (ref $f2)))
;; RTRIP-NEXT: (local.set $1
;; RTRIP-NEXT: (loop $label$1 (type $f1) (result (ref $f1) (ref $f2))
;; RTRIP-NEXT: (local.set $0
Expand Down Expand Up @@ -64,8 +64,8 @@
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; RTRIP: (func $f2 (type $f2) (result (ref $f2) (ref $f1))
;; RTRIP-NEXT: (local $0 ((ref $f2) (ref $f1)))
;; RTRIP-NEXT: (local $1 ((ref $f2) (ref $f1)))
;; RTRIP-NEXT: (local $0 (tuple (ref $f2) (ref $f1)))
;; RTRIP-NEXT: (local $1 (tuple (ref $f2) (ref $f1)))
;; RTRIP-NEXT: (local.set $1
;; RTRIP-NEXT: (loop $label$1 (type $f2) (result (ref $f2) (ref $f1))
;; RTRIP-NEXT: (local.set $0
Expand Down
2 changes: 1 addition & 1 deletion test/lit/ctor-eval/multivalue-local.wast
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

(func $multivalue-local (export "multivalue-local") (result i32)
(local $0 i32)
(local $1 (i32 i32))
(local $1 (tuple i32 i32))

;; We can eval this line. But we will stop evalling at the line after it, the
;; import call. As a result we'll only have a partial evalling of this
Expand Down
2 changes: 1 addition & 1 deletion test/lit/multivalue-stack-ir.wast
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $test
(local $pair (f32 i32))
(local $pair (tuple f32 i32))
(local $f32 f32)
;; Normally this get-set pair would be eliminated by stack IR optimizations,
;; but then the binary writer's tuple optimizations would leave the only the
Expand Down
Loading

0 comments on commit 289c262

Please sign in to comment.