Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions scripts/update_lit_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@

ALL_ITEMS = '|'.join(['type', 'import', 'global', 'memory', 'data', 'table',
'elem', 'tag', 'export', 'start', 'func'])
ITEM_NAME = r'\$?[^\s()]*|"[^\s()]*"'

# Regular names as well as the "declare" in (elem declare ... to get declarative
# segments included in the output.
ITEM_NAME = r'\$[^\s()]*|"[^\s()]*"|declare'

# FIXME: This does not handle nested string contents. For example,
# (data (i32.const 10) "hello(")
# will look unterminated, due to the '(' inside the string. As a result, the
Expand Down Expand Up @@ -152,9 +156,9 @@ def parse_output_fuzz_exec(text):
for line in text.split('\n'):
func = FUZZ_EXEC_FUNC.match(line)
if func:
# Add quotes around the name because that is how it will be parsed
# Add a '$' prefix to the name because that is how it will be parsed
# in the input.
name = f'"{func.group("name")}"'
name = '$' + func.group("name")
items.append((('func', name), [line]))
elif line.startswith('[host limit'):
# Skip mentions of host limits that we hit. This can happen even
Expand Down
2 changes: 1 addition & 1 deletion src/passes/Heap2Local.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
//
// (import "env" "import" (func $import (param i32) (result i32)))
//
// (func "example"
// (func $example
// (local $ref (ref null $boxed-int))
//
// ;; Allocate a boxed integer of 42 and save the reference to it.
Expand Down
87 changes: 52 additions & 35 deletions src/passes/Print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,40 +51,6 @@ bool isFullForced() {
return false;
}

std::ostream& printName(Name name, std::ostream& o) {
assert(name && "Cannot print an empty name");
// We need to quote names if they have tricky chars.
// TODO: This is not spec-compliant since the spec does not support quoted
// identifiers and has a limited set of valid idchars. We need a more robust
// escaping scheme here. Reusing `printEscapedString` is not sufficient,
// either.
if (name.str.find_first_of("()") == std::string_view::npos) {
o << '$' << name.str;
} else {
o << "\"$" << name.str << '"';
}
return o;
}

std::ostream& printMemoryName(Name name, std::ostream& o, Module* wasm) {
if (!wasm || wasm->memories.size() > 1) {
o << ' ';
printName(name, o);
}
return o;
}

std::ostream& printLocal(Index index, Function* func, std::ostream& o) {
Name name;
if (func) {
name = func->getLocalNameOrDefault(index);
}
if (!name) {
name = Name::fromInt(index);
}
return printName(name, o);
}

std::ostream& printEscapedString(std::ostream& os, std::string_view str) {
os << '"';
for (unsigned char c : str) {
Expand Down Expand Up @@ -119,6 +85,56 @@ std::ostream& printEscapedString(std::ostream& os, std::string_view str) {
return os << '"';
}

// TODO: Use unicode rather than char.
bool isIDChar(char c) {
if ('0' <= c && c <= '9') {
return true;
}
if ('A' <= c && c <= 'Z') {
return true;
}
if ('a' <= c && c <= 'z') {
return true;
}
static std::array<char, 23> otherIDChars = {
{'!', '#', '$', '%', '&', '\'', '*', '+', '-', '.', '/', ':',
'<', '=', '>', '?', '@', '\\', '^', '_', '`', '|', '~'}};
return std::find(otherIDChars.begin(), otherIDChars.end(), c) !=
otherIDChars.end();
}

std::ostream& printName(Name name, std::ostream& o) {
assert(name && "Cannot print an empty name");
// We need to quote names if they have tricky chars.
// TODO: This is not spec-compliant since the spec does not yet support quoted
// identifiers and has a limited set of valid idchars.
o << '$';
if (std::all_of(name.str.begin(), name.str.end(), isIDChar)) {
return o << name.str;
} else {
return printEscapedString(o, name.str);
}
}

std::ostream& printMemoryName(Name name, std::ostream& o, Module* wasm) {
if (!wasm || wasm->memories.size() > 1) {
o << ' ';
printName(name, o);
}
return o;
}

std::ostream& printLocal(Index index, Function* func, std::ostream& o) {
Name name;
if (func) {
name = func->getLocalNameOrDefault(index);
}
if (!name) {
name = Name::fromInt(index);
}
return printName(name, o);
}

// Print a name from the type section, if available. Otherwise print the type
// normally.
void printTypeOrName(Type type, std::ostream& o, Module* wasm) {
Expand Down Expand Up @@ -3266,7 +3282,8 @@ void PrintSExpression::visitModule(Module* curr) {
printMedium(o, "(elem");
o << " declare func";
for (auto name : elemDeclareNames) {
o << " $" << name;
o << ' ';
printName(name, o);
}
o << ')' << maybeNewLine;
}
Expand Down
6 changes: 1 addition & 5 deletions src/wasm/wasm-s-parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1073,11 +1073,7 @@ size_t SExpressionWasmBuilder::parseFunctionNames(Element& s,
Name& exportName) {
size_t i = 1;
while (i < s.size() && i < 3 && s[i]->isStr()) {
if (s[i]->quoted()) {
// an export name
exportName = s[i]->str();
i++;
} else if (s[i]->dollared()) {
if (s[i]->dollared()) {
name = s[i]->str();
i++;
} else {
Expand Down
28 changes: 14 additions & 14 deletions test/binaryen.js/kitchen-sink.js.txt
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,14 @@ getExpressionInfo(tuple[3])={"id":14,"type":5,"value":3.7}
(data $0 (i32.const 10) "hello, world")
(data $1 "I am passive")
(table $t0 1 funcref)
(elem $e0 (i32.const 0) "$kitchen()sinker")
(elem $e0 (i32.const 0) $"kitchen()sinker")
(tag $a-tag (param i32))
(export "mem" (memory $0))
(export "kitchen_sinker" (func "$kitchen()sinker"))
(export "kitchen_sinker" (func $"kitchen()sinker"))
(export "a-global-exp" (global $a-global))
(export "a-tag-exp" (tag $a-tag))
(start $starter)
(func "$kitchen()sinker" (type $0) (param $0 i32) (param $1 i64) (param $2 f32) (param $3 f64) (result i32)
(func $"kitchen()sinker" (type $0) (param $0 i32) (param $1 i64) (param $2 f32) (param $3 f64) (result i32)
(local $4 i32)
(block $the-body (result i32)
(block $the-nothing
Expand Down Expand Up @@ -1979,7 +1979,7 @@ getExpressionInfo(tuple[3])={"id":14,"type":5,"value":3.7}
)
(drop
(i32.eqz
(call "$kitchen()sinker"
(call $"kitchen()sinker"
(i32.const 13)
(i64.const 37)
(f32.const 1.2999999523162842)
Expand Down Expand Up @@ -2057,7 +2057,7 @@ getExpressionInfo(tuple[3])={"id":14,"type":5,"value":3.7}
(return
(i32.const 1337)
)
(return_call "$kitchen()sinker"
(return_call $"kitchen()sinker"
(i32.const 13)
(i64.const 37)
(f32.const 1.2999999523162842)
Expand All @@ -2082,13 +2082,13 @@ getExpressionInfo(tuple[3])={"id":14,"type":5,"value":3.7}
)
(drop
(ref.is_null
(ref.func "$kitchen()sinker")
(ref.func $"kitchen()sinker")
)
)
(drop
(select (result funcref)
(ref.null nofunc)
(ref.func "$kitchen()sinker")
(ref.func $"kitchen()sinker")
(i32.const 1)
)
)
Expand Down Expand Up @@ -2247,14 +2247,14 @@ getExpressionInfo(tuple[3])={"id":14,"type":5,"value":3.7}
(data $0 (i32.const 10) "hello, world")
(data $1 "I am passive")
(table $t0 1 funcref)
(elem $e0 (i32.const 0) "$kitchen()sinker")
(elem $e0 (i32.const 0) $"kitchen()sinker")
(tag $a-tag (param i32))
(export "mem" (memory $0))
(export "kitchen_sinker" (func "$kitchen()sinker"))
(export "kitchen_sinker" (func $"kitchen()sinker"))
(export "a-global-exp" (global $a-global))
(export "a-tag-exp" (tag $a-tag))
(start $starter)
(func "$kitchen()sinker" (type $0) (param $0 i32) (param $1 i64) (param $2 f32) (param $3 f64) (result i32)
(func $"kitchen()sinker" (type $0) (param $0 i32) (param $1 i64) (param $2 f32) (param $3 f64) (result i32)
(local $4 i32)
(block $the-body (result i32)
(block $the-nothing
Expand Down Expand Up @@ -4083,7 +4083,7 @@ getExpressionInfo(tuple[3])={"id":14,"type":5,"value":3.7}
)
(drop
(i32.eqz
(call "$kitchen()sinker"
(call $"kitchen()sinker"
(i32.const 13)
(i64.const 37)
(f32.const 1.2999999523162842)
Expand Down Expand Up @@ -4161,7 +4161,7 @@ getExpressionInfo(tuple[3])={"id":14,"type":5,"value":3.7}
(return
(i32.const 1337)
)
(return_call "$kitchen()sinker"
(return_call $"kitchen()sinker"
(i32.const 13)
(i64.const 37)
(f32.const 1.2999999523162842)
Expand All @@ -4186,13 +4186,13 @@ getExpressionInfo(tuple[3])={"id":14,"type":5,"value":3.7}
)
(drop
(ref.is_null
(ref.func "$kitchen()sinker")
(ref.func $"kitchen()sinker")
)
)
(drop
(select (result funcref)
(ref.null nofunc)
(ref.func "$kitchen()sinker")
(ref.func $"kitchen()sinker")
(i32.const 1)
)
)
Expand Down
4 changes: 2 additions & 2 deletions test/ctor-eval/gc-2.wast
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@
)
)

(func "test1"
(func $test (export "test1")
(global.set $global2
(global.get $global3)
)
)

(func "keepalive" (result i32)
(func $keepalive (export "keepalive") (result i32)
(select
(struct.get $struct 0
(ref.cast (ref $struct)
Expand Down
4 changes: 2 additions & 2 deletions test/ctor-eval/gc-2.wast.out
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
(i32.const 1337)
))
(global $global1 (ref any) (global.get $ctor-eval$global))
(export "keepalive" (func $1))
(func $1 (type $1) (result i32)
(export "keepalive" (func $keepalive))
(func $keepalive (type $1) (result i32)
(select
(struct.get $struct 0
(ref.cast (ref $struct)
Expand Down
5 changes: 2 additions & 3 deletions test/ctor-eval/gc-array.wast
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@
)
)

(func "test1"
(func $test1 (export "test1")
(array.set $array
(global.get $global2)
(i32.const 1)
(i32.const 1337)
)
)

(func "keepalive" (result i32)
(func $keepalive (export "keepalive") (result i32)
(i32.add
(array.get $array
(global.get $global1)
Expand All @@ -42,4 +42,3 @@
)
)
)

4 changes: 2 additions & 2 deletions test/ctor-eval/gc-array.wast.out
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
(i32.const 42)
(i32.const 1337)
))
(export "keepalive" (func $1))
(func $1 (type $1) (result i32)
(export "keepalive" (func $keepalive))
(func $keepalive (type $1) (result i32)
(i32.add
(array.get $array
(global.get $global1)
Expand Down
4 changes: 2 additions & 2 deletions test/ctor-eval/gc.wast
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
;; so a new (immutable) global will appear, and we will read from it.
(global $global2 (mut (ref null $struct)) (ref.null $struct))

(func "test1"
(func $test1 (export "test1")
;; The locals will be optimized into a single non-nullable one by the
;; optimizer.
(local $temp1 (ref null $struct))
Expand Down Expand Up @@ -51,7 +51,7 @@
(call $import (local.get $temp2))
)

(func "keepalive" (result i32)
(func $keepalive (export "keepalive") (result i32)
(i32.add
(struct.get $struct 0
(global.get $global1)
Expand Down
8 changes: 4 additions & 4 deletions test/ctor-eval/gc.wast.out
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
(global $ctor-eval$global_4 (ref $struct) (struct.new $struct
(i32.const 99)
))
(export "test1" (func $0_3))
(export "keepalive" (func $1))
(func $1 (type $2) (result i32)
(export "test1" (func $test1_3))
(export "keepalive" (func $keepalive))
(func $keepalive (type $2) (result i32)
(i32.add
(struct.get $struct 0
(global.get $global1)
Expand All @@ -26,7 +26,7 @@
)
)
)
(func $0_3 (type $3)
(func $test1_3 (type $3)
(local $0 (ref $struct))
(local.set $0
(global.get $ctor-eval$global_4)
Expand Down
6 changes: 3 additions & 3 deletions test/ctor-eval/ignore-external-input-gc.wast
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
(global $global1 (mut i32) (i32.const 10))
(global $global2 (mut i32) (i32.const 20))

(func "test1" (param $any (ref null any))
(func $test1 (export "test1") (param $any (ref null any))
;; This is ok to call: when ignoring external input we assume 0 for the
;; parameters, and this parameter is nullable.
(drop
Expand All @@ -13,7 +13,7 @@
)
)

(func "test2" (param $any (ref any))
(func $test2 (export "test2") (param $any (ref any))
;; This is *not* ok to call: when ignoring external input we assume 0 for
;; the parameters, and this parameter is not nullable.
(drop
Expand All @@ -24,7 +24,7 @@
)
)

(func "keepalive" (result i32)
(func $keepalive (export "keepalive") (result i32)
(i32.add
(global.get $global1)
(global.get $global2)
Expand Down
Loading