| 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 List<Statement> statements = <Statement>[]; | 14 final List<Statement> statements = <Statement>[]; |
| 14 final List<Block> predecessors = <Block>[]; | 15 final List<Block> predecessors = <Block>[]; |
| 15 final List<Block> successors = <Block>[]; | 16 final List<Block> successors = <Block>[]; |
| 16 | 17 |
| 17 String get name => 'B$index'; | 18 String get name => 'B$index'; |
| 18 | 19 |
| 20 Block([this.label]); |
| 21 |
| 19 void addEdgeTo(Block successor) { | 22 void addEdgeTo(Block successor) { |
| 20 successors.add(successor); | 23 successors.add(successor); |
| 21 successor.predecessors.add(this); | 24 successor.predecessors.add(this); |
| 22 } | 25 } |
| 23 } | 26 } |
| 24 | 27 |
| 25 class BlockCollector extends Visitor { | 28 class BlockCollector extends Visitor { |
| 26 // Accumulate a list of blocks. The current block is the last block in | 29 // Accumulate a list of blocks. The current block is the last block in |
| 27 // the list. | 30 // the list. |
| 28 final List<Block> blocks = [new Block()..index = 0]; | 31 final List<Block> blocks = [new Block()..index = 0]; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 52 visitInvokeConstructor(InvokeConstructor node) {} | 55 visitInvokeConstructor(InvokeConstructor node) {} |
| 53 visitConcatenateStrings(ConcatenateStrings node) {} | 56 visitConcatenateStrings(ConcatenateStrings node) {} |
| 54 visitLiteralList(LiteralList node) {} | 57 visitLiteralList(LiteralList node) {} |
| 55 visitLiteralMap(LiteralMap node) {} | 58 visitLiteralMap(LiteralMap node) {} |
| 56 visitConstant(Constant node) {} | 59 visitConstant(Constant node) {} |
| 57 visitConditional(Conditional node) {} | 60 visitConditional(Conditional node) {} |
| 58 visitLogicalOperator(LogicalOperator node) {} | 61 visitLogicalOperator(LogicalOperator node) {} |
| 59 visitNot(Not node) {} | 62 visitNot(Not node) {} |
| 60 | 63 |
| 61 visitLabeledStatement(LabeledStatement node) { | 64 visitLabeledStatement(LabeledStatement node) { |
| 62 Block target = new Block(); | 65 Block target = new Block(node.label); |
| 63 breakTargets[node.label] = target; | 66 breakTargets[node.label] = target; |
| 64 visitStatement(node.body); | 67 visitStatement(node.body); |
| 65 _addBlock(target); | 68 _addBlock(target); |
| 66 visitStatement(node.next); | 69 visitStatement(node.next); |
| 67 } | 70 } |
| 68 | 71 |
| 69 visitAssign(Assign node) { | 72 visitAssign(Assign node) { |
| 70 _addStatement(node); | 73 _addStatement(node); |
| 71 visitStatement(node.next); | 74 visitStatement(node.next); |
| 72 } | 75 } |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 printProperty("successors", block.successors.map((b) => b.name)); | 148 printProperty("successors", block.successors.map((b) => b.name)); |
| 146 printEmptyProperty("xhandlers"); | 149 printEmptyProperty("xhandlers"); |
| 147 printEmptyProperty("flags"); | 150 printEmptyProperty("flags"); |
| 148 tag("states", () { | 151 tag("states", () { |
| 149 tag("locals", () { | 152 tag("locals", () { |
| 150 printProperty("size", 0); | 153 printProperty("size", 0); |
| 151 printProperty("method", "None"); | 154 printProperty("method", "None"); |
| 152 }); | 155 }); |
| 153 }); | 156 }); |
| 154 tag("HIR", () { | 157 tag("HIR", () { |
| 158 if (block.label != null) { |
| 159 printStatement(null, |
| 160 "Label ${block.name}, breakCount=${block.label.breakCount}"); |
| 161 } |
| 155 block.statements.forEach(visitStatement); | 162 block.statements.forEach(visitStatement); |
| 156 }); | 163 }); |
| 157 }); | 164 }); |
| 158 } | 165 } |
| 159 | 166 |
| 160 void printStatement(String name, String contents) { | 167 void printStatement(String name, String contents) { |
| 161 int bci = 0; | 168 int bci = 0; |
| 162 int uses = 0; | 169 int uses = 0; |
| 163 if (name == null) { | 170 if (name == null) { |
| 164 name = 'x${statementCounter++}'; | 171 name = 'x${statementCounter++}'; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 179 printStatement(null, "dead-use ${node.value}"); | 186 printStatement(null, "dead-use ${node.value}"); |
| 180 } | 187 } |
| 181 | 188 |
| 182 visitLabeledStatement(LabeledStatement node) { | 189 visitLabeledStatement(LabeledStatement node) { |
| 183 // These do not get added to a block's list of statements. | 190 // These do not get added to a block's list of statements. |
| 184 } | 191 } |
| 185 | 192 |
| 186 visitAssign(Assign node) { | 193 visitAssign(Assign node) { |
| 187 String name = names.varName(node.variable); | 194 String name = names.varName(node.variable); |
| 188 String rhs = expr(node.definition); | 195 String rhs = expr(node.definition); |
| 189 printStatement(name, "let $name = $rhs"); | 196 String extra = node.hasExactlyOneUse ? "[single-use]" : ""; |
| 197 printStatement(null, "assign $name = $rhs $extra"); |
| 190 } | 198 } |
| 191 | 199 |
| 192 visitInvokeMethod(InvokeMethod node) { | 200 visitInvokeMethod(InvokeMethod node) { |
| 193 printStatement(null, expr(node)); | 201 printStatement(null, expr(node)); |
| 194 } | 202 } |
| 195 | 203 |
| 196 visitInvokeConstructor(InvokeConstructor node) { | 204 visitInvokeConstructor(InvokeConstructor node) { |
| 197 printStatement(null, expr(node)); | 205 printStatement(null, expr(node)); |
| 198 } | 206 } |
| 199 | 207 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 224 visitReturn(Return node) { | 232 visitReturn(Return node) { |
| 225 printStatement(null, "return ${expr(node.value)}"); | 233 printStatement(null, "return ${expr(node.value)}"); |
| 226 } | 234 } |
| 227 | 235 |
| 228 visitBreak(Break node) { | 236 visitBreak(Break node) { |
| 229 printStatement(null, "break ${collector.breakTargets[node.target].name}"); | 237 printStatement(null, "break ${collector.breakTargets[node.target].name}"); |
| 230 } | 238 } |
| 231 | 239 |
| 232 visitContinue(Continue node) { | 240 visitContinue(Continue node) { |
| 233 printStatement(null, | 241 printStatement(null, |
| 234 "continue ${collector.breakTargets[node.target].name}"); | 242 "continue ${collector.continueTargets[node.target].name}"); |
| 235 } | 243 } |
| 236 | 244 |
| 237 visitIf(If node) { | 245 visitIf(If node) { |
| 238 String condition = expr(node.condition); | 246 String condition = expr(node.condition); |
| 239 String thenTarget = collector.ifTargets[node.thenStatement].name; | 247 String thenTarget = collector.ifTargets[node.thenStatement].name; |
| 240 String elseTarget = collector.ifTargets[node.elseStatement].name; | 248 String elseTarget = collector.ifTargets[node.elseStatement].name; |
| 241 printStatement(null, "if $condition then $thenTarget else $elseTarget"); | 249 printStatement(null, "if $condition then $thenTarget else $elseTarget"); |
| 242 } | 250 } |
| 243 | 251 |
| 244 visitWhile(While node) { | 252 visitWhile(While node) { |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 381 * name to avoid clashing with a previously synthesized variable name. | 389 * name to avoid clashing with a previously synthesized variable name. |
| 382 */ | 390 */ |
| 383 class Names { | 391 class Names { |
| 384 final Map<Variable, String> _names = {}; | 392 final Map<Variable, String> _names = {}; |
| 385 final Set<String> _usedNames = new Set(); | 393 final Set<String> _usedNames = new Set(); |
| 386 int _counter = 0; | 394 int _counter = 0; |
| 387 | 395 |
| 388 String varName(Variable v) { | 396 String varName(Variable v) { |
| 389 String name = _names[v]; | 397 String name = _names[v]; |
| 390 if (name == null) { | 398 if (name == null) { |
| 391 name = v.name; | 399 String prefix = v.element == null ? 'v' : '${v.element.name}_'; |
| 392 if (v.cachedName != null) { | |
| 393 name = v.cachedName; | |
| 394 } | |
| 395 while (name == null || _usedNames.contains(name)) { | 400 while (name == null || _usedNames.contains(name)) { |
| 396 name = "v${_counter++}"; | 401 name = "$prefix${_counter++}"; |
| 397 } | 402 } |
| 398 _names[v] = name; | 403 _names[v] = name; |
| 399 _usedNames.add(name); | 404 _usedNames.add(name); |
| 400 } | 405 } |
| 401 return name; | 406 return name; |
| 402 } | 407 } |
| 403 } | 408 } |
| OLD | NEW |