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 | 10 |
11 class Block { | 11 class Block { |
12 Label label; | 12 Label label; |
13 int index; | 13 int index; |
| 14 |
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 |
20 /// The catch block associated with the immediately enclosing try block or | 21 /// The catch block associated with the immediately enclosing try block or |
21 /// `null` if not inside a try block. | 22 /// `null` if not inside a try block. |
22 Block catcher; | 23 Block catcher; |
23 | 24 |
(...skipping 20 matching lines...) Expand all Loading... |
44 // (if targets) to blocks. | 45 // (if targets) to blocks. |
45 final Map<Label, Block> breakTargets = <Label, Block>{}; | 46 final Map<Label, Block> breakTargets = <Label, Block>{}; |
46 final Map<Label, Block> continueTargets = <Label, Block>{}; | 47 final Map<Label, Block> continueTargets = <Label, Block>{}; |
47 final Map<Statement, Block> substatements = <Statement, Block>{}; | 48 final Map<Statement, Block> substatements = <Statement, Block>{}; |
48 | 49 |
49 Block catcher; | 50 Block catcher; |
50 | 51 |
51 void _addStatement(Statement statement) { | 52 void _addStatement(Statement statement) { |
52 blocks.last.statements.add(statement); | 53 blocks.last.statements.add(statement); |
53 } | 54 } |
| 55 |
54 void _addGotoStatement(Block target) { | 56 void _addGotoStatement(Block target) { |
55 blocks.last.statements.add(target); | 57 blocks.last.statements.add(target); |
56 } | 58 } |
57 | 59 |
58 void _addBlock(Block block) { | 60 void _addBlock(Block block) { |
59 block.index = blocks.length; | 61 block.index = blocks.length; |
60 block.catcher = catcher; | 62 block.catcher = catcher; |
61 blocks.add(block); | 63 blocks.add(block); |
62 } | 64 } |
63 | 65 |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
223 printProperty("size", 0); | 225 printProperty("size", 0); |
224 printProperty("method", "None"); | 226 printProperty("method", "None"); |
225 }); | 227 }); |
226 }); | 228 }); |
227 tag("HIR", () { | 229 tag("HIR", () { |
228 if (block.isEntryPoint) { | 230 if (block.isEntryPoint) { |
229 String params = parameters.map(names.varName).join(', '); | 231 String params = parameters.map(names.varName).join(', '); |
230 printStatement(null, 'Entry ($params)'); | 232 printStatement(null, 'Entry ($params)'); |
231 } | 233 } |
232 if (block.label != null) { | 234 if (block.label != null) { |
233 printStatement(null, | 235 printStatement( |
234 "Label ${block.name}, useCount=${block.label.useCount}"); | 236 null, "Label ${block.name}, useCount=${block.label.useCount}"); |
235 } | 237 } |
236 if (block.catcher != null) { | 238 if (block.catcher != null) { |
237 printStatement(null, 'Catch exceptions at ${block.catcher.name}'); | 239 printStatement(null, 'Catch exceptions at ${block.catcher.name}'); |
238 } | 240 } |
239 block.statements.forEach(visitBlockMember); | 241 block.statements.forEach(visitBlockMember); |
240 }); | 242 }); |
241 }); | 243 }); |
242 } | 244 } |
243 | 245 |
244 void visitBlockMember(member) { | 246 void visitBlockMember(member) { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
276 printStatement(null, "unreachable"); | 278 printStatement(null, "unreachable"); |
277 } | 279 } |
278 | 280 |
279 visitBreak(Break node) { | 281 visitBreak(Break node) { |
280 Block block = collector.breakTargets[node.target]; | 282 Block block = collector.breakTargets[node.target]; |
281 String name = block != null ? block.name : '<missing label>'; | 283 String name = block != null ? block.name : '<missing label>'; |
282 printStatement(null, "break $name"); | 284 printStatement(null, "break $name"); |
283 } | 285 } |
284 | 286 |
285 visitContinue(Continue node) { | 287 visitContinue(Continue node) { |
286 printStatement(null, | 288 printStatement( |
287 "continue ${collector.continueTargets[node.target].name}"); | 289 null, "continue ${collector.continueTargets[node.target].name}"); |
288 } | 290 } |
289 | 291 |
290 visitIf(If node) { | 292 visitIf(If node) { |
291 String condition = expr(node.condition); | 293 String condition = expr(node.condition); |
292 String thenTarget = collector.substatements[node.thenStatement].name; | 294 String thenTarget = collector.substatements[node.thenStatement].name; |
293 String elseTarget = collector.substatements[node.elseStatement].name; | 295 String elseTarget = collector.substatements[node.elseStatement].name; |
294 printStatement(null, "if $condition then $thenTarget else $elseTarget"); | 296 printStatement(null, "if $condition then $thenTarget else $elseTarget"); |
295 } | 297 } |
296 | 298 |
297 visitWhileTrue(WhileTrue node) { | 299 visitWhileTrue(WhileTrue node) { |
298 printStatement(null, "while true do"); | 300 printStatement(null, "while true do"); |
299 } | 301 } |
300 | 302 |
301 visitFor(For node) { | 303 visitFor(For node) { |
302 String bodyTarget = collector.substatements[node.body].name; | 304 String bodyTarget = collector.substatements[node.body].name; |
303 String nextTarget = collector.substatements[node.next].name; | 305 String nextTarget = collector.substatements[node.next].name; |
304 String updates = node.updates.map(expr).join(', '); | 306 String updates = node.updates.map(expr).join(', '); |
305 printStatement(null, "while ${expr(node.condition)}"); | 307 printStatement(null, "while ${expr(node.condition)}"); |
306 printStatement(null, "do $bodyTarget"); | 308 printStatement(null, "do $bodyTarget"); |
307 printStatement(null, "updates ($updates)"); | 309 printStatement(null, "updates ($updates)"); |
308 printStatement(null, "then $nextTarget" ); | 310 printStatement(null, "then $nextTarget"); |
309 } | 311 } |
310 | 312 |
311 visitTry(Try node) { | 313 visitTry(Try node) { |
312 String tryTarget = collector.substatements[node.tryBody].name; | 314 String tryTarget = collector.substatements[node.tryBody].name; |
313 String catchParams = node.catchParameters.map(names.varName).join(','); | 315 String catchParams = node.catchParameters.map(names.varName).join(','); |
314 String catchTarget = collector.substatements[node.catchBody].name; | 316 String catchTarget = collector.substatements[node.catchBody].name; |
315 printStatement(null, 'try $tryTarget catch($catchParams) $catchTarget'); | 317 printStatement(null, 'try $tryTarget catch($catchParams) $catchTarget'); |
316 } | 318 } |
317 | 319 |
318 visitExpressionStatement(ExpressionStatement node) { | 320 visitExpressionStatement(ExpressionStatement node) { |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
417 String visitConstant(Constant node) { | 419 String visitConstant(Constant node) { |
418 return "${node.value.toStructuredString()}"; | 420 return "${node.value.toStructuredString()}"; |
419 } | 421 } |
420 | 422 |
421 String visitThis(This node) { | 423 String visitThis(This node) { |
422 return "this"; | 424 return "this"; |
423 } | 425 } |
424 | 426 |
425 static bool usesInfixNotation(Expression node) { | 427 static bool usesInfixNotation(Expression node) { |
426 return node is Conditional || | 428 return node is Conditional || |
427 node is LogicalOperator || | 429 node is LogicalOperator || |
428 node is Assign || | 430 node is Assign || |
429 node is SetField; | 431 node is SetField; |
430 } | 432 } |
431 | 433 |
432 String visitConditional(Conditional node) { | 434 String visitConditional(Conditional node) { |
433 String condition = visitExpression(node.condition); | 435 String condition = visitExpression(node.condition); |
434 String thenExpr = visitExpression(node.thenExpression); | 436 String thenExpr = visitExpression(node.thenExpression); |
435 String elseExpr = visitExpression(node.elseExpression); | 437 String elseExpr = visitExpression(node.elseExpression); |
436 return "$condition ? $thenExpr : $elseExpr"; | 438 return "$condition ? $thenExpr : $elseExpr"; |
437 } | 439 } |
438 | 440 |
439 String visitLogicalOperator(LogicalOperator node) { | 441 String visitLogicalOperator(LogicalOperator node) { |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
610 String prefix = v.element == null ? 'v' : '${v.element.name}_'; | 612 String prefix = v.element == null ? 'v' : '${v.element.name}_'; |
611 while (name == null || _usedNames.contains(name)) { | 613 while (name == null || _usedNames.contains(name)) { |
612 name = "$prefix${_counter++}"; | 614 name = "$prefix${_counter++}"; |
613 } | 615 } |
614 _names[v] = name; | 616 _names[v] = name; |
615 _usedNames.add(name); | 617 _usedNames.add(name); |
616 } | 618 } |
617 return name; | 619 return name; |
618 } | 620 } |
619 } | 621 } |
OLD | NEW |