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
86 changes: 40 additions & 46 deletions src/passes/Print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,25 +229,15 @@ void TypeNamePrinter::print(const Rtt& rtt) {

} // anonymous namespace

// Unlike the default format, tuple types in s-expressions should not have
// commas.
struct SExprType {
Type type;
SExprType(Type type) : type(type){};
};

static std::ostream& printSExprType(std::ostream& o,
const SExprType& sType,
Module* wasm = nullptr) {
Type type = sType.type;
static std::ostream& printType(std::ostream& o, Type type, Module* wasm) {
if (type.isBasic()) {
o << type;
} else if (type.isTuple()) {
o << '(';
auto sep = "";
for (const auto& t : type) {
o << sep;
printSExprType(o, t, wasm);
printType(o, t, wasm);
sep = " ";
}
o << ')';
Expand All @@ -272,32 +262,35 @@ static std::ostream& printSExprType(std::ostream& o,
return o;
}

// TODO: try to simplify or even remove this, as we may be able to do the same
// things with SExprType
struct ResultTypeName {
Type type;
ResultTypeName(Type type) : type(type) {}
};

std::ostream& printResultTypeName(std::ostream& os,
ResultTypeName typeName,
Module* wasm = nullptr) {
auto type = typeName.type;
os << "(result ";
static std::ostream& printPrefixedTypes(std::ostream& o,
const char* prefix,
Type type,
Module* wasm) {
o << '(' << prefix;
if (type == Type::none) {
return o << ')';
}
if (type.isTuple()) {
// Tuple types are not printed in parens, we can just emit them one after
// the other in the same list as the "result".
auto sep = "";
for (auto t : type) {
os << sep;
sep = " ";
printSExprType(os, t, wasm);
o << ' ';
printType(o, t, wasm);
}
} else {
printSExprType(os, type, wasm);
o << ' ';
printType(o, type, wasm);
}
os << ')';
return os;
o << ')';
return o;
}

static std::ostream& printResultType(std::ostream& o, Type type, Module* wasm) {
return printPrefixedTypes(o, "result", type, wasm);
}

static std::ostream& printParamType(std::ostream& o, Type type, Module* wasm) {
return printPrefixedTypes(o, "param", type, wasm);
}

// Generic processing of a struct's field, given an optional module. Calls func
Expand Down Expand Up @@ -352,14 +345,14 @@ struct PrintExpressionContents
}
if (curr->type.isConcrete()) {
o << ' ';
printResultTypeName(o, curr->type, wasm);
printResultType(o, curr->type, wasm);
}
}
void visitIf(If* curr) {
printMedium(o, "if");
if (curr->type.isConcrete()) {
o << ' ';
printResultTypeName(o, curr->type, wasm);
printResultType(o, curr->type, wasm);
}
}
void visitLoop(Loop* curr) {
Expand All @@ -370,7 +363,7 @@ struct PrintExpressionContents
}
if (curr->type.isConcrete()) {
o << ' ';
printResultTypeName(o, curr->type, wasm);
printResultType(o, curr->type, wasm);
}
}
void visitBreak(Break* curr) {
Expand Down Expand Up @@ -1840,7 +1833,7 @@ struct PrintExpressionContents
restoreNormalColor(o);
if (curr->type.isRef()) {
o << ' ';
printResultTypeName(o, curr->type, wasm);
printResultType(o, curr->type, wasm);
}
}
void visitDrop(Drop* curr) { printMedium(o, "drop"); }
Expand Down Expand Up @@ -1881,7 +1874,8 @@ struct PrintExpressionContents
printName(curr->name, o);
}
if (curr->type.isConcrete()) {
o << ' ' << ResultType(curr->type);
o << ' ';
printResultType(o, curr->type, wasm);
}
}
void visitThrow(Throw* curr) {
Expand Down Expand Up @@ -2401,7 +2395,7 @@ struct PrintSExpression : public UnifiedExpressionVisitor<PrintSExpression> {
auto sep = "";
for (auto type : curr.params) {
o << sep;
printSExprType(o, type, currModule);
printType(o, type, currModule);
sep = " ";
}
o << ')';
Expand All @@ -2412,7 +2406,7 @@ struct PrintSExpression : public UnifiedExpressionVisitor<PrintSExpression> {
auto sep = "";
for (auto type : curr.results) {
o << sep;
printSExprType(o, type, currModule);
printType(o, type, currModule);
sep = " ";
}
o << ')';
Expand All @@ -2432,7 +2426,7 @@ struct PrintSExpression : public UnifiedExpressionVisitor<PrintSExpression> {
WASM_UNREACHABLE("invalid packed type");
}
} else {
printSExprType(o, field.type, currModule);
printType(o, field.type, currModule);
}
if (field.mutable_) {
o << ')';
Expand Down Expand Up @@ -2513,9 +2507,9 @@ struct PrintSExpression : public UnifiedExpressionVisitor<PrintSExpression> {
void emitGlobalType(Global* curr) {
if (curr->mutable_) {
o << "(mut ";
printSExprType(o, curr->type, currModule) << ')';
printType(o, curr->type, currModule) << ')';
} else {
printSExprType(o, curr->type, currModule);
printType(o, curr->type, currModule);
}
}
void visitImportedGlobal(Global* curr) {
Expand Down Expand Up @@ -2576,21 +2570,21 @@ struct PrintSExpression : public UnifiedExpressionVisitor<PrintSExpression> {
printMinor(o, "param ");
printLocal(i, currFunction, o);
o << ' ';
printSExprType(o, param, currModule) << ')';
printType(o, param, currModule) << ')';
++i;
}
}
if (curr->sig.results != Type::none) {
o << maybeSpace;
printResultTypeName(o, curr->sig.results, currModule);
printResultType(o, curr->sig.results, currModule);
}
incIndent();
for (size_t i = curr->getVarIndexBase(); i < curr->getNumLocals(); i++) {
doIndent(o, indent);
o << '(';
printMinor(o, "local ");
printLocal(i, currFunction, o) << ' ';
printSExprType(o, curr->getLocalType(i), currModule) << ')';
printType(o, curr->getLocalType(i), currModule) << ')';
o << maybeNewLine;
}
// Print the body.
Expand Down Expand Up @@ -2641,7 +2635,7 @@ struct PrintSExpression : public UnifiedExpressionVisitor<PrintSExpression> {
o << "(event ";
printName(curr->name, o);
o << maybeSpace << "(attr " << curr->attribute << ')' << maybeSpace;
o << ParamType(curr->sig.params);
printParamType(o, curr->sig.params, currModule);
o << "))";
o << maybeNewLine;
}
Expand All @@ -2651,7 +2645,7 @@ struct PrintSExpression : public UnifiedExpressionVisitor<PrintSExpression> {
printMedium(o, "event ");
printName(curr->name, o);
o << maybeSpace << "(attr " << curr->attribute << ')' << maybeSpace;
o << ParamType(curr->sig.params);
printParamType(o, curr->sig.params, currModule);
o << ")" << maybeNewLine;
}
void printTableHeader(Table* curr) {
Expand Down
18 changes: 1 addition & 17 deletions src/wasm-type.h
Original file line number Diff line number Diff line change
Expand Up @@ -281,20 +281,6 @@ class Type {
const Type& operator[](size_t i) const;
};

// Wrapper type for formatting types as "(param i32 i64 f32)"
struct ParamType {
Type type;
ParamType(Type type) : type(type) {}
std::string toString() const;
};

// Wrapper type for formatting types as "(result i32 i64 f32)"
struct ResultType {
Type type;
ResultType(Type type) : type(type) {}
std::string toString() const;
};

class HeapType {
// Unlike `Type`, which represents the types of values on the WebAssembly
// stack, `HeapType` is used to describe the structures that reference types
Expand Down Expand Up @@ -509,14 +495,12 @@ struct TypeBuilder {
};

std::ostream& operator<<(std::ostream&, Type);
std::ostream& operator<<(std::ostream&, ParamType);
std::ostream& operator<<(std::ostream&, ResultType);
std::ostream& operator<<(std::ostream&, HeapType);
std::ostream& operator<<(std::ostream&, Tuple);
std::ostream& operator<<(std::ostream&, Signature);
std::ostream& operator<<(std::ostream&, Field);
std::ostream& operator<<(std::ostream&, Struct);
std::ostream& operator<<(std::ostream&, Array);
std::ostream& operator<<(std::ostream&, HeapType);
std::ostream& operator<<(std::ostream&, Rtt);

} // namespace wasm
Expand Down
Loading