[wasm_builder] Update incorrect docs, some minor refactoring

- Imported things (modules, globals, tables, functions) don't need to
  declared before defining things, as we don't assign indices to things
  before finalizing the IR, and when finalizing we assign ids to
  imported things before defined things, in
  `finalizeImportsAndBuilders`.

  Remove documentation saying imports should be declared before
  definitions.

  `FunctionsBuilder._functions` list was used in the name section, and
  required imports to come before definitions. This list is now removed,
  instead we pass `[...imported, ...defined]` to the names section.

- Stop upcasting imported things to `Import` before we need to upcast.

- Fix global lists passed to `NameSection`.

  These lists need to include imports as well (even though we never name
  them, wasm_builder doesn't allow naming them yet) otherwise the
  indices in the name section will be incorrect.

  wasm_builder doesn't allow importing types yet, so we don't need to do
  the same for the types list.

Change-Id: Id05632c3af7937bd66d7581d89d538137020f6e6
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/366601
Commit-Queue: Ömer Ağacan <omersa@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
This commit is contained in:
Ömer Sinan Ağacan 2024-05-15 09:49:02 +00:00 committed by Commit Queue
parent 0299c05481
commit da4bf60a09
10 changed files with 24 additions and 35 deletions

View file

@ -11,9 +11,8 @@ part 'function.dart';
/// The interface for the functions in a module.
class FunctionsBuilder with Builder<ir.Functions> {
final ModuleBuilder _module;
final _functions = <ir.BaseFunction>[];
final _functionBuilders = <FunctionBuilder>[];
final _importedFunctions = <ir.Import>[];
final _importedFunctions = <ir.ImportedFunction>[];
int _nameCount = 0;
ir.BaseFunction? _start;
@ -28,7 +27,6 @@ class FunctionsBuilder with Builder<ir.Functions> {
if (name != null) {
_nameCount++;
}
_functions.add(function);
}
/// Defines a new function in this module with the given function type.
@ -44,9 +42,6 @@ class FunctionsBuilder with Builder<ir.Functions> {
}
/// Import a function into the module.
///
/// All imported functions must be specified before any functions are declared
/// using [FunctionsBuilder.define].
ir.ImportedFunction import(String module, String name, ir.FunctionType type,
[String? functionName]) {
final function = ir.ImportedFunction(
@ -60,7 +55,6 @@ class FunctionsBuilder with Builder<ir.Functions> {
ir.Functions forceBuild() {
final built = finalizeImportsAndBuilders<ir.DefinedFunction>(
_importedFunctions, _functionBuilders);
return ir.Functions(
_start, _importedFunctions, built, _functions, _nameCount);
return ir.Functions(_start, _importedFunctions, built, _nameCount);
}
}

View file

@ -10,7 +10,7 @@ part 'global.dart';
class GlobalsBuilder with Builder<ir.Globals> {
final ModuleBuilder _module;
final _importedGlobals = <ir.Import>[];
final _importedGlobals = <ir.ImportedGlobal>[];
final _globalBuilders = <GlobalBuilder>[];
/// Number of named globals.
@ -29,9 +29,6 @@ class GlobalsBuilder with Builder<ir.Globals> {
}
/// Imports a global variable into this module.
///
/// All imported globals must be specified before any globals are declared
/// using [Globals.define].
ir.ImportedGlobal import(String module, String name, ir.GlobalType type) {
final global = ir.ImportedGlobal(module, name, ir.FinalizableIndex(), type);
_importedGlobals.add(global);

View file

@ -8,7 +8,7 @@ import 'util.dart';
class MemoriesBuilder with Builder<ir.Memories> {
final _definedMemories = <ir.DefinedMemory>[];
final _importedMemories = <ir.Import>[];
final _importedMemories = <ir.ImportedMemory>[];
/// Add a new memory to the module.
ir.DefinedMemory define(bool shared, int minSize, [int? maxSize]) {
@ -19,9 +19,6 @@ class MemoriesBuilder with Builder<ir.Memories> {
}
/// Imports a memory into this module.
///
/// All imported memories must be specified before any memories are declared
/// using [defined].
ir.ImportedMemory import(String module, String name, bool shared, int minSize,
[int? maxSize]) {
final memory = ir.ImportedMemory(

View file

@ -41,11 +41,12 @@ class ModuleBuilder with Builder<ir.Module> {
finalGlobals,
types.build(),
dataSegments.build(),
finalFunctions.imported
.followedBy(finalTables.imported)
.followedBy(finalMemories.imported)
.followedBy(finalGlobals.imported)
.toList(),
<ir.Import>[
...finalFunctions.imported,
...finalTables.imported,
...finalMemories.imported,
...finalGlobals.imported
],
watchPoints);
}
}

View file

@ -11,7 +11,7 @@ part 'table.dart';
/// The interface for the tables in a module.
class TablesBuilder with Builder<ir.Tables> {
final _tableBuilders = <TableBuilder>[];
final _importedTables = <ir.Import>[];
final _importedTables = <ir.ImportedTable>[];
/// Defines a new table in this module.
TableBuilder define(ir.RefType type, int minSize, [int? maxSize]) {
@ -21,9 +21,6 @@ class TablesBuilder with Builder<ir.Tables> {
}
/// Imports a table into this module.
///
/// All imported tables must be specified before any tables are declared
/// using [Tables.define].
ir.ImportedTable import(
String module, String name, ir.RefType type, int minSize,
[int? maxSize]) {

View file

@ -13,16 +13,13 @@ class Functions {
final BaseFunction? start;
/// Imported functions.
final List<Import> imported;
final List<ImportedFunction> imported;
/// Defined functions.
final List<DefinedFunction> defined;
/// All functions, in the order they were emitted.
final List<BaseFunction> all;
/// Named functions.
final int namedCount;
Functions(this.start, this.imported, this.defined, this.all, this.namedCount);
Functions(this.start, this.imported, this.defined, this.namedCount);
}

View file

@ -9,7 +9,7 @@ part 'global.dart';
class Globals {
/// Imported globals.
final List<Import> imported;
final List<ImportedGlobal> imported;
/// Defined globals.
final List<DefinedGlobal> defined;

View file

@ -9,7 +9,7 @@ part 'memory.dart';
class Memories {
/// Imported memories.
final List<Import> imported;
final List<ImportedMemory> imported;
/// Defined memories.
final List<DefinedMemory> defined;

View file

@ -51,8 +51,14 @@ class Module implements Serializable {
DataCountSection(dataSegments.defined, watchPoints).serialize(s);
CodeSection(functions.defined, watchPoints).serialize(s);
DataSection(dataSegments.defined, watchPoints).serialize(s);
if (functions.namedCount > 0 || types.namedCount > 0) {
NameSection(functions.all, types.defined, globals.defined, watchPoints,
if (functions.namedCount > 0 ||
types.namedCount > 0 ||
globals.namedCount > 0) {
NameSection(
<BaseFunction>[...functions.imported, ...functions.defined],
types.defined,
<Global>[...globals.imported, ...globals.defined],
watchPoints,
functionNameCount: functions.namedCount,
typeNameCount: types.namedCount,
globalNameCount: globals.namedCount)

View file

@ -9,7 +9,7 @@ part 'table.dart';
class Tables {
/// Imported tables.
final List<Import> imported;
final List<ImportedTable> imported;
/// Defined tables.
final List<DefinedTable> defined;