Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(295)

Unified Diff: lib/compiler/implementation/ssa/builder.dart

Issue 10544026: Simplify generated code for string interpolation by delaying the constant folding. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: lib/compiler/implementation/ssa/builder.dart
diff --git a/lib/compiler/implementation/ssa/builder.dart b/lib/compiler/implementation/ssa/builder.dart
index 53aacdd1fe8c8884ba66ad2fce96aac676ae5162..a546b85e29384cdd298507fb2ac3a8fd7f77c582 100644
--- a/lib/compiler/implementation/ssa/builder.dart
+++ b/lib/compiler/implementation/ssa/builder.dart
@@ -2590,9 +2590,7 @@ class SsaBuilder implements Visitor {
stack.add(graph.addConstantString(node.dartString, node));
return;
}
- int offset = node.getBeginToken().charOffset;
- StringBuilderVisitor stringBuilder =
- new StringBuilderVisitor(this, offset);
+ StringBuilderVisitor stringBuilder = new StringBuilderVisitor(this);
stringBuilder.visit(node);
stack.add(stringBuilder.result(node));
}
@@ -2744,9 +2742,7 @@ class SsaBuilder implements Visitor {
}
visitStringInterpolation(StringInterpolation node) {
- int offset = node.getBeginToken().charOffset;
- StringBuilderVisitor stringBuilder =
- new StringBuilderVisitor(this, offset);
+ StringBuilderVisitor stringBuilder = new StringBuilderVisitor(this);
stringBuilder.visit(node);
stack.add(stringBuilder.result(node));
}
@@ -3290,33 +3286,13 @@ class SsaBuilder implements Visitor {
*/
class StringBuilderVisitor extends AbstractVisitor {
final SsaBuilder builder;
- final Element stringConcat;
- final Element stringToString;
/**
- * Offset used for the synthetic operator token used by concat.
- * Can probably be removed when we stop using String.operator+.
- */
- final int offset;
-
- /**
- * Used to collect concatenated string literals into a single literal
- * instead of introducing unnecessary concatenations.
- */
- DartString literalAccumulator = const LiteralDartString("");
-
- /**
- * The string value generated so far (not including that which is still
- * in [literalAccumulator]).
+ * The string value generated so far.
*/
HInstruction prefix = null;
floitsch 2012/06/06 12:13:34 Maybe rename to 'result' and remove result-method?
kasperl 2012/06/06 13:31:03 Done.
- StringBuilderVisitor(builder, this.offset)
- : this.builder = builder,
- stringConcat = builder.compiler.findHelper(
- const SourceString("stringConcat")),
- stringToString = builder.compiler.findHelper(
- const SourceString("stringToString"));
+ StringBuilderVisitor(this.builder);
void visit(Node node) {
node.accept(this);
@@ -3327,27 +3303,9 @@ class StringBuilderVisitor extends AbstractVisitor {
}
void visitExpression(Node node) {
- flushLiterals(node);
node.accept(builder);
- HInstruction asString = buildToString(node, builder.pop());
- prefix = buildConcat(prefix, asString);
- }
-
- void visitLiteralNull(LiteralNull node) {
- addLiteral(const LiteralDartString("null"));
- }
-
- void visitLiteralInt(LiteralInt node) {
- addLiteral(new DartString.literal(node.value.toString()));
- }
-
- void visitLiteralDouble(LiteralDouble node) {
- addLiteral(new DartString.literal(node.value.toString()));
- }
-
- void visitLiteralBool(LiteralBool node) {
- addLiteral(node.value ? const LiteralDartString("true")
- : const LiteralDartString("false"));
+ HInstruction expression = builder.pop();
+ prefix = (prefix === null) ? expression : concat(prefix, expression);
}
void visitStringInterpolation(StringInterpolation node) {
@@ -3359,10 +3317,6 @@ class StringBuilderVisitor extends AbstractVisitor {
visit(node.string);
}
- void visitLiteralString(LiteralString node) {
- addLiteral(node.dartString);
- }
-
void visitStringJuxtaposition(StringJuxtaposition node) {
node.visitChildren(this);
}
@@ -3371,52 +3325,13 @@ class StringBuilderVisitor extends AbstractVisitor {
node.visitChildren(this);
}
- /**
- * Add another literal string to the literalAccumulator.
- */
- void addLiteral(DartString dartString) {
- literalAccumulator = new DartString.concat(literalAccumulator, dartString);
- }
-
- /**
- * Combine the strings in [literalAccumulator] into the prefix instruction.
- * After this, the [literalAccumulator] is empty and [prefix] is non-null.
- */
- void flushLiterals(Node node) {
- if (literalAccumulator.isEmpty()) {
- if (prefix === null) {
- prefix = builder.graph.addConstantString(literalAccumulator, node);
- }
- return;
- }
- HInstruction string =
- builder.graph.addConstantString(literalAccumulator, node);
- literalAccumulator = new DartString.empty();
- if (prefix !== null) {
- prefix = buildConcat(prefix, string);
- } else {
- prefix = string;
- }
- }
-
- HInstruction buildConcat(HInstruction left, HInstruction right) {
- HStatic target = new HStatic(stringConcat);
- builder.add(target);
- builder.push(new HAdd(target, left, right));
- return builder.pop();
- }
-
- HInstruction buildToString(Node node, HInstruction input) {
- HStatic target = new HStatic(stringToString);
- builder.add(target);
- builder.push(new HInvokeStatic(Selector.INVOCATION_1,
- <HInstruction>[target, input],
- HType.STRING));
- return builder.pop();
+ HInstruction concat(HInstruction left, HInstruction right) {
+ HInstruction result = new HStringConcat([left, right]);
+ builder.add(result);
+ return result;
}
HInstruction result(Node node) {
floitsch 2012/06/06 12:13:34 If you keep the result-method, remove the argument
- flushLiterals(node);
return prefix;
}
}

Powered by Google App Engine
This is Rietveld 408576698