| OLD | NEW |
| 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_backend.tracer; | 5 library dart_backend.tracer; |
| 6 | 6 |
| 7 import 'dart:async' show EventSink; | 7 import 'dart:async' show EventSink; |
| 8 import '../tracer.dart'; | 8 import '../tracer.dart'; |
| 9 import 'dart_tree.dart'; | 9 import 'dart_tree.dart'; |
| 10 | 10 |
| 11 class Block { | 11 class Block { |
| 12 Label label; |
| 12 int index; | 13 int index; |
| 13 final Label label; | |
| 14 /// Mixed list of [Statement] and [Block]. | 14 /// Mixed list of [Statement] and [Block]. |
| 15 /// A [Block] represents a synthetic goto statement. | 15 /// A [Block] represents a synthetic goto statement. |
| 16 final List statements = []; | 16 final List statements = []; |
| 17 final List<Block> predecessors = <Block>[]; | 17 final List<Block> predecessors = <Block>[]; |
| 18 final List<Block> successors = <Block>[]; | 18 final List<Block> successors = <Block>[]; |
| 19 | 19 |
| 20 String get name => 'B$index'; | 20 String get name => 'B$index'; |
| 21 | 21 |
| 22 Block([this.label]); | 22 Block([this.label]); |
| 23 | 23 |
| (...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 add("$bci $uses $name $contents <|@\n"); | 216 add("$bci $uses $name $contents <|@\n"); |
| 217 } | 217 } |
| 218 | 218 |
| 219 visitLabeledStatement(LabeledStatement node) { | 219 visitLabeledStatement(LabeledStatement node) { |
| 220 // These do not get added to a block's list of statements. | 220 // These do not get added to a block's list of statements. |
| 221 } | 221 } |
| 222 | 222 |
| 223 visitAssign(Assign node) { | 223 visitAssign(Assign node) { |
| 224 String name = names.varName(node.variable); | 224 String name = names.varName(node.variable); |
| 225 String rhs = expr(node.definition); | 225 String rhs = expr(node.definition); |
| 226 printStatement(name, "let $name = $rhs"); | 226 String extra = node.hasExactlyOneUse ? "[single-use]" : ""; |
| 227 printStatement(null, "assign $name = $rhs $extra"); |
| 227 } | 228 } |
| 228 | 229 |
| 229 visitReturn(Return node) { | 230 visitReturn(Return node) { |
| 230 printStatement(null, "return ${expr(node.value)}"); | 231 printStatement(null, "return ${expr(node.value)}"); |
| 231 } | 232 } |
| 232 | 233 |
| 233 visitBreak(Break node) { | 234 visitBreak(Break node) { |
| 234 printStatement(null, "break ${collector.breakTargets[node.target].name}"); | 235 printStatement(null, "break ${collector.breakTargets[node.target].name}"); |
| 235 } | 236 } |
| 236 | 237 |
| 237 visitContinue(Continue node) { | 238 visitContinue(Continue node) { |
| 238 printStatement(null, | 239 printStatement(null, |
| 239 "continue ${collector.breakTargets[node.target].name}"); | 240 "continue ${collector.continueTargets[node.target].name}"); |
| 240 } | 241 } |
| 241 | 242 |
| 242 visitIf(If node) { | 243 visitIf(If node) { |
| 243 String condition = expr(node.condition); | 244 String condition = expr(node.condition); |
| 244 String thenTarget = collector.ifTargets[node.thenStatement].name; | 245 String thenTarget = collector.ifTargets[node.thenStatement].name; |
| 245 String elseTarget = collector.ifTargets[node.elseStatement].name; | 246 String elseTarget = collector.ifTargets[node.elseStatement].name; |
| 246 printStatement(null, "if $condition then $thenTarget else $elseTarget"); | 247 printStatement(null, "if $condition then $thenTarget else $elseTarget"); |
| 247 } | 248 } |
| 248 | 249 |
| 249 visitWhileTrue(WhileTrue node) { | 250 visitWhileTrue(WhileTrue node) { |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 390 * name to avoid clashing with a previously synthesized variable name. | 391 * name to avoid clashing with a previously synthesized variable name. |
| 391 */ | 392 */ |
| 392 class Names { | 393 class Names { |
| 393 final Map<Variable, String> _names = {}; | 394 final Map<Variable, String> _names = {}; |
| 394 final Set<String> _usedNames = new Set(); | 395 final Set<String> _usedNames = new Set(); |
| 395 int _counter = 0; | 396 int _counter = 0; |
| 396 | 397 |
| 397 String varName(Variable v) { | 398 String varName(Variable v) { |
| 398 String name = _names[v]; | 399 String name = _names[v]; |
| 399 if (name == null) { | 400 if (name == null) { |
| 400 name = v.name; | 401 String prefix = v.element == null ? 'v' : '${v.element.name}_'; |
| 401 if (v.cachedName != null) { | |
| 402 name = v.cachedName; | |
| 403 } | |
| 404 while (name == null || _usedNames.contains(name)) { | 402 while (name == null || _usedNames.contains(name)) { |
| 405 name = "v${_counter++}"; | 403 name = "$prefix${_counter++}"; |
| 406 } | 404 } |
| 407 _names[v] = name; | 405 _names[v] = name; |
| 408 _usedNames.add(name); | 406 _usedNames.add(name); |
| 409 } | 407 } |
| 410 return name; | 408 return name; |
| 411 } | 409 } |
| 412 } | 410 } |
| OLD | NEW |