summaryrefslogtreecommitdiff
path: root/src/passes/Print.cpp
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2018-09-19 15:50:30 -0700
committerGitHub <noreply@github.com>2018-09-19 15:50:30 -0700
commitfe88b47749115009da0447e340cbdc86edf30984 (patch)
tree7dfd9aba7086c8aa6dff4877ac1ee3b9d78bc5ce /src/passes/Print.cpp
parenta53356ab155a7d8c2f334dc9a3c1432bacbc78fe (diff)
downloadbinaryen-fe88b47749115009da0447e340cbdc86edf30984.tar.gz
binaryen-fe88b47749115009da0447e340cbdc86edf30984.tar.bz2
binaryen-fe88b47749115009da0447e340cbdc86edf30984.zip
Unify imported and non-imported things (#1678)
Fixes #1649 This moves us to a single object for functions, which can be imported or nor, and likewise for globals (as a result, GetGlobals do not need to check if the global is imported or not, etc.). All imported things now inherit from Importable, which has the module and base of the import, and if they are set then it is an import. For convenient iteration, there are a few helpers like ModuleUtils::iterDefinedGlobals(wasm, [&](Global* global) { .. use global .. }); as often iteration only cares about imported or defined (non-imported) things.
Diffstat (limited to 'src/passes/Print.cpp')
-rw-r--r--src/passes/Print.cpp133
1 files changed, 83 insertions, 50 deletions
diff --git a/src/passes/Print.cpp b/src/passes/Print.cpp
index 77562af4b..3ab1cc75d 100644
--- a/src/passes/Print.cpp
+++ b/src/passes/Print.cpp
@@ -109,10 +109,6 @@ struct PrintExpressionContents : public Visitor<PrintExpressionContents> {
printMedium(o, "call ");
printName(curr->target, o);
}
- void visitCallImport(CallImport* curr) {
- printMedium(o, "call ");
- printName(curr->target, o);
- }
void visitCallIndirect(CallIndirect* curr) {
printMedium(o, "call_indirect (type ") << curr->fullType << ')';
}
@@ -478,6 +474,7 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
}
void decIndent() {
if (!minify) {
+ assert(indent > 0);
indent--;
doIndent(o, indent);
}
@@ -636,11 +633,6 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
PrintExpressionContents(currFunction, o).visit(curr);
printCallOperands(curr);
}
- void visitCallImport(CallImport* curr) {
- o << '(';
- PrintExpressionContents(currFunction, o).visit(curr);
- printCallOperands(curr);
- }
void visitCallIndirect(CallIndirect* curr) {
o << '(';
PrintExpressionContents(currFunction, o).visit(curr);
@@ -818,20 +810,6 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
}
o << ")";
}
- void visitImport(Import* curr) {
- o << '(';
- printMedium(o, "import ");
- printText(o, curr->module.str) << ' ';
- printText(o, curr->base.str) << ' ';
- switch (curr->kind) {
- case ExternalKind::Function: if (curr->functionType.is()) visitFunctionType(currModule->getFunctionType(curr->functionType), &curr->name); break;
- case ExternalKind::Table: printTableHeader(&currModule->table); break;
- case ExternalKind::Memory: printMemoryHeader(&currModule->memory); break;
- case ExternalKind::Global: o << "(global " << curr->name << ' ' << printType(curr->globalType) << ")"; break;
- default: WASM_UNREACHABLE();
- }
- o << ')';
- }
void visitExport(Export* curr) {
o << '(';
printMedium(o, "export ");
@@ -846,19 +824,66 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
o << ' ';
printName(curr->value, o) << "))";
}
+ void emitImportHeader(Importable* curr) {
+ printMedium(o, "import ");
+ printText(o, curr->module.str) << ' ';
+ printText(o, curr->base.str) << ' ';
+ }
void visitGlobal(Global* curr) {
- o << '(';
- printMedium(o, "global ");
- printName(curr->name, o) << ' ';
+ if (curr->imported()) {
+ visitImportedGlobal(curr);
+ } else {
+ visitDefinedGlobal(curr);
+ }
+ }
+ void emitGlobalType(Global* curr) {
if (curr->mutable_) {
- o << "(mut " << printType(curr->type) << ") ";
+ o << "(mut " << printType(curr->type) << ')';
} else {
- o << printType(curr->type) << ' ';
+ o << printType(curr->type);
}
+ }
+ void visitImportedGlobal(Global* curr) {
+ doIndent(o, indent);
+ o << '(';
+ emitImportHeader(curr);
+ o << "(global ";
+ printName(curr->name, o) << ' ';
+ emitGlobalType(curr);
+ o << "))" << maybeNewLine;
+ }
+ void visitDefinedGlobal(Global* curr) {
+ doIndent(o, indent);
+ o << '(';
+ printMedium(o, "global ");
+ printName(curr->name, o) << ' ';
+ emitGlobalType(curr);
+ o << ' ';
visit(curr->init);
o << ')';
+ o << maybeNewLine;
}
void visitFunction(Function* curr) {
+ if (curr->imported()) {
+ visitImportedFunction(curr);
+ } else {
+ visitDefinedFunction(curr);
+ }
+ }
+ void visitImportedFunction(Function* curr) {
+ doIndent(o, indent);
+ currFunction = curr;
+ lastPrintedLocation = { 0, 0, 0 };
+ o << '(';
+ emitImportHeader(curr);
+ if (curr->type.is()) {
+ visitFunctionType(currModule->getFunctionType(curr->type), &curr->name);
+ }
+ o << ')';
+ o << maybeNewLine;
+ }
+ void visitDefinedFunction(Function* curr) {
+ doIndent(o, indent);
currFunction = curr;
lastPrintedLocation = { 0, 0, 0 };
if (currFunction->prologLocation.size()) {
@@ -927,6 +952,7 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
} else {
decIndent();
}
+ o << maybeNewLine;
}
void printTableHeader(Table* curr) {
o << '(';
@@ -937,8 +963,13 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
}
void visitTable(Table* curr) {
if (!curr->exists) return;
- // if table wasn't imported, declare it
- if (!curr->imported) {
+ if (curr->imported()) {
+ doIndent(o, indent);
+ o << '(';
+ emitImportHeader(curr);
+ printTableHeader(&currModule->table);
+ o << ')' << maybeNewLine;
+ } else {
doIndent(o, indent);
printTableHeader(curr);
o << maybeNewLine;
@@ -954,7 +985,7 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
o << ' ';
printName(name, o);
}
- o << ")\n";
+ o << ')' << maybeNewLine;
}
}
void printMemoryHeader(Memory* curr) {
@@ -972,8 +1003,13 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
}
void visitMemory(Memory* curr) {
if (!curr->exists) return;
- // if memory wasn't imported, declare it
- if (!curr->imported) {
+ if (curr->imported()) {
+ doIndent(o, indent);
+ o << '(';
+ emitImportHeader(curr);
+ printMemoryHeader(&currModule->memory);
+ o << ')' << maybeNewLine;
+ } else {
doIndent(o, indent);
printMemoryHeader(curr);
o << '\n';
@@ -1004,7 +1040,7 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
}
}
}
- o << "\")\n";
+ o << "\")" << maybeNewLine;
}
}
void visitModule(Module* curr) {
@@ -1020,20 +1056,19 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
visitFunctionType(child.get());
o << ")" << maybeNewLine;
}
- for (auto& child : curr->imports) {
- doIndent(o, indent);
- visitImport(child.get());
- o << maybeNewLine;
- }
- for (auto& child : curr->globals) {
- doIndent(o, indent);
- visitGlobal(child.get());
- o << maybeNewLine;
- }
+ visitMemory(&curr->memory);
if (curr->table.exists) {
visitTable(&curr->table); // Prints its own newlines
}
- visitMemory(&curr->memory);
+ ModuleUtils::iterImportedGlobals(*curr, [&](Global* global) {
+ visitGlobal(global);
+ });
+ ModuleUtils::iterImportedFunctions(*curr, [&](Function* func) {
+ visitFunction(func);
+ });
+ ModuleUtils::iterDefinedGlobals(*curr, [&](Global* global) {
+ visitGlobal(global);
+ });
for (auto& child : curr->exports) {
doIndent(o, indent);
visitExport(child.get());
@@ -1045,11 +1080,9 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
printMedium(o, "start") << ' ' << curr->start << ')';
o << maybeNewLine;
}
- for (auto& child : curr->functions) {
- doIndent(o, indent);
- visitFunction(child.get());
- o << maybeNewLine;
- }
+ ModuleUtils::iterDefinedFunctions(*curr, [&](Function* func) {
+ visitFunction(func);
+ });
for (auto& section : curr->userSections) {
doIndent(o, indent);
o << ";; custom section \"" << section.name << "\", size " << section.data.size();