| 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 tree_ir_tracer; | 5 library tree_ir_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 'tree_ir_nodes.dart'; | 9 import 'tree_ir_nodes.dart'; |
| 10 import 'optimization/optimization.dart'; |
| 10 | 11 |
| 11 class Block { | 12 class Block { |
| 12 Label label; | 13 Label label; |
| 13 int index; | 14 int index; |
| 14 /// Mixed list of [Statement] and [Block]. | 15 /// Mixed list of [Statement] and [Block]. |
| 15 /// A [Block] represents a synthetic goto statement. | 16 /// A [Block] represents a synthetic goto statement. |
| 16 final List statements = []; | 17 final List statements = []; |
| 17 final List<Block> predecessors = <Block>[]; | 18 final List<Block> predecessors = <Block>[]; |
| 18 final List<Block> successors = <Block>[]; | 19 final List<Block> successors = <Block>[]; |
| 19 | 20 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 45 blocks.last.statements.add(target); | 46 blocks.last.statements.add(target); |
| 46 } | 47 } |
| 47 | 48 |
| 48 void _addBlock(Block block) { | 49 void _addBlock(Block block) { |
| 49 block.index = blocks.length; | 50 block.index = blocks.length; |
| 50 blocks.add(block); | 51 blocks.add(block); |
| 51 } | 52 } |
| 52 | 53 |
| 53 void collect(ExecutableDefinition node) { | 54 void collect(ExecutableDefinition node) { |
| 54 if (node.body != null) { | 55 if (node.body != null) { |
| 56 if (node is ConstructorDefinition) { |
| 57 for (Initializer initializer in node.initializers) { |
| 58 if (initializer is FieldInitializer) { |
| 59 visitStatement(initializer.body); |
| 60 } |
| 61 } |
| 62 } |
| 55 visitStatement(node.body); | 63 visitStatement(node.body); |
| 56 } | 64 } |
| 57 } | 65 } |
| 58 | 66 |
| 59 visitLabeledStatement(LabeledStatement node) { | 67 visitLabeledStatement(LabeledStatement node) { |
| 60 Block target = new Block(node.label); | 68 Block target = new Block(node.label); |
| 61 breakTargets[node.label] = target; | 69 breakTargets[node.label] = target; |
| 62 visitStatement(node.body); | 70 visitStatement(node.body); |
| 63 _addBlock(target); | 71 _addBlock(target); |
| 64 visitStatement(node.next); | 72 visitStatement(node.next); |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 | 143 |
| 136 visitExpressionStatement(ExpressionStatement node) { | 144 visitExpressionStatement(ExpressionStatement node) { |
| 137 _addStatement(node); | 145 _addStatement(node); |
| 138 visitStatement(node.next); | 146 visitStatement(node.next); |
| 139 } | 147 } |
| 140 | 148 |
| 141 visitFunctionDeclaration(FunctionDeclaration node) { | 149 visitFunctionDeclaration(FunctionDeclaration node) { |
| 142 _addStatement(node); | 150 _addStatement(node); |
| 143 visitStatement(node.next); | 151 visitStatement(node.next); |
| 144 } | 152 } |
| 153 |
| 145 } | 154 } |
| 146 | 155 |
| 147 class TreeTracer extends TracerUtil with StatementVisitor { | 156 class TreeTracer extends TracerUtil with StatementVisitor, PassMixin { |
| 148 final EventSink<String> output; | 157 final EventSink<String> output; |
| 149 | 158 |
| 150 TreeTracer(this.output); | 159 TreeTracer(this.output); |
| 151 | 160 |
| 152 Names names; | 161 Names names; |
| 153 BlockCollector collector; | 162 BlockCollector collector; |
| 154 int statementCounter; | 163 int statementCounter; |
| 155 | 164 |
| 156 void traceGraph(String name, ExecutableDefinition node) { | 165 void traceGraph(String name, ExecutableDefinition node) { |
| 166 if (node is FunctionDefinition && node.isAbstract) return; |
| 167 if (node is FieldDefinition && node.body == null) return; |
| 168 tag("cfg", () { |
| 169 printProperty("name", name); |
| 170 rewrite(node); |
| 171 collector.blocks.forEach(printBlock); |
| 172 }); |
| 173 } |
| 174 |
| 175 @override |
| 176 void rewriteExecutableDefinition(ExecutableDefinition node) { |
| 177 collector = new BlockCollector(); |
| 157 names = new Names(); | 178 names = new Names(); |
| 158 statementCounter = 0; | 179 statementCounter = 0; |
| 159 collector = new BlockCollector(); | 180 collector = new BlockCollector(); |
| 160 collector.collect(node); | 181 collector.collect(node); |
| 161 tag("cfg", () { | 182 collector.blocks.forEach(printBlock); |
| 162 printProperty("name", name); | |
| 163 int blockCounter = 0; | |
| 164 collector.blocks.forEach(printBlock); | |
| 165 }); | |
| 166 names = null; | |
| 167 } | 183 } |
| 168 | 184 |
| 169 void printBlock(Block block) { | 185 void printBlock(Block block) { |
| 170 tag("block", () { | 186 tag("block", () { |
| 171 printProperty("name", block.name); | 187 printProperty("name", block.name); |
| 172 printProperty("from_bci", -1); | 188 printProperty("from_bci", -1); |
| 173 printProperty("to_bci", -1); | 189 printProperty("to_bci", -1); |
| 174 printProperty("predecessors", block.predecessors.map((b) => b.name)); | 190 printProperty("predecessors", block.predecessors.map((b) => b.name)); |
| 175 printProperty("successors", block.successors.map((b) => b.name)); | 191 printProperty("successors", block.successors.map((b) => b.name)); |
| 176 printEmptyProperty("xhandlers"); | 192 printEmptyProperty("xhandlers"); |
| (...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 386 if (usesInfixNotation(node.operand)) { | 402 if (usesInfixNotation(node.operand)) { |
| 387 operand = '($operand)'; | 403 operand = '($operand)'; |
| 388 } | 404 } |
| 389 return '!$operand'; | 405 return '!$operand'; |
| 390 } | 406 } |
| 391 | 407 |
| 392 String visitFunctionExpression(FunctionExpression node) { | 408 String visitFunctionExpression(FunctionExpression node) { |
| 393 return "function ${node.definition.element.name}"; | 409 return "function ${node.definition.element.name}"; |
| 394 } | 410 } |
| 395 | 411 |
| 412 String visitFieldInitializer(FieldInitializer node) { |
| 413 throw "$node should not be visited by $this"; |
| 414 } |
| 415 |
| 416 String visitSuperInitializer(SuperInitializer node) { |
| 417 throw "$node should not be visited by $this"; |
| 418 } |
| 419 |
| 396 } | 420 } |
| 397 | 421 |
| 398 /** | 422 /** |
| 399 * Invents (and remembers) names for Variables that do not have an associated | 423 * Invents (and remembers) names for Variables that do not have an associated |
| 400 * identifier. | 424 * identifier. |
| 401 * | 425 * |
| 402 * In case a variable is named v0, v1, etc, it may be assigned a different | 426 * In case a variable is named v0, v1, etc, it may be assigned a different |
| 403 * name to avoid clashing with a previously synthesized variable name. | 427 * name to avoid clashing with a previously synthesized variable name. |
| 404 */ | 428 */ |
| 405 class Names { | 429 class Names { |
| 406 final Map<Variable, String> _names = {}; | 430 final Map<Variable, String> _names = {}; |
| 407 final Set<String> _usedNames = new Set(); | 431 final Set<String> _usedNames = new Set(); |
| 408 int _counter = 0; | 432 int _counter = 0; |
| 409 | 433 |
| 410 String varName(Variable v) { | 434 String varName(Variable v) { |
| 411 String name = _names[v]; | 435 String name = _names[v]; |
| 412 if (name == null) { | 436 if (name == null) { |
| 413 String prefix = v.element == null ? 'v' : '${v.element.name}_'; | 437 String prefix = v.element == null ? 'v' : '${v.element.name}_'; |
| 414 while (name == null || _usedNames.contains(name)) { | 438 while (name == null || _usedNames.contains(name)) { |
| 415 name = "$prefix${_counter++}"; | 439 name = "$prefix${_counter++}"; |
| 416 } | 440 } |
| 417 _names[v] = name; | 441 _names[v] = name; |
| 418 _usedNames.add(name); | 442 _usedNames.add(name); |
| 419 } | 443 } |
| 420 return name; | 444 return name; |
| 421 } | 445 } |
| 422 } | 446 } |
| OLD | NEW |