summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/passes/Print.cpp36
-rw-r--r--src/wasm-s-parser.h1
-rw-r--r--test/example/c-api-kitchen-sink.c6
-rw-r--r--test/example/c-api-kitchen-sink.txt8
4 files changed, 35 insertions, 16 deletions
diff --git a/src/passes/Print.cpp b/src/passes/Print.cpp
index 2408ed1b7..5fe7f93ac 100644
--- a/src/passes/Print.cpp
+++ b/src/passes/Print.cpp
@@ -78,6 +78,16 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
return name;
}
+ std::ostream& printName(Name name) {
+ // we need to quote names if they have tricky chars
+ if (strpbrk(name.str, "()")) {
+ o << '"' << name << '"';
+ } else {
+ o << name;
+ }
+ return o;
+ }
+
void visitBlock(Block *curr) {
// special-case Block, because Block nesting (in their first element) can be incredibly deep
std::vector<Block*> stack;
@@ -86,7 +96,8 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
stack.push_back(curr);
printOpening(o, "block");
if (curr->name.is()) {
- o << ' ' << curr->name;
+ o << ' ';
+ printName(curr->name);
}
incIndent();
if (curr->list.size() > 0 && curr->list[0]->is<Block>()) {
@@ -157,10 +168,12 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
}
void visitBreak(Break *curr) {
if (curr->condition) {
- printOpening(o, "br_if ") << curr->name;
+ printOpening(o, "br_if ");
+ printName(curr->name);
incIndent();
} else {
- printOpening(o, "br ") << curr->name;
+ printOpening(o, "br ");
+ printName(curr->name);
if (!curr->value || curr->value->is<Nop>()) {
// avoid a new line just for the parens
o << ")";
@@ -188,7 +201,7 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
template<typename CallBase>
void printCallBody(CallBase* curr) {
- o << curr->target;
+ printName(curr->target);
if (curr->operands.size() > 0) {
incIndent();
for (auto operand : curr->operands) {
@@ -474,7 +487,8 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
// Module-level visitors
void visitFunctionType(FunctionType *curr, bool full=false) {
if (full) {
- printOpening(o, "type") << ' ' << curr->name << " (func";
+ printOpening(o, "type") << ' ';
+ printName(curr->name) << " (func";
}
if (curr->params.size() > 0) {
o << maybeSpace;
@@ -493,7 +507,8 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
}
}
void visitImport(Import *curr) {
- printOpening(o, "import ") << curr->name << ' ';
+ printOpening(o, "import ");
+ printName(curr->name) << ' ';
printText(o, curr->module.str) << ' ';
printText(o, curr->base.str);
if (curr->type) visitFunctionType(curr->type);
@@ -501,11 +516,13 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
}
void visitExport(Export *curr) {
printOpening(o, "export ");
- printText(o, curr->name.str) << ' ' << curr->value << ')';
+ printText(o, curr->name.str) << ' ';
+ printName(curr->value) << ')';
}
void visitFunction(Function *curr) {
currFunction = curr;
- printOpening(o, "func ", true) << curr->name;
+ printOpening(o, "func ", true);
+ printName(curr->name);
if (curr->type.is()) {
o << maybeSpace << "(type " << curr->type << ')';
}
@@ -540,7 +557,8 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
void visitTable(Table *curr) {
printOpening(o, "table");
for (auto name : curr->names) {
- o << ' ' << name;
+ o << ' ';
+ printName(name);
}
o << ')';
}
diff --git a/src/wasm-s-parser.h b/src/wasm-s-parser.h
index b31b727b7..a48ee1a5f 100644
--- a/src/wasm-s-parser.h
+++ b/src/wasm-s-parser.h
@@ -78,6 +78,7 @@ public:
}
Element* operator[](unsigned i) {
+ if (i >= list().size()) throw ParseException("expected more elements in list", line, col);
return list()[i];
}
diff --git a/test/example/c-api-kitchen-sink.c b/test/example/c-api-kitchen-sink.c
index fa245c102..444ed9384 100644
--- a/test/example/c-api-kitchen-sink.c
+++ b/test/example/c-api-kitchen-sink.c
@@ -166,7 +166,7 @@ void test_core() {
BinaryenSwitch(module, switchValueNames, 1, "the-value", makeInt32(module, 0), makeInt32(module, 1)),
BinaryenSwitch(module, switchBodyNames, 1, "the-nothing", makeInt32(module, 2), NULL),
BinaryenUnary(module, BinaryenEqZInt32(), // check the output type of the call node
- BinaryenCall(module, "kitchen-sinker", callOperands4, 4, BinaryenInt32())
+ BinaryenCall(module, "kitchen()sinker", callOperands4, 4, BinaryenInt32())
),
BinaryenUnary(module, BinaryenEqZInt32(), // check the output type of the call node
BinaryenUnary(module,
@@ -202,7 +202,7 @@ void test_core() {
// Create the function
BinaryenType localTypes[] = { BinaryenInt32() };
- BinaryenFunctionRef sinker = BinaryenAddFunction(module, "kitchen-sinker", iiIfF, localTypes, 1, body);
+ BinaryenFunctionRef sinker = BinaryenAddFunction(module, "kitchen()sinker", iiIfF, localTypes, 1, body);
// Imports
@@ -212,7 +212,7 @@ void test_core() {
// Exports
- BinaryenAddExport(module, "kitchen-sinker", "kitchen_sinker");
+ BinaryenAddExport(module, "kitchen()sinker", "kitchen_sinker");
// Function table. One per module
BinaryenFunctionRef functions[] = { sinker };
diff --git a/test/example/c-api-kitchen-sink.txt b/test/example/c-api-kitchen-sink.txt
index 9e2e62aad..42610c2cc 100644
--- a/test/example/c-api-kitchen-sink.txt
+++ b/test/example/c-api-kitchen-sink.txt
@@ -17,9 +17,9 @@ BinaryenFloat64: 4
(type $v (func))
(type $3 (func))
(import $an-imported "module" "base" (param i32 f64) (result f32))
- (export "kitchen_sinker" $kitchen-sinker)
- (table $kitchen-sinker)
- (func $kitchen-sinker (type $iiIfF) (param $0 i32) (param $1 i64) (param $2 f32) (param $3 f64) (result i32)
+ (export "kitchen_sinker" "$kitchen()sinker")
+ (table "$kitchen()sinker")
+ (func "$kitchen()sinker" (type $iiIfF) (param $0 i32) (param $1 i64) (param $2 f32) (param $3 f64) (result i32)
(local $4 i32)
(block $the-body
(block $the-nothing
@@ -299,7 +299,7 @@ BinaryenFloat64: 4
(i32.const 2)
)
(i32.eqz
- (call $kitchen-sinker
+ (call "$kitchen()sinker"
(i32.const 13)
(i64.const 37)
(f32.const 1.2999999523162842)