diff options
Diffstat (limited to 'src/passes/Print.cpp')
-rw-r--r-- | src/passes/Print.cpp | 199 |
1 files changed, 72 insertions, 127 deletions
diff --git a/src/passes/Print.cpp b/src/passes/Print.cpp index 9cc8b52ca..6e4c1452c 100644 --- a/src/passes/Print.cpp +++ b/src/passes/Print.cpp @@ -23,6 +23,7 @@ #include <ir/table-utils.h> #include <pass.h> #include <pretty_printing.h> +#include <support/string.h> #include <wasm-stack.h> #include <wasm-type-printing.h> #include <wasm.h> @@ -51,75 +52,10 @@ bool isFullForced() { return false; } -std::ostream& printEscapedString(std::ostream& os, std::string_view str) { - os << '"'; - for (unsigned char c : str) { - switch (c) { - case '\t': - os << "\\t"; - break; - case '\n': - os << "\\n"; - break; - case '\r': - os << "\\r"; - break; - case '"': - os << "\\\""; - break; - case '\'': - os << "\\'"; - break; - case '\\': - os << "\\\\"; - break; - default: { - if (c >= 32 && c < 127) { - os << c; - } else { - os << std::hex << '\\' << (c / 16) << (c % 16) << std::dec; - } - } - } - } - 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); + name.print(o); } return o; } @@ -132,7 +68,7 @@ std::ostream& printLocal(Index index, Function* func, std::ostream& o) { if (!name) { name = Name::fromInt(index); } - return printName(name, o); + return name.print(o); } // Print a name from the type section, if available. Otherwise print the type @@ -246,7 +182,7 @@ struct PrintSExpression : public UnifiedExpressionVisitor<PrintSExpression> { if (type.isBasic()) { return o << type; } - return o << '$' << typePrinter.getNames(type).name; + return typePrinter.getNames(type).name.print(o); } std::ostream& printPrefixedTypes(const char* prefix, Type type); @@ -413,7 +349,7 @@ struct PrintExpressionContents printMedium(o, "block"); if (curr->name.is()) { o << ' '; - printName(curr->name, o); + curr->name.print(o); } if (curr->type.isConcrete()) { o << ' '; @@ -431,7 +367,7 @@ struct PrintExpressionContents printMedium(o, "loop"); if (curr->name.is()) { o << ' '; - printName(curr->name, o); + curr->name.print(o); } if (curr->type.isConcrete()) { o << ' '; @@ -444,16 +380,16 @@ struct PrintExpressionContents } else { printMedium(o, "br "); } - printName(curr->name, o); + curr->name.print(o); } void visitSwitch(Switch* curr) { printMedium(o, "br_table"); for (auto& t : curr->targets) { o << ' '; - printName(t, o); + t.print(o); } o << ' '; - printName(curr->default_, o); + curr->default_.print(o); } void visitCall(Call* curr) { if (curr->isReturn) { @@ -461,7 +397,7 @@ struct PrintExpressionContents } else { printMedium(o, "call "); } - printName(curr->target, o); + curr->target.print(o); } void visitCallIndirect(CallIndirect* curr) { if (curr->isReturn) { @@ -471,7 +407,7 @@ struct PrintExpressionContents } if (features.hasReferenceTypes()) { - printName(curr->table, o); + curr->table.print(o); o << ' '; } @@ -496,11 +432,11 @@ struct PrintExpressionContents } void visitGlobalGet(GlobalGet* curr) { printMedium(o, "global.get "); - printName(curr->name, o); + curr->name.print(o); } void visitGlobalSet(GlobalSet* curr) { printMedium(o, "global.set "); - printName(curr->name, o); + curr->name.print(o); } void visitLoad(Load* curr) { prepareColor(o) << forceConcrete(curr->type); @@ -871,13 +807,15 @@ struct PrintExpressionContents o << "memory.init"; restoreNormalColor(o); printMemoryName(curr->memory, o, wasm); - o << " $" << curr->segment; + o << ' '; + curr->segment.print(o); } void visitDataDrop(DataDrop* curr) { prepareColor(o); o << "data.drop"; restoreNormalColor(o); - o << " $" << curr->segment; + o << ' '; + curr->segment.print(o); } void visitMemoryCopy(MemoryCopy* curr) { prepareColor(o); @@ -1952,40 +1890,40 @@ struct PrintExpressionContents void visitRefIsNull(RefIsNull* curr) { printMedium(o, "ref.is_null"); } void visitRefFunc(RefFunc* curr) { printMedium(o, "ref.func "); - printName(curr->func, o); + curr->func.print(o); } void visitRefEq(RefEq* curr) { printMedium(o, "ref.eq"); } void visitTableGet(TableGet* curr) { printMedium(o, "table.get "); - printName(curr->table, o); + curr->table.print(o); } void visitTableSet(TableSet* curr) { printMedium(o, "table.set "); - printName(curr->table, o); + curr->table.print(o); } void visitTableSize(TableSize* curr) { printMedium(o, "table.size "); - printName(curr->table, o); + curr->table.print(o); } void visitTableGrow(TableGrow* curr) { printMedium(o, "table.grow "); - printName(curr->table, o); + curr->table.print(o); } void visitTableFill(TableFill* curr) { printMedium(o, "table.fill "); - printName(curr->table, o); + curr->table.print(o); } void visitTableCopy(TableCopy* curr) { printMedium(o, "table.copy "); - printName(curr->destTable, o); + curr->destTable.print(o); o << ' '; - printName(curr->sourceTable, o); + curr->sourceTable.print(o); } void visitTry(Try* curr) { printMedium(o, "try"); if (curr->name.is()) { o << ' '; - printName(curr->name, o); + curr->name.print(o); } if (curr->type.isConcrete()) { o << ' '; @@ -2002,22 +1940,22 @@ struct PrintExpressionContents o << " ("; if (curr->catchTags[i]) { printMedium(o, curr->catchRefs[i] ? "catch_ref " : "catch "); - printName(curr->catchTags[i], o); + curr->catchTags[i].print(o); o << ' '; } else { printMedium(o, curr->catchRefs[i] ? "catch_all_ref " : "catch_all "); } - printName(curr->catchDests[i], o); + curr->catchDests[i].print(o); o << ')'; } } void visitThrow(Throw* curr) { printMedium(o, "throw "); - printName(curr->tag, o); + curr->tag.print(o); } void visitRethrow(Rethrow* curr) { printMedium(o, "rethrow "); - printName(curr->target, o); + curr->target.print(o); } void visitThrowRef(ThrowRef* curr) { printMedium(o, "throw_ref"); } void visitNop(Nop* curr) { printMinor(o, "nop"); } @@ -2087,15 +2025,15 @@ struct PrintExpressionContents switch (curr->op) { case BrOnNull: printMedium(o, "br_on_null "); - printName(curr->name, o); + curr->name.print(o); return; case BrOnNonNull: printMedium(o, "br_on_non_null "); - printName(curr->name, o); + curr->name.print(o); return; case BrOnCast: printMedium(o, "br_on_cast "); - printName(curr->name, o); + curr->name.print(o); o << ' '; printType(curr->ref->type); o << ' '; @@ -2103,7 +2041,7 @@ struct PrintExpressionContents return; case BrOnCastFail: printMedium(o, "br_on_cast_fail "); - printName(curr->name, o); + curr->name.print(o); o << ' '; printType(curr->ref->type); o << ' '; @@ -2126,7 +2064,7 @@ struct PrintExpressionContents void printFieldName(HeapType type, Index index) { auto names = parent.typePrinter.getNames(type).fieldNames; if (auto it = names.find(index); it != names.end()) { - o << '$' << it->second; + it->second.print(o); } else { o << index; } @@ -2178,7 +2116,8 @@ struct PrintExpressionContents printMedium(o, "array.new_data"); o << ' '; printHeapType(curr->type.getHeapType()); - o << " $" << curr->segment; + o << ' '; + curr->segment.print(o); } void visitArrayNewElem(ArrayNewElem* curr) { if (printUnreachableReplacement(curr)) { @@ -2187,7 +2126,8 @@ struct PrintExpressionContents printMedium(o, "array.new_elem"); o << ' '; printHeapType(curr->type.getHeapType()); - o << " $" << curr->segment; + o << ' '; + curr->segment.print(o); } void visitArrayNewFixed(ArrayNewFixed* curr) { if (printUnreachableReplacement(curr)) { @@ -2246,7 +2186,8 @@ struct PrintExpressionContents } printMedium(o, "array.init_data "); printHeapType(curr->ref->type.getHeapType()); - o << " $" << curr->segment; + o << ' '; + curr->segment.print(o); } void visitArrayInitElem(ArrayInitElem* curr) { if (printUnreachableOrNullReplacement(curr->ref)) { @@ -2254,7 +2195,8 @@ struct PrintExpressionContents } printMedium(o, "array.init_elem "); printHeapType(curr->ref->type.getHeapType()); - o << " $" << curr->segment; + o << ' '; + curr->segment.print(o); } void visitRefAs(RefAs* curr) { switch (curr->op) { @@ -2314,7 +2256,7 @@ struct PrintExpressionContents } void visitStringConst(StringConst* curr) { printMedium(o, "string.const "); - printEscapedString(o, curr->string.str); + String::printEscaped(o, curr->string.str); } void visitStringMeasure(StringMeasure* curr) { switch (curr->op) { @@ -2448,9 +2390,9 @@ struct PrintExpressionContents for (Index i = 0; i < curr->handlerTags.size(); i++) { o << " ("; printMedium(o, "tag "); - printName(curr->handlerTags[i], o); + curr->handlerTags[i].print(o); o << ' '; - printName(curr->handlerBlocks[i], o); + curr->handlerBlocks[i].print(o); o << ')'; } } @@ -2754,7 +2696,7 @@ void PrintSExpression::visitTry(Try* curr) { printDebugDelimiterLocation(curr, i); o << '('; printMedium(o, "catch "); - printName(curr->catchTags[i], o); + curr->catchTags[i].print(o); incIndent(); maybePrintImplicitBlock(curr->catchBodies[i]); decIndent(); @@ -2779,7 +2721,7 @@ void PrintSExpression::visitTry(Try* curr) { if (curr->delegateTarget == DELEGATE_CALLER_TARGET) { o << controlFlowDepth; } else { - printName(curr->delegateTarget, o); + curr->delegateTarget.print(o); } o << ")\n"; } @@ -2866,7 +2808,8 @@ void PrintSExpression::handleSignature(HeapType curr, Name name) { Signature sig = curr.getSignature(); o << "(func"; if (name.is()) { - o << " $" << name; + o << ' '; + name.print(o); if (currModule && currModule->features.hasGC()) { o << " (type "; printHeapType(curr) << ')'; @@ -2922,7 +2865,7 @@ void PrintSExpression::visitExport(Export* curr) { WASM_UNREACHABLE("invalid ExternalKind"); } o << ' '; - printName(curr->value, o) << "))"; + curr->value.print(o) << "))"; } void PrintSExpression::emitImportHeader(Importable* curr) { @@ -2954,7 +2897,7 @@ void PrintSExpression::visitImportedGlobal(Global* curr) { o << '('; emitImportHeader(curr); o << "(global "; - printName(curr->name, o) << ' '; + curr->name.print(o) << ' '; emitGlobalType(curr); o << "))" << maybeNewLine; } @@ -2963,7 +2906,7 @@ void PrintSExpression::visitDefinedGlobal(Global* curr) { doIndent(o, indent); o << '('; printMedium(o, "global "); - printName(curr->name, o) << ' '; + curr->name.print(o) << ' '; emitGlobalType(curr); o << ' '; visit(curr->init); @@ -2999,7 +2942,7 @@ void PrintSExpression::visitDefinedFunction(Function* curr) { } o << '('; printMajor(o, "func "); - printName(curr->name, o); + curr->name.print(o); if (currModule && currModule->features.hasGC()) { o << " (type "; printHeapType(curr->type) << ')'; @@ -3080,7 +3023,7 @@ void PrintSExpression::visitImportedTag(Tag* curr) { o << '('; emitImportHeader(curr); o << "(tag "; - printName(curr->name, o); + curr->name.print(o); if (curr->sig.params != Type::none) { o << maybeSpace; printParamType(curr->sig.params); @@ -3097,7 +3040,7 @@ void PrintSExpression::visitDefinedTag(Tag* curr) { doIndent(o, indent); o << '('; printMedium(o, "tag "); - printName(curr->name, o); + curr->name.print(o); if (curr->sig.params != Type::none) { o << maybeSpace; printParamType(curr->sig.params); @@ -3112,7 +3055,7 @@ void PrintSExpression::visitDefinedTag(Tag* curr) { void PrintSExpression::printTableHeader(Table* curr) { o << '('; printMedium(o, "table") << ' '; - printName(curr->name, o) << ' '; + curr->name.print(o) << ' '; o << curr->initial; if (curr->hasMax()) { o << ' ' << curr->max; @@ -3148,13 +3091,13 @@ void PrintSExpression::visitElementSegment(ElementSegment* curr) { doIndent(o, indent); o << '('; printMedium(o, "elem "); - printName(curr->name, o); + curr->name.print(o); if (curr->table.is()) { if (usesExpressions || currModule->tables.size() > 1) { // tableuse o << " (table "; - printName(curr->table, o); + curr->table.print(o); o << ")"; } @@ -3174,7 +3117,7 @@ void PrintSExpression::visitElementSegment(ElementSegment* curr) { for (auto* entry : curr->data) { auto* refFunc = entry->cast<RefFunc>(); o << ' '; - printName(refFunc->func, o); + refFunc->func.print(o); } } else { for (auto* entry : curr->data) { @@ -3188,7 +3131,7 @@ void PrintSExpression::visitElementSegment(ElementSegment* curr) { void PrintSExpression::printMemoryHeader(Memory* curr) { o << '('; printMedium(o, "memory") << ' '; - printName(curr->name, o) << ' '; + curr->name.print(o) << ' '; if (curr->is64()) { o << "i64 "; } @@ -3220,17 +3163,19 @@ void PrintSExpression::visitDataSegment(DataSegment* curr) { doIndent(o, indent); o << '('; printMajor(o, "data "); - printName(curr->name, o); + curr->name.print(o); o << ' '; if (!curr->isPassive) { assert(!currModule || currModule->memories.size() > 0); if (!currModule || curr->memory != currModule->memories[0]->name) { - o << "(memory $" << curr->memory << ") "; + o << "(memory "; + curr->memory.print(o); + o << ") "; } visit(curr->offset); o << ' '; } - printEscapedString(o, {curr->data.data(), curr->data.size()}); + String::printEscaped(o, {curr->data.data(), curr->data.size()}); o << ')' << maybeNewLine; } @@ -3259,7 +3204,7 @@ void PrintSExpression::visitModule(Module* curr) { printMajor(o, "module"); if (curr->name.is()) { o << ' '; - printName(curr->name, o); + curr->name.print(o); } incIndent(); @@ -3320,7 +3265,7 @@ void PrintSExpression::visitModule(Module* curr) { o << " declare func"; for (auto name : elemDeclareNames) { o << ' '; - printName(name, o); + name.print(o); } o << ')' << maybeNewLine; } @@ -3334,7 +3279,7 @@ void PrintSExpression::visitModule(Module* curr) { doIndent(o, indent); o << '('; printMedium(o, "start") << ' '; - printName(curr->start, o) << ')'; + curr->start.print(o) << ')'; o << maybeNewLine; } ModuleUtils::iterDefinedFunctions( @@ -3508,7 +3453,7 @@ printStackInst(StackInst* inst, std::ostream& o, Function* func) { } case StackInst::Delegate: { printMedium(o, "delegate "); - printName(inst->origin->cast<Try>()->delegateTarget, o); + inst->origin->cast<Try>()->delegateTarget.print(o); break; } default: @@ -3580,7 +3525,7 @@ static std::ostream& printStackIR(StackIR* ir, PrintSExpression& printer) { doIndent(); printMedium(o, "catch "); Try* curr = inst->origin->cast<Try>(); - printName(curr->catchTags[catchIndexStack.back()++], o); + curr->catchTags[catchIndexStack.back()++].print(o); indent++; break; } @@ -3600,7 +3545,7 @@ static std::ostream& printStackIR(StackIR* ir, PrintSExpression& printer) { if (curr->delegateTarget == DELEGATE_CALLER_TARGET) { o << controlFlowDepth; } else { - printName(curr->delegateTarget, o); + curr->delegateTarget.print(o); } break; } |