Skip to content

Commit 511bffe

Browse files
osa1Commit Queue
authored andcommitted
[dart2wasm] Allow giving names to globals in the module globals section
Currently no globals are named, so the subsection will be empty. Three extra bytes will be generated in the names section for the empty subsection. When debugging we can now name globals with the optional positional argument: // "my global" is new: m.globals.define(w.GlobalType(type), "my global") wasm-opt and v8 (d8, wami) support the subsection, so the global will now appear as "my global" in the debugger and Wast outputs. Change-Id: I5988ab792209c5c82593b85c48fead65ad536031 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/355120 Reviewed-by: Martin Kustermann <[email protected]> Commit-Queue: Ömer Ağacan <[email protected]>
1 parent a856897 commit 511bffe

File tree

7 files changed

+51
-14
lines changed

7 files changed

+51
-14
lines changed

pkg/wasm_builder/lib/src/builder/global.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ part of 'globals.dart';
88
class GlobalBuilder extends ir.Global with IndexableBuilder<ir.DefinedGlobal> {
99
final InstructionsBuilder initializer;
1010

11-
GlobalBuilder(ModuleBuilder module, super.index, super.type)
11+
GlobalBuilder(ModuleBuilder module, super.index, super.type,
12+
[super.globalName])
1213
: initializer =
1314
InstructionsBuilder(module, [type.type], isGlobalInitializer: true);
1415

1516
@override
1617
ir.DefinedGlobal forceBuild() =>
17-
ir.DefinedGlobal(initializer.build(), finalizableIndex, type);
18+
ir.DefinedGlobal(initializer.build(), finalizableIndex, type, globalName);
1819
}

pkg/wasm_builder/lib/src/builder/globals.dart

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,18 @@ class GlobalsBuilder with Builder<ir.Globals> {
1313
final _importedGlobals = <ir.Import>[];
1414
final _globalBuilders = <GlobalBuilder>[];
1515

16+
/// Number of named globals.
17+
int _namedCount = 0;
18+
1619
GlobalsBuilder(this._module);
1720

1821
/// Defines a new global variable in this module.
19-
GlobalBuilder define(ir.GlobalType type) {
20-
final global = GlobalBuilder(_module, ir.FinalizableIndex(), type);
22+
GlobalBuilder define(ir.GlobalType type, [String? name]) {
23+
final global = GlobalBuilder(_module, ir.FinalizableIndex(), type, name);
2124
_globalBuilders.add(global);
25+
if (name != null) {
26+
_namedCount += 1;
27+
}
2228
return global;
2329
}
2430

@@ -36,6 +42,6 @@ class GlobalsBuilder with Builder<ir.Globals> {
3642
ir.Globals forceBuild() {
3743
final built = finalizeImportsAndBuilders<ir.DefinedGlobal>(
3844
_importedGlobals, _globalBuilders);
39-
return ir.Globals(_importedGlobals, built);
45+
return ir.Globals(_importedGlobals, built, _namedCount);
4046
}
4147
}

pkg/wasm_builder/lib/src/ir/global.dart

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@ abstract class Global with Indexable implements Exportable {
1010
final FinalizableIndex finalizableIndex;
1111
final GlobalType type;
1212

13-
Global(this.finalizableIndex, this.type);
13+
/// Name of the global in the names section.
14+
final String? globalName;
15+
16+
Global(this.finalizableIndex, this.type, this.globalName);
1417

1518
@override
16-
String toString() => "$finalizableIndex";
19+
String toString() => globalName ?? "$finalizableIndex";
1720

1821
@override
1922
Export export(String name) => GlobalExport(name, this);
@@ -23,7 +26,8 @@ abstract class Global with Indexable implements Exportable {
2326
class DefinedGlobal extends Global implements Serializable {
2427
final Instructions initializer;
2528

26-
DefinedGlobal(this.initializer, super.finalizableIndex, super.type);
29+
DefinedGlobal(this.initializer, super.finalizableIndex, super.type,
30+
[super.globalName]);
2731

2832
@override
2933
void serialize(Serializer s) {
@@ -36,10 +40,12 @@ class DefinedGlobal extends Global implements Serializable {
3640
class ImportedGlobal extends Global implements Import {
3741
@override
3842
final String module;
43+
3944
@override
4045
final String name;
4146

42-
ImportedGlobal(this.module, this.name, super.finalizableIndex, super.type);
47+
ImportedGlobal(this.module, this.name, super.finalizableIndex, super.type,
48+
[super.globalName]);
4349

4450
@override
4551
void serialize(Serializer s) {

pkg/wasm_builder/lib/src/ir/globals.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,8 @@ class Globals {
1414
/// Defined globals.
1515
final List<DefinedGlobal> defined;
1616

17-
Globals(this.imported, this.defined);
17+
/// Number of named globals.
18+
final int namedCount;
19+
20+
Globals(this.imported, this.defined, this.namedCount);
1821
}

pkg/wasm_builder/lib/src/ir/imports.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import 'ir.dart';
88
/// Any import (function, table, memory or global).
99
abstract class Import implements Indexable, Serializable {
1010
String get module;
11+
1112
@override
1213
String get name;
1314
}

pkg/wasm_builder/lib/src/ir/module.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,10 @@ class Module implements Serializable {
5959
CodeSection(functions.defined, watchPoints).serialize(s);
6060
DataSection(dataSegments.defined, watchPoints).serialize(s);
6161
if (functions.namedCount > 0 || types.namedCount > 0) {
62-
NameSection(functions.all, types.defined, watchPoints,
62+
NameSection(functions.all, types.defined, globals.defined, watchPoints,
6363
functionNameCount: functions.namedCount,
64-
typeNameCount: types.namedCount)
64+
typeNameCount: types.namedCount,
65+
globalNameCount: globals.namedCount)
6566
.serialize(s);
6667
}
6768
}

pkg/wasm_builder/lib/src/serialize/sections.dart

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,11 +330,15 @@ abstract class CustomSection extends Section {
330330
class NameSection extends CustomSection {
331331
final List<ir.BaseFunction> functions;
332332
final List<ir.DefType> types;
333+
final List<ir.Global> globals;
333334
final int functionNameCount;
334335
final int typeNameCount;
336+
final int globalNameCount;
335337

336-
NameSection(this.functions, this.types, super.watchPoints,
337-
{required this.functionNameCount, required this.typeNameCount});
338+
NameSection(this.functions, this.types, this.globals, super.watchPoints,
339+
{required this.functionNameCount,
340+
required this.typeNameCount,
341+
required this.globalNameCount});
338342

339343
@override
340344
bool get isNotEmpty => functionNameCount > 0 || typeNameCount > 0;
@@ -363,11 +367,26 @@ class NameSection extends CustomSection {
363367
}
364368
}
365369

370+
final globalNameSubsection = Serializer();
371+
globalNameSubsection.writeUnsigned(globalNameCount);
372+
for (int i = 0; i < globals.length; i++) {
373+
final globalName = globals[i].globalName;
374+
if (globalName != null) {
375+
globalNameSubsection.writeUnsigned(i);
376+
globalNameSubsection.writeName(globalName);
377+
}
378+
}
379+
366380
s.writeByte(1); // Function names subsection
367381
s.writeUnsigned(functionNameSubsection.data.length);
368382
s.writeData(functionNameSubsection);
383+
369384
s.writeByte(4); // Type names subsection
370385
s.writeUnsigned(typeNameSubsection.data.length);
371386
s.writeData(typeNameSubsection);
387+
388+
s.writeByte(7); // Global names subsection
389+
s.writeUnsigned(globalNameSubsection.data.length);
390+
s.writeData(globalNameSubsection);
372391
}
373392
}

0 commit comments

Comments
 (0)