Skip to content

Commit e533329

Browse files
natebiggsCommit Queue
authored and
Commit Queue
committed
[dart2js] Migrate ssa/jump_handler.dart to null safety.
Change-Id: I23475740209738302e17ef298394bccb847a341f Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/277060 Commit-Queue: Nate Biggs <[email protected]> Reviewed-by: Sigmund Cherem <[email protected]>
1 parent b9469f4 commit e533329

File tree

5 files changed

+44
-72
lines changed

5 files changed

+44
-72
lines changed

pkg/compiler/lib/src/ssa/builder.dart

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2368,8 +2368,8 @@ class KernelSsaGraphBuilder extends ir.Visitor<void>
23682368
HBasicBlock conditionBlock = addNewBlock();
23692369

23702370
List<LocalsHandler> continueHandlers = <LocalsHandler>[];
2371-
jumpHandler.forEachContinue(
2372-
(HContinue instruction, interfaces.LocalsHandler locals) {
2371+
jumpHandler
2372+
.forEachContinue((HContinue instruction, LocalsHandler locals) {
23732373
instruction.block.addSuccessor(conditionBlock);
23742374
continueHandlers.add(locals);
23752375
});
@@ -2724,8 +2724,7 @@ class KernelSsaGraphBuilder extends ir.Visitor<void>
27242724

27252725
HBasicBlock joinBlock = graph.addNewBlock();
27262726
List<LocalsHandler> breakHandlers = [];
2727-
handler.forEachBreak(
2728-
(HBreak breakInstruction, interfaces.LocalsHandler locals) {
2727+
handler.forEachBreak((HBreak breakInstruction, LocalsHandler locals) {
27292728
breakInstruction.block.addSuccessor(joinBlock);
27302729
breakHandlers.add(locals);
27312730
});
@@ -3072,13 +3071,11 @@ class KernelSsaGraphBuilder extends ir.Visitor<void>
30723071
// the join block is never added to the graph.
30733072
HBasicBlock joinBlock = HBasicBlock();
30743073
List<LocalsHandler> caseHandlers = [];
3075-
jumpHandler
3076-
.forEachBreak((HBreak instruction, interfaces.LocalsHandler locals) {
3074+
jumpHandler.forEachBreak((HBreak instruction, LocalsHandler locals) {
30773075
instruction.block.addSuccessor(joinBlock);
30783076
caseHandlers.add(locals);
30793077
});
3080-
jumpHandler.forEachContinue(
3081-
(HContinue instruction, interfaces.LocalsHandler locals) {
3078+
jumpHandler.forEachContinue((HContinue instruction, LocalsHandler locals) {
30823079
assert(
30833080
false,
30843081
failedAt(_elementMap.getSpannable(targetElement, switchStatement),

pkg/compiler/lib/src/ssa/builder_interfaces.dart

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
import 'package:compiler/src/elements/types.dart';
65
import 'package:compiler/src/options.dart';
76
import 'package:kernel/ast.dart' as ir;
87

@@ -12,9 +11,10 @@ import '../elements/entities.dart';
1211
import '../elements/jumps.dart';
1312
import '../inferrer/abstract_value_domain.dart';
1413
import '../inferrer/types.dart';
15-
import '../io/source_information.dart';
1614
import '../js_model/js_world.dart';
1715
import 'nodes.dart';
16+
import 'jump_handler.dart';
17+
import 'locals_handler.dart';
1818

1919
abstract class KernelSsaGraphBuilder extends ir.Visitor<void> {
2020
JClosedWorld get closedWorld;
@@ -53,31 +53,3 @@ abstract class KernelSsaGraphBuilder extends ir.Visitor<void> {
5353

5454
HInstruction pop();
5555
}
56-
57-
abstract class JumpHandler {
58-
void generateBreak(SourceInformation sourceInformation,
59-
[LabelDefinition label]);
60-
void generateContinue(SourceInformation sourceInformation,
61-
[LabelDefinition label]);
62-
void forEachBreak(void action(HBreak instruction, LocalsHandler locals));
63-
void forEachContinue(
64-
void action(HContinue instruction, LocalsHandler locals));
65-
bool hasAnyContinue();
66-
bool hasAnyBreak();
67-
void close();
68-
JumpTarget get target;
69-
List<LabelDefinition> get labels;
70-
}
71-
72-
abstract class LocalsHandler {
73-
void updateLocal(JumpTarget target, HInstruction value);
74-
75-
DartType substInContext(DartType type);
76-
77-
HInstruction readThis({SourceInformation? sourceInformation});
78-
79-
Local getTypeVariableAsLocal(TypeVariableType parameter);
80-
81-
HInstruction readLocal(Local typeVariableLocal,
82-
{SourceInformation? sourceInformation});
83-
}

pkg/compiler/lib/src/ssa/jump_handler.dart

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
// @dart = 2.10
6-
75
import '../common.dart';
86
import '../elements/jumps.dart';
97
import '../inferrer/abstract_value_domain.dart';
@@ -22,11 +20,24 @@ class _JumpHandlerEntry {
2220
_JumpHandlerEntry(this.jumpInstruction, this.locals);
2321
}
2422

25-
abstract class JumpHandler extends interfaces.JumpHandler {
23+
abstract class JumpHandler {
2624
factory JumpHandler(
2725
interfaces.KernelSsaGraphBuilder builder, JumpTarget target) {
2826
return TargetJumpHandler(builder, target);
2927
}
28+
29+
void generateBreak(SourceInformation sourceInformation,
30+
[LabelDefinition? label]);
31+
void generateContinue(SourceInformation sourceInformation,
32+
[LabelDefinition? label]);
33+
void forEachBreak(void action(HBreak instruction, LocalsHandler locals));
34+
void forEachContinue(
35+
void action(HContinue instruction, LocalsHandler locals));
36+
bool hasAnyContinue();
37+
bool hasAnyBreak();
38+
void close();
39+
JumpTarget? get target;
40+
List<LabelDefinition> get labels;
3041
}
3142

3243
/// Jump handler used to avoid null checks when a target isn't used as the
@@ -39,14 +50,14 @@ class NullJumpHandler implements JumpHandler {
3950

4051
@override
4152
void generateBreak(SourceInformation sourceInformation,
42-
[LabelDefinition label]) {
53+
[LabelDefinition? label]) {
4354
reporter.internalError(CURRENT_ELEMENT_SPANNABLE,
4455
'NullJumpHandler.generateBreak should not be called.');
4556
}
4657

4758
@override
4859
void generateContinue(SourceInformation sourceInformation,
49-
[LabelDefinition label]) {
60+
[LabelDefinition? label]) {
5061
reporter.internalError(CURRENT_ELEMENT_SPANNABLE,
5162
'NullJumpHandler.generateContinue should not be called.');
5263
}
@@ -65,7 +76,7 @@ class NullJumpHandler implements JumpHandler {
6576
@override
6677
List<LabelDefinition> get labels => const [];
6778
@override
68-
JumpTarget get target => null;
79+
JumpTarget? get target => null;
6980
}
7081

7182
/// Jump handler that records breaks until a target block is available.
@@ -88,7 +99,7 @@ class TargetJumpHandler implements JumpHandler {
8899

89100
@override
90101
void generateBreak(SourceInformation sourceInformation,
91-
[LabelDefinition label]) {
102+
[LabelDefinition? label]) {
92103
HInstruction breakInstruction;
93104
if (label == null) {
94105
breakInstruction =
@@ -98,13 +109,13 @@ class TargetJumpHandler implements JumpHandler {
98109
HBreak.toLabel(_abstractValueDomain, label, sourceInformation);
99110
}
100111
LocalsHandler locals = LocalsHandler.from(builder.localsHandler);
101-
builder.close(breakInstruction);
112+
builder.close(breakInstruction as HJump);
102113
jumps.add(_JumpHandlerEntry(breakInstruction, locals));
103114
}
104115

105116
@override
106117
void generateContinue(SourceInformation sourceInformation,
107-
[LabelDefinition label]) {
118+
[LabelDefinition? label]) {
108119
HInstruction continueInstruction;
109120
if (label == null) {
110121
continueInstruction =
@@ -117,23 +128,25 @@ class TargetJumpHandler implements JumpHandler {
117128
assert(!label.target.isSwitchCase);
118129
}
119130
LocalsHandler locals = LocalsHandler.from(builder.localsHandler);
120-
builder.close(continueInstruction);
131+
builder.close(continueInstruction as HJump);
121132
jumps.add(_JumpHandlerEntry(continueInstruction, locals));
122133
}
123134

124135
@override
125136
void forEachBreak(
126137
void Function(HBreak instruction, LocalsHandler locals) action) {
127138
for (_JumpHandlerEntry entry in jumps) {
128-
if (entry.isBreak()) action(entry.jumpInstruction, entry.locals);
139+
final jumpInstruction = entry.jumpInstruction;
140+
if (jumpInstruction is HBreak) action(jumpInstruction, entry.locals);
129141
}
130142
}
131143

132144
@override
133145
void forEachContinue(
134146
void Function(HContinue instruction, LocalsHandler locals) action) {
135147
for (_JumpHandlerEntry entry in jumps) {
136-
if (entry.isContinue()) action(entry.jumpInstruction, entry.locals);
148+
final jumpInstruction = entry.jumpInstruction;
149+
if (jumpInstruction is HContinue) action(jumpInstruction, entry.locals);
137150
}
138151
}
139152

@@ -161,7 +174,7 @@ class TargetJumpHandler implements JumpHandler {
161174

162175
@override
163176
List<LabelDefinition> get labels {
164-
List<LabelDefinition> result = null;
177+
List<LabelDefinition>? result;
165178
for (LabelDefinition element in target.labels) {
166179
result ??= <LabelDefinition>[];
167180
result.add(element);
@@ -177,13 +190,11 @@ abstract class SwitchCaseJumpHandler extends TargetJumpHandler {
177190
/// switch case loop.
178191
final Map<JumpTarget, int> targetIndexMap = {};
179192

180-
SwitchCaseJumpHandler(
181-
interfaces.KernelSsaGraphBuilder builder, JumpTarget target)
182-
: super(builder, target);
193+
SwitchCaseJumpHandler(super.builder, super.target);
183194

184195
@override
185196
void generateBreak(SourceInformation sourceInformation,
186-
[LabelDefinition label]) {
197+
[LabelDefinition? label]) {
187198
if (label == null) {
188199
// Creates a special break instruction for the synthetic loop generated
189200
// for a switch statement with continue statements. See
@@ -193,35 +204,34 @@ abstract class SwitchCaseJumpHandler extends TargetJumpHandler {
193204
_abstractValueDomain, target, sourceInformation,
194205
breakSwitchContinueLoop: true);
195206
LocalsHandler locals = LocalsHandler.from(builder.localsHandler);
196-
builder.close(breakInstruction);
207+
builder.close(breakInstruction as HJump);
197208
jumps.add(_JumpHandlerEntry(breakInstruction, locals));
198209
} else {
199210
super.generateBreak(sourceInformation, label);
200211
}
201212
}
202213

203-
bool isContinueToSwitchCase(LabelDefinition label) {
214+
bool isContinueToSwitchCase(LabelDefinition? label) {
204215
return label != null && targetIndexMap.containsKey(label.target);
205216
}
206217

207218
@override
208219
void generateContinue(SourceInformation sourceInformation,
209-
[LabelDefinition label]) {
220+
[LabelDefinition? label]) {
210221
if (isContinueToSwitchCase(label)) {
211222
// Creates the special instructions 'label = i; continue l;' used in
212223
// switch statements with continue statements. See
213224
// [SsaFromAstMixin.buildComplexSwitchStatement] for detail.
214225

215-
assert(label != null);
216226
HInstruction value = builder.graph
217-
.addConstantInt(targetIndexMap[label.target], builder.closedWorld);
227+
.addConstantInt(targetIndexMap[label!.target]!, builder.closedWorld);
218228
builder.localsHandler.updateLocal(target, value);
219229

220230
assert(label.target.labels.contains(label));
221231
HInstruction continueInstruction =
222232
HContinue(_abstractValueDomain, target, sourceInformation);
223233
LocalsHandler locals = LocalsHandler.from(builder.localsHandler);
224-
builder.close(continueInstruction);
234+
builder.close(continueInstruction as HJump);
225235
jumps.add(_JumpHandlerEntry(continueInstruction, locals));
226236
} else {
227237
super.generateContinue(sourceInformation, label);

pkg/compiler/lib/src/ssa/locals_handler.dart

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import 'types.dart';
2424
/// Keeps track of locals (including parameters and phis) when building. The
2525
/// 'this' reference is treated as parameter and hence handled by this class,
2626
/// too.
27-
class LocalsHandler implements interfaces.LocalsHandler {
27+
class LocalsHandler {
2828
/// The values of locals that can be directly accessed (without redirections
2929
/// to boxes or closure-fields).
3030
///
@@ -83,7 +83,6 @@ class LocalsHandler implements interfaces.LocalsHandler {
8383

8484
/// Substituted type variables occurring in [type] into the context of
8585
/// [contextClass].
86-
@override
8786
DartType substInContext(DartType type) {
8887
DartType newType = type;
8988
final iType = instanceType;
@@ -341,7 +340,6 @@ class LocalsHandler implements interfaces.LocalsHandler {
341340
/// Returns an [HInstruction] for the given element. If the element is
342341
/// boxed or stored in a closure then the method generates code to retrieve
343342
/// the value.
344-
@override
345343
HInstruction readLocal(Local local, {SourceInformation? sourceInformation}) {
346344
if (isAccessedDirectly(local)) {
347345
HInstruction? value = directLocals[local];
@@ -401,7 +399,6 @@ class LocalsHandler implements interfaces.LocalsHandler {
401399
}
402400
}
403401

404-
@override
405402
HInstruction readThis({SourceInformation? sourceInformation}) {
406403
return readLocal(_scopeInfo!.thisLocal!,
407404
sourceInformation: sourceInformation);
@@ -426,14 +423,12 @@ class LocalsHandler implements interfaces.LocalsHandler {
426423
});
427424
}
428425

429-
@override
430426
Local getTypeVariableAsLocal(TypeVariableType type) {
431427
return typeVariableLocals[type.element] ??= TypeVariableLocal(type.element);
432428
}
433429

434430
/// Sets the [element] to [value]. If the element is boxed or stored in a
435431
/// closure then the method generates code to set the value.
436-
@override
437432
void updateLocal(Local local, HInstruction value,
438433
{SourceInformation? sourceInformation}) {
439434
if (value is HRef) {

pkg/compiler/lib/src/ssa/loop_handler.dart

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import '../inferrer/abstract_value_domain.dart';
1212
import '../io/source_information.dart';
1313

1414
import 'builder.dart';
15-
import 'builder_interfaces.dart' as interfaces;
1615
import 'jump_handler.dart';
1716
import 'locals_handler.dart';
1817
import 'nodes.dart';
@@ -102,8 +101,8 @@ abstract class LoopHandler {
102101
HBasicBlock updateBlock = builder.addNewBlock();
103102

104103
List<LocalsHandler> continueHandlers = <LocalsHandler>[];
105-
jumpHandler.forEachContinue(
106-
(HContinue instruction, interfaces.LocalsHandler locals) {
104+
jumpHandler
105+
.forEachContinue((HContinue instruction, LocalsHandler locals) {
107106
instruction.block.addSuccessor(updateBlock);
108107
continueHandlers.add(locals);
109108
});
@@ -262,8 +261,7 @@ abstract class LoopHandler {
262261

263262
List<LocalsHandler> breakHandlers = <LocalsHandler>[];
264263
// Collect data for the successors and the phis at each break.
265-
jumpHandler.forEachBreak(
266-
(HBreak breakInstruction, interfaces.LocalsHandler locals) {
264+
jumpHandler.forEachBreak((HBreak breakInstruction, LocalsHandler locals) {
267265
breakInstruction.block.addSuccessor(loopExitBlock);
268266
breakHandlers.add(locals);
269267
});

0 commit comments

Comments
 (0)