Skip to content

Commit 5c2125c

Browse files
author
Kevin Millikin
committed
dart2js: Flatten the printed representation of the CPS IR.
BUG= R=sigmund@google.com Review URL: https://codereview.chromium.org/1588543003 .
1 parent f114dc1 commit 5c2125c

File tree

1 file changed

+36
-9
lines changed

1 file changed

+36
-9
lines changed

pkg/compiler/lib/src/cps_ir/cps_ir_nodes_sexpr.dart

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,24 +71,41 @@ class SExpressionStringifier extends Indentation implements Visitor<String> {
7171
String visitLetPrim(LetPrim node) {
7272
String name = newValueName(node.primitive);
7373
String value = visit(node.primitive);
74+
String bindings = '($name $value)';
75+
String skip = ' ' * '(LetPrim ('.length;
76+
while (node.body is LetPrim) {
77+
node = node.body;
78+
name = newValueName(node.primitive);
79+
value = visit(node.primitive);
80+
bindings += '\n${indentation}$skip($name $value)';
81+
}
7482
String body = indentBlock(() => visit(node.body));
75-
return '$indentation(LetPrim ($name $value)\n$body)';
83+
return '$indentation(LetPrim ($bindings)\n$body)';
84+
}
85+
86+
bool isBranchTarget(Continuation cont) {
87+
return cont.hasExactlyOneUse && cont.firstRef.parent is Branch;
7688
}
7789

7890
String visitLetCont(LetCont node) {
7991
String conts;
8092
bool first = true;
93+
String skip = ' ' * '(LetCont ('.length;
8194
for (Continuation continuation in node.continuations) {
95+
// Branch continuations will be printed at their use site.
96+
if (isBranchTarget(continuation)) continue;
8297
if (first) {
8398
first = false;
8499
conts = visit(continuation);
85100
} else {
86101
// Each subsequent line is indented additional spaces to align it
87102
// with the previous continuation.
88-
String indent = '$indentation${' ' * '(LetCont ('.length}';
89-
conts = '$conts\n$indent${visit(continuation)}';
103+
conts += '\n${indentation}$skip${visit(continuation)}';
90104
}
91105
}
106+
// If there were no continuations printed, just print the body.
107+
if (first) return visit(node.body);
108+
92109
String body = indentBlock(() => visit(node.body));
93110
return '$indentation(LetCont ($conts)\n$body)';
94111
}
@@ -191,10 +208,14 @@ class SExpressionStringifier extends Indentation implements Visitor<String> {
191208

192209
String visitBranch(Branch node) {
193210
String condition = access(node.condition);
194-
String trueCont = access(node.trueContinuation);
195-
String falseCont = access(node.falseContinuation);
211+
assert(isBranchTarget(node.trueContinuation.definition));
212+
assert(isBranchTarget(node.falseContinuation.definition));
213+
String trueCont =
214+
indentBlock(() => visit(node.trueContinuation.definition));
215+
String falseCont =
216+
indentBlock(() => visit(node.falseContinuation.definition));
196217
String strict = node.isStrictCheck ? 'Strict' : 'NonStrict';
197-
return '$indentation(Branch $condition $trueCont $falseCont $strict)';
218+
return '$indentation(Branch $strict $condition\n$trueCont\n$falseCont)';
198219
}
199220

200221
String visitUnreachable(Unreachable node) {
@@ -207,11 +228,17 @@ class SExpressionStringifier extends Indentation implements Visitor<String> {
207228
}
208229

209230
String visitContinuation(Continuation node) {
231+
if (isBranchTarget(node)) {
232+
assert(node.parameters.isEmpty);
233+
assert(!node.isRecursive);
234+
return indentBlock(() => visit(node.body));
235+
}
210236
String name = newContinuationName(node);
211237
if (node.isRecursive) name = 'rec $name';
212-
// TODO(karlklose): this should be changed to `.map(visit).join(' ')` and
213-
// should recurse to [visit]. Currently we can't do that, because the
214-
// unstringifier_test produces [LetConts] with dummy arguments on them.
238+
// TODO(karlklose): this should be changed to `.map(visit).join(' ')`
239+
// and should recurse to [visit]. Currently we can't do that, because
240+
// the unstringifier_test produces [LetConts] with dummy arguments on
241+
// them.
215242
String parameters = node.parameters
216243
.map((p) => '${decorator(p, newValueName(p))}')
217244
.join(' ');

0 commit comments

Comments
 (0)