[dart2wasm] Add wasm module printing functionality.

This adds support for printing module IR as text format.
For convenience we add a `pkg/dart2wasm/bin/wami.dart` that
produces very similar output to V8's `wami`.

The goal is to use this to write size/perf optimization tests
by dumping IR into expectation files (will add this
infrastructure in a future CL)

Issue https://github.com/dart-lang/sdk/issues/60928

Change-Id: I42d19c2b8c6242f55693d6ed5d844a5c1ecb1f39
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/454600
Commit-Queue: Martin Kustermann <kustermann@google.com>
Reviewed-by: Ömer Ağacan <omersa@google.com>
This commit is contained in:
Martin Kustermann
2025-10-15 12:36:50 -07:00
committed by Commit Queue
parent d70aa28821
commit e63dcafc0c
12 changed files with 2390 additions and 2 deletions
+52
View File
@@ -3,6 +3,7 @@
// BSD-style license that can be found in the LICENSE file.
import '../serialize/serialize.dart';
import '../serialize/printer.dart';
import 'ir.dart';
/// A local variable defined in a function.
@@ -12,6 +13,15 @@ class Local {
Local(this.index, this.type);
void printTo(IrPrinter p, Map<int, String> localNames,
{bool isParam = false}) {
p.write(isParam ? 'param' : 'local');
p.write(' ');
p.writeLocalIndexReference(index);
p.write(' ');
p.writeValueType(type);
}
@override
String toString() => "$index";
}
@@ -82,6 +92,39 @@ class DefinedFunction extends BaseFunction implements Serializable {
s.writeData(localS);
}
void printTo(IrPrinter p) {
p.write('(func ');
p.writeFunctionReference(this);
String? exportName;
for (final f in enclosingModule.exports.exported) {
if (f is FunctionExport && f.function == this) {
exportName = f.name;
break;
}
}
if (exportName != null) {
p.write(' ');
p.writeExport(exportName);
}
p.withLocalNames(localNames, () {
if (type.inputs.isNotEmpty || type.outputs.isNotEmpty) {
p.write(' ');
type.printSignatureWithNamesTo(p, oneLine: true);
}
p.writeln('');
p.withIndent(() {
for (int i = type.inputs.length; i < locals.length; ++i) {
p.write('(');
locals[i].printTo(p, localNames);
p.writeln(')');
}
body.printTo(p);
});
});
p.write(')');
}
@override
String toString() => functionName ?? "#$finalizableIndex";
}
@@ -105,6 +148,15 @@ class ImportedFunction extends BaseFunction implements Import {
s.writeUnsigned(type.index);
}
void printTo(IrPrinter p) {
p.write('(func ');
p.writeFunctionReference(this);
p.writeImport(module, name);
p.write(' ');
type.printOneLineSignatureTo(p);
p.write(')');
}
@override
String toString() => "$module.$name";
}