Skip to content

Commit 2fc0c8b

Browse files
mkustermannCommit Queue
authored andcommitted
[dart2wasm] Some refactorings of the compiler
a) Usage of `ClassInfo.*` We have an abstraction layer that translates dart types to wasm types - which is the `Translator` object (it has e.g. `Translator.translateType`). => We remove direct use of `ClassInfo.*` in most places => Instead of exposing `ClassInfo` for specific types (e.g. top type), we expose the wasm representation type b) Type of `ClassInfo.repr` Currently `ClassInfo.repr` is itself a `ClassInfo`, which is rather confusing. We use `ClassInfo.repr` to determine how to translate `InterfaceType`s of that class to the wasm type system. => We make `Class.repr` be an actual wasm representation type (we make it a `w.RefType`) c) We create a macro assembler `loadClassId` method so we have one place where we load the class id and we verify in assertions that the receiver type is `ref #Top`. Change-Id: Ie24008a98131dae34948406a1f5aec881499c1f0 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/431080 Commit-Queue: Martin Kustermann <[email protected]> Reviewed-by: Ömer Ağacan <[email protected]>
1 parent ac2fc57 commit 2fc0c8b

File tree

15 files changed

+213
-205
lines changed

15 files changed

+213
-205
lines changed

pkg/dart2wasm/lib/async.dart

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ mixin AsyncCodeGeneratorMixin on StateMachineEntryAstCodeGenerator {
5555
// here.
5656

5757
b.local_get(asyncStateLocal);
58-
b.ref_null(translator.topInfo.struct); // await value
59-
b.ref_null(translator.topInfo.struct); // error value
60-
b.ref_null(translator.stackTraceInfo.repr.struct); // stack trace
58+
b.ref_null(translator.topType.heapType); // await value
59+
b.ref_null(translator.topType.heapType); // error value
60+
b.ref_null(translator.stackTraceType.heapType); // stack trace
6161
translator.callFunction(resumeFun, b);
6262
b.drop(); // drop null
6363

@@ -79,15 +79,14 @@ mixin AsyncCodeGeneratorMixin on StateMachineEntryAstCodeGenerator {
7979
b.module.functions.define(
8080
translator.typesBuilder.defineFunction([
8181
asyncSuspendStateInfo.nonNullableType, // _AsyncSuspendState
82-
translator.topInfo.nullableType, // Object?, await value
83-
translator.topInfo.nullableType, // Object?, error value
84-
translator.stackTraceInfo.repr
85-
.nullableType // StackTrace?, error stack trace
82+
translator.topType, // Object?, await value
83+
translator.topType, // Object?, error value
84+
translator.stackTraceTypeNullable // StackTrace?, error stack trace
8685
], [
8786
// Inner function does not return a value, but it's Dart type is
8887
// `void Function(...)` and all Dart functions return a value, so we
8988
// add a return type.
90-
translator.topInfo.nullableType
89+
translator.topType
9190
]),
9291
"${function.functionName} inner");
9392
}
@@ -254,17 +253,15 @@ class AsyncStateMachineCodeGenerator extends StateMachineCodeGenerator {
254253
// Final state: return.
255254
emitTargetLabel(targets.last);
256255
b.local_get(_suspendStateLocal);
257-
b.ref_null(translator.topInfo.struct);
256+
b.ref_null(translator.topType.heapType);
258257
call(translator.getFunctionEntry(
259258
translator.asyncSuspendStateComplete.reference,
260259
uncheckedEntry: true));
261260
b.return_();
262261
b.end(); // masterLoop
263262

264-
final stackTraceLocal =
265-
addLocal(translator.stackTraceInfo.repr.nonNullableType);
266-
267-
final exceptionLocal = addLocal(translator.topInfo.nonNullableType);
263+
final stackTraceLocal = addLocal(translator.stackTraceType);
264+
final exceptionLocal = addLocal(translator.topTypeNonNullable);
268265

269266
void callCompleteError() {
270267
b.local_get(_suspendStateLocal);

pkg/dart2wasm/lib/class_info.dart

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,11 @@ class ClassInfo {
183183
/// the superclass.
184184
final Map<TypeParameter, TypeParameter> typeParameterMatch;
185185

186-
/// The class whose struct is used as the type for variables of this type.
187-
/// This is a type which is a superclass of all subtypes of this type.
188-
ClassInfo get repr => _repr!;
186+
/// The wasm type used to represent values of a dart interface type of this
187+
/// class.
188+
w.RefType get repr => _repr!;
189189

190-
ClassInfo? _repr;
190+
w.RefType? _repr;
191191

192192
/// Nullabe Wasm ref type for this class.
193193
final w.RefType nullableType;
@@ -478,7 +478,7 @@ class ClassInfoCollector {
478478
}
479479

480480
for (Field field in info.cls!.fields) {
481-
info._addField(w.FieldType(topInfo.nullableType),
481+
info._addField(w.FieldType(translator.topType),
482482
fieldName: field.name.text);
483483
}
484484
}
@@ -548,7 +548,7 @@ class ClassInfoCollector {
548548
// class maps to topInfo because boxed values are a subtype of Object in
549549
// Dart but not of the object struct.
550550
representation = cls == translator.coreTypes.objectClass
551-
? translator.topInfo
551+
? topInfo
552552
: translator.objectInfo;
553553
} else {
554554
void addRanges(List<Range> ranges) {
@@ -581,7 +581,13 @@ class ClassInfoCollector {
581581
}
582582
}
583583
final info = translator.classInfo[cls]!;
584-
info._repr = representation ?? info;
584+
representation ??= info;
585+
586+
if (representation == topInfo) {
587+
info._repr = translator.topTypeNonNullable;
588+
} else {
589+
info._repr = representation!.nonNullableType;
590+
}
585591
}
586592

587593
// Now that the representation types for all classes have been computed,

pkg/dart2wasm/lib/closures.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ class ClosureLayouter extends RecursiveVisitor {
356356
superType: superType);
357357
}
358358

359-
w.ValueType get topType => translator.topInfo.nullableType;
359+
w.ValueType get topType => translator.topType;
360360

361361
ClosureLayouter(this.translator)
362362
: procedureAttributeMetadata =

0 commit comments

Comments
 (0)