Skip to content

Commit b232033

Browse files
authored
Don't include $ with names unless outputting to wat format (#2506)
The `$` is not actually part of the name, its the marker that starts a name in the wat format. It can be confusing to see it show up when doing `cerr << name`, for example. This change has Print.cpp add the `$` which seem like the right place to do this. Plus it revealed a bunch of places where were not calling printName to escape all the names we were printing.
1 parent 6f55457 commit b232033

14 files changed

Lines changed: 608 additions & 600 deletions

src/passes/Print.cpp

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,22 @@ static bool isFullForced() {
3737
static std::ostream& printName(Name name, std::ostream& o) {
3838
// we need to quote names if they have tricky chars
3939
if (!name.str || !strpbrk(name.str, "()")) {
40-
o << name;
40+
o << '$' << name.str;
4141
} else {
42-
o << '"' << name << '"';
42+
o << "\"$" << name.str << '"';
4343
}
4444
return o;
4545
}
4646

47-
static Name printableLocal(Index index, Function* func) {
47+
static std::ostream& printLocal(Index index, Function* func, std::ostream& o) {
4848
Name name;
4949
if (func) {
5050
name = func->getLocalNameOrDefault(index);
5151
}
52-
if (!name.is()) {
52+
if (!name) {
5353
name = Name::fromInt(index);
5454
}
55-
return name;
55+
return printName(name, o);
5656
}
5757

5858
// Printing "unreachable" as a instruction prefix type is not valid in wasm text
@@ -88,7 +88,8 @@ struct PrintExpressionContents
8888
void visitLoop(Loop* curr) {
8989
printMedium(o, "loop");
9090
if (curr->name.is()) {
91-
o << ' ' << curr->name;
91+
o << ' ';
92+
printName(curr->name, o);
9293
}
9394
if (curr->type.isConcrete()) {
9495
o << ' ' << ResultType(curr->type);
@@ -105,9 +106,11 @@ struct PrintExpressionContents
105106
void visitSwitch(Switch* curr) {
106107
printMedium(o, "br_table");
107108
for (auto& t : curr->targets) {
108-
o << ' ' << t;
109+
o << ' ';
110+
printName(t, o);
109111
}
110-
o << ' ' << curr->default_;
112+
o << ' ';
113+
printName(curr->default_, o);
111114
}
112115
void visitCall(Call* curr) {
113116
if (curr->isReturn) {
@@ -123,18 +126,19 @@ struct PrintExpressionContents
123126
} else {
124127
printMedium(o, "call_indirect (type ");
125128
}
126-
o << curr->fullType << ')';
129+
printName(curr->fullType, o) << ')';
127130
}
128131
void visitLocalGet(LocalGet* curr) {
129-
printMedium(o, "local.get ") << printableLocal(curr->index, currFunction);
132+
printMedium(o, "local.get ");
133+
printLocal(curr->index, currFunction, o);
130134
}
131135
void visitLocalSet(LocalSet* curr) {
132136
if (curr->isTee()) {
133137
printMedium(o, "local.tee ");
134138
} else {
135139
printMedium(o, "local.set ");
136140
}
137-
o << printableLocal(curr->index, currFunction);
141+
printLocal(curr->index, currFunction, o);
138142
}
139143
void visitGlobalGet(GlobalGet* curr) {
140144
printMedium(o, "global.get ");
@@ -1868,7 +1872,8 @@ struct PrintSExpression : public OverriddenVisitor<PrintSExpression> {
18681872
void visitFunctionType(FunctionType* curr, Name* internalName = nullptr) {
18691873
o << "(func";
18701874
if (internalName) {
1871-
o << ' ' << *internalName;
1875+
o << ' ';
1876+
printName(*internalName, o);
18721877
}
18731878
if (curr->params.size() > 0) {
18741879
o << maybeSpace;
@@ -1989,14 +1994,15 @@ struct PrintSExpression : public OverriddenVisitor<PrintSExpression> {
19891994
o << " (; has Stack IR ;)";
19901995
}
19911996
if (curr->type.is()) {
1992-
o << maybeSpace << "(type " << curr->type << ')';
1997+
o << maybeSpace << "(type ";
1998+
printName(curr->type, o) << ')';
19931999
}
19942000
if (curr->params.size() > 0) {
19952001
for (size_t i = 0; i < curr->params.size(); i++) {
19962002
o << maybeSpace;
19972003
o << '(';
1998-
printMinor(o, "param ") << printableLocal(i, currFunction) << ' '
1999-
<< curr->getLocalType(i) << ')';
2004+
printMinor(o, "param ");
2005+
printLocal(i, currFunction, o) << ' ' << curr->getLocalType(i) << ')';
20002006
}
20012007
}
20022008
if (curr->result != none) {
@@ -2007,8 +2013,8 @@ struct PrintSExpression : public OverriddenVisitor<PrintSExpression> {
20072013
for (size_t i = curr->getVarIndexBase(); i < curr->getNumLocals(); i++) {
20082014
doIndent(o, indent);
20092015
o << '(';
2010-
printMinor(o, "local ") << printableLocal(i, currFunction) << ' '
2011-
<< curr->getLocalType(i) << ')';
2016+
printMinor(o, "local ");
2017+
printLocal(i, currFunction, o) << ' ' << curr->getLocalType(i) << ')';
20122018
o << maybeNewLine;
20132019
}
20142020
// Print the body.
@@ -2232,7 +2238,8 @@ struct PrintSExpression : public OverriddenVisitor<PrintSExpression> {
22322238
if (curr->start.is()) {
22332239
doIndent(o, indent);
22342240
o << '(';
2235-
printMedium(o, "start") << ' ' << curr->start << ')';
2241+
printMedium(o, "start") << ' ';
2242+
printName(curr->start, o) << ')';
22362243
o << maybeNewLine;
22372244
}
22382245
ModuleUtils::iterDefinedFunctions(

src/support/name.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ struct Name : public cashew::IString {
4141

4242
friend std::ostream& operator<<(std::ostream& o, Name name) {
4343
if (name.str) {
44-
// reference interpreter requires we prefix all names
45-
return o << '$' << name.str;
44+
return o << name.str;
4645
} else {
4746
return o << "(null Name)";
4847
}

test/binaryen.js/fatal.js.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Fatal: Module::addFunctionType: $vI already exists
1+
Fatal: Module::addFunctionType: vI already exists

test/binaryen.js/kitchen-sink.js.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3956,7 +3956,7 @@ module loaded from binary form:
39563956
)
39573957
)
39583958

3959-
[wasm-validator error in function $func] i32 != i64: local.set type must match function, on
3959+
[wasm-validator error in function func] i32 != i64: local.set type must match function, on
39603960
[none] (local.set $0
39613961
[i64] (i64.const 1234)
39623962
)
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
[wasm-validator error in function $test] unexpected false: global.get name must be valid, on
1+
[wasm-validator error in function test] unexpected false: global.get name must be valid, on
22
[i32] (global.get $missing)
33
0
4-
[wasm-validator error in function $test] unexpected false: local.get index must be small enough, on
4+
[wasm-validator error in function test] unexpected false: local.get index must be small enough, on
55
[i32] (local.get $0)
6-
[wasm-validator error in function $test] unexpected false: local.get must have proper type, on
6+
[wasm-validator error in function test] unexpected false: local.get must have proper type, on
77
[i32] (local.get $0)
88
0

0 commit comments

Comments
 (0)