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

Side by Side Diff: sdk/lib/_internal/compiler/implementation/dart_backend/dart_codegen.dart

Issue 318723002: dart2dart: Restore loops with conditions and updates in backend. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Test cases Created 6 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 | Annotate | Revision Log
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 dart_codegen; 5 library dart_codegen;
6 6
7 import 'dart_tree.dart' as tree; 7 import 'dart_tree.dart' as tree;
8 import 'dart_printer.dart'; 8 import 'dart_printer.dart';
9 import 'dart_tree_printer.dart' show TreePrinter; 9 import 'dart_tree_printer.dart' show TreePrinter;
10 import '../tree/tree.dart' as frontend; 10 import '../tree/tree.dart' as frontend;
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after
205 // Fall through to break target 205 // Fall through to break target
206 } else if (fall is tree.Break && fall.target == stmt.target) { 206 } else if (fall is tree.Break && fall.target == stmt.target) {
207 // Fall through to equivalent break 207 // Fall through to equivalent break
208 } else { 208 } else {
209 usedLabels.add(stmt.target); 209 usedLabels.add(stmt.target);
210 statementBuffer.add(new Break(stmt.target.name)); 210 statementBuffer.add(new Break(stmt.target.name));
211 } 211 }
212 } 212 }
213 213
214 void visitContinue(tree.Continue stmt) { 214 void visitContinue(tree.Continue stmt) {
215 statementBuffer.add(new Continue(stmt.target.name)); 215 tree.Statement fall = fallthrough;
216 if (stmt.target.binding == fall) {
217 // Fall through to continue target
218 } else if (fall is tree.Continue && fall.target == stmt.target) {
219 // Fall through to equivalent continue
220 } else {
221 usedLabels.add(stmt.target);
222 statementBuffer.add(new Continue(stmt.target.name));
223 }
216 } 224 }
217 225
218 void visitIf(tree.If stmt) { 226 void visitIf(tree.If stmt) {
219 Expression condition = visitExpression(stmt.condition); 227 Expression condition = visitExpression(stmt.condition);
220 List<Statement> savedBuffer = statementBuffer; 228 List<Statement> savedBuffer = statementBuffer;
221 List<Statement> thenBuffer = statementBuffer = <Statement>[]; 229 List<Statement> thenBuffer = statementBuffer = <Statement>[];
222 visitStatement(stmt.thenStatement); 230 visitStatement(stmt.thenStatement);
223 List<Statement> elseBuffer = statementBuffer = <Statement>[]; 231 List<Statement> elseBuffer = statementBuffer = <Statement>[];
224 visitStatement(stmt.elseStatement); 232 visitStatement(stmt.elseStatement);
225 savedBuffer.add( 233 savedBuffer.add(
226 new If(condition, new Block(thenBuffer), new Block(elseBuffer))); 234 new If(condition, new Block(thenBuffer), new Block(elseBuffer)));
227 statementBuffer = savedBuffer; 235 statementBuffer = savedBuffer;
228 } 236 }
229 237
230 void visitWhile(tree.While stmt) { 238 void visitWhileTrue(tree.WhileTrue stmt) {
231 Expression condition = new Literal(new dart2js.BoolConstant(true)); 239 List<Expression> updates = stmt.updates.reversed
240 .map(visitExpression)
241 .toList(growable:false);
242
232 List<Statement> savedBuffer = statementBuffer; 243 List<Statement> savedBuffer = statementBuffer;
244 tree.Statement savedFallthrough = fallthrough;
233 statementBuffer = <Statement>[]; 245 statementBuffer = <Statement>[];
234 tree.Statement savedFallthrough = fallthrough; 246 fallthrough = stmt;
235 fallthrough = stmt.body; 247
236 visitStatement(stmt.body); 248 visitStatement(stmt.body);
237 savedBuffer.add( 249 Statement body = new Block(statementBuffer);
238 new LabeledStatement( 250 Statement statement = new For(null, null, updates, body);
239 stmt.label.name, 251 if (usedLabels.remove(stmt.label.name)) {
240 new While(condition, new Block(statementBuffer)))); 252 statement = new LabeledStatement(stmt.label.name, statement);
253 }
254 savedBuffer.add(statement);
255
241 statementBuffer = savedBuffer; 256 statementBuffer = savedBuffer;
242 fallthrough = savedFallthrough; 257 fallthrough = savedFallthrough;
243 } 258 }
244 259
260 void visitWhileCondition(tree.WhileCondition stmt) {
261 Expression condition = visitExpression(stmt.condition);
262 List<Expression> updates = stmt.updates.reversed
263 .map(visitExpression)
264 .toList(growable:false);
265
266 List<Statement> savedBuffer = statementBuffer;
267 tree.Statement savedFallthrough = fallthrough;
268 statementBuffer = <Statement>[];
269 fallthrough = stmt;
270
271 visitStatement(stmt.body);
272 Statement body = new Block(statementBuffer);
273 Statement statement;
274 if (updates.isEmpty) {
275 // while(E) is the same as for(;E;), but the former is nicer
276 statement = new While(condition, body);
277 } else {
278 statement = new For(null, condition, updates, body);
279 }
280 if (usedLabels.remove(stmt.label.name)) {
281 statement = new LabeledStatement(stmt.label.name, statement);
282 }
283 savedBuffer.add(statement);
284
285 statementBuffer = savedBuffer;
286 fallthrough = savedFallthrough;
287
288 visitStatement(stmt.next);
289 }
290
245 Expression visitConstant(tree.Constant exp) { 291 Expression visitConstant(tree.Constant exp) {
246 return emitConstant(exp.value); 292 return emitConstant(exp.value);
247 } 293 }
248 294
249 Expression visitLiteralList(tree.LiteralList exp) { 295 Expression visitLiteralList(tree.LiteralList exp) {
250 return new LiteralList( 296 return new LiteralList(
251 exp.values.map(visitExpression).toList(growable: false)); 297 exp.values.map(visitExpression).toList(growable: false));
252 } 298 }
253 299
254 Expression visitLiteralMap(tree.LiteralMap exp) { 300 Expression visitLiteralMap(tree.LiteralMap exp) {
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after
430 if (constant is dart2js.ConstructedConstant && constant.isLiteralSymbol) { 476 if (constant is dart2js.ConstructedConstant && constant.isLiteralSymbol) {
431 dart2js.StringConstant nameConstant = constant.fields[0]; 477 dart2js.StringConstant nameConstant = constant.fields[0];
432 String nameString = nameConstant.value.slowToString(); 478 String nameString = nameConstant.value.slowToString();
433 return new LiteralSymbol(nameString); 479 return new LiteralSymbol(nameString);
434 } else { 480 } else {
435 throw "Unsupported constant: $constant"; 481 throw "Unsupported constant: $constant";
436 } 482 }
437 } 483 }
438 } 484 }
439 } 485 }
486
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698