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

Side by Side Diff: pkg/compiler/lib/src/js_backend/codegen/codegen.dart

Issue 1195573003: dart2js cps: Refactor and optimize string concatenations. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Remove renegade linebreak Created 5 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 unified diff | Download patch
OLDNEW
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library code_generator; 5 library code_generator;
6 6
7 import 'glue.dart'; 7 import 'glue.dart';
8 8
9 import '../../tree_ir/tree_ir_nodes.dart' as tree_ir; 9 import '../../tree_ir/tree_ir_nodes.dart' as tree_ir;
10 import '../../tree_ir/tree_ir_nodes.dart' show BuiltinOperator; 10 import '../../tree_ir/tree_ir_nodes.dart' show BuiltinOperator;
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after
164 (int index) => visitExpression(expressions[index]), 164 (int index) => visitExpression(expressions[index]),
165 growable: false); 165 growable: false);
166 } 166 }
167 167
168 giveup(tree_ir.Node node, 168 giveup(tree_ir.Node node,
169 [String reason = 'unimplemented in CodeGenerator']) { 169 [String reason = 'unimplemented in CodeGenerator']) {
170 throw new CodegenBailout(node, reason); 170 throw new CodegenBailout(node, reason);
171 } 171 }
172 172
173 @override 173 @override
174 js.Expression visitConcatenateStrings(tree_ir.ConcatenateStrings node) {
175 js.Expression addStrings(js.Expression left, js.Expression right) {
176 return new js.Binary('+', left, right);
177 }
178
179 js.Expression toString(tree_ir.Expression input) {
180 bool useDirectly = input is tree_ir.Constant &&
181 (input.value.isString ||
182 input.value.isInt ||
183 input.value.isBool);
184 js.Expression value = visitExpression(input);
185 if (useDirectly) {
186 return value;
187 } else {
188 Element convertToString = glue.getStringConversion();
189 registry.registerStaticUse(convertToString);
190 js.Expression access = glue.staticFunctionAccess(convertToString);
191 return (new js.Call(access, <js.Expression>[value]));
192 }
193 }
194
195 return node.arguments.map(toString).reduce(addStrings);
196 }
197
198 @override
199 js.Expression visitConditional(tree_ir.Conditional node) { 174 js.Expression visitConditional(tree_ir.Conditional node) {
200 return new js.Conditional( 175 return new js.Conditional(
201 visitExpression(node.condition), 176 visitExpression(node.condition),
202 visitExpression(node.thenExpression), 177 visitExpression(node.thenExpression),
203 visitExpression(node.elseExpression)); 178 visitExpression(node.elseExpression));
204 } 179 }
205 180
206 js.Expression buildConstant(ConstantValue constant) { 181 js.Expression buildConstant(ConstantValue constant) {
207 registry.registerCompileTimeConstant(constant); 182 registry.registerCompileTimeConstant(constant);
208 return glue.constantReference(constant); 183 return glue.constantReference(constant);
(...skipping 508 matching lines...) Expand 10 before | Expand all | Expand 10 after
717 case BuiltinOperator.NumXor: 692 case BuiltinOperator.NumXor:
718 return js.js('(# ^ #) >>> 0', args); 693 return js.js('(# ^ #) >>> 0', args);
719 case BuiltinOperator.NumLt: 694 case BuiltinOperator.NumLt:
720 return new js.Binary('<', args[0], args[1]); 695 return new js.Binary('<', args[0], args[1]);
721 case BuiltinOperator.NumLe: 696 case BuiltinOperator.NumLe:
722 return new js.Binary('<=', args[0], args[1]); 697 return new js.Binary('<=', args[0], args[1]);
723 case BuiltinOperator.NumGt: 698 case BuiltinOperator.NumGt:
724 return new js.Binary('>', args[0], args[1]); 699 return new js.Binary('>', args[0], args[1]);
725 case BuiltinOperator.NumGe: 700 case BuiltinOperator.NumGe:
726 return new js.Binary('>=', args[0], args[1]); 701 return new js.Binary('>=', args[0], args[1]);
702 case BuiltinOperator.StringConcat:
703 if (args.isEmpty) return js.string('');
704 return args.reduce((e1,e2) => new js.Binary('+', e1, e2));
727 case BuiltinOperator.StrictEq: 705 case BuiltinOperator.StrictEq:
728 return new js.Binary('===', args[0], args[1]); 706 return new js.Binary('===', args[0], args[1]);
729 case BuiltinOperator.StrictNeq: 707 case BuiltinOperator.StrictNeq:
730 return new js.Binary('!==', args[0], args[1]); 708 return new js.Binary('!==', args[0], args[1]);
731 case BuiltinOperator.LooseEq: 709 case BuiltinOperator.LooseEq:
732 return new js.Binary('==', args[0], args[1]); 710 return new js.Binary('==', args[0], args[1]);
733 case BuiltinOperator.LooseNeq: 711 case BuiltinOperator.LooseNeq:
734 return new js.Binary('!=', args[0], args[1]); 712 return new js.Binary('!=', args[0], args[1]);
735 case BuiltinOperator.IsFalsy: 713 case BuiltinOperator.IsFalsy:
736 return new js.Prefix('!', args[0]); 714 return new js.Prefix('!', args[0]);
737 case BuiltinOperator.IsNumber: 715 case BuiltinOperator.IsNumber:
738 return js.js("typeof # === 'number'", args); 716 return js.js("typeof # === 'number'", args);
739 case BuiltinOperator.IsNotNumber: 717 case BuiltinOperator.IsNotNumber:
740 return js.js("typeof # !== 'number'", args); 718 return js.js("typeof # !== 'number'", args);
741 case BuiltinOperator.IsFloor: 719 case BuiltinOperator.IsFloor:
742 return js.js("Math.floor(#) === #", args); 720 return js.js("Math.floor(#) === #", args);
743 case BuiltinOperator.IsNumberAndFloor: 721 case BuiltinOperator.IsNumberAndFloor:
744 return js.js("typeof # === 'number' && Math.floor(#) === #", args); 722 return js.js("typeof # === 'number' && Math.floor(#) === #", args);
745 } 723 }
746 } 724 }
747 725
748 visitFunctionExpression(tree_ir.FunctionExpression node) { 726 visitFunctionExpression(tree_ir.FunctionExpression node) {
749 // FunctionExpressions are currently unused. 727 // FunctionExpressions are currently unused.
750 // We might need them if we want to emit raw JS nested functions. 728 // We might need them if we want to emit raw JS nested functions.
751 throw 'FunctionExpressions should not be used'; 729 throw 'FunctionExpressions should not be used';
752 } 730 }
753 } 731 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698